-
Notifications
You must be signed in to change notification settings - Fork 6
/
pwm-fancontroller.ino
124 lines (97 loc) · 2.5 KB
/
pwm-fancontroller.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include <OneWire.h>
int DS18S20_Pin = 6; //DS18S20 Signal pin on digital 2
//Temperature chip i/o
OneWire ds(DS18S20_Pin); // on digital pin 2
int pwmPin = 3; // digital PWM pin 9
int pwmVal = 1; // The PWM Value
unsigned long time;
unsigned int rpm;
String stringRPM;
void setup()
{
// generate 25kHz PWM pulse rate on Pin 3
pinMode(pwmPin, OUTPUT); // OCR2B sets duty cycle
// Set up Fast PWM on Pin 3
TCCR2A = 0x23; // COM2B1, WGM21, WGM20
// Set prescaler
TCCR2B = 0x0A; // WGM21, Prescaler = /8
// Set TOP and initialize duty cycle to zero(0)
OCR2A = 79; // TOP DO NOT CHANGE, SETS PWM PULSE RATE
OCR2B = 0; // duty cycle for Pin 3 (0-79) generates 1 500nS pulse even when 0 :
digitalWrite(2, HIGH); // Starts reading
Serial.begin(9600);
}
float getTemp(){
//returns the temperature from one DS18S20 in DEG Celsius
byte data[12];
byte addr[8];
if ( !ds.search(addr)) {
//no more sensors on chain, reset search
ds.reset_search();
return -1000;
}
if ( OneWire::crc8( addr, 7) != addr[7]) {
Serial.println("CRC is not valid!");
return -1000;
}
if ( addr[0] != 0x10 && addr[0] != 0x28) {
Serial.print("Device is not recognized");
return -1000;
}
ds.reset();
ds.select(addr);
ds.write(0x44,1); // start conversion, with parasite power on at the end
byte present = ds.reset();
ds.select(addr);
ds.write(0xBE); // Read Scratchpad
for (int i = 0; i < 9; i++) { // we need 9 bytes
data[i] = ds.read();
}
ds.reset_search();
byte MSB = data[1];
byte LSB = data[0];
float tempRead = ((MSB << 8) | LSB); //using two's compliment
float TemperatureSum = tempRead / 16;
return TemperatureSum;
}
void loop()
{
unsigned int x;
float temperature = getTemp();
x = 10;
// full on at 55 deegrees
//x = (int)(79 / 55 * temperature) ;
if (temperature > 25) {
if (temperature >= 80) {
x = 79;
} else {
x = 10+temperature;
if (x<=0) {
x = 0;
}
if (x>79) {
x = 79;
}
}
}
OCR2B = x;
time = pulseIn(2, HIGH);
rpm = (1000000 * 60) / (time * 4);
if (rpm==65535)
rpm = 0;
int pwmp = (100*x/79);
Serial.print("temp=");
Serial.print(temperature);
Serial.print(" PWM%=");
Serial.print(pwmp);
Serial.print(" RPM=");
Serial.println(rpm);
}
char getRPMS() {
time = pulseIn(2, HIGH);
rpm = (1000000 * 60) / (time * 4);
stringRPM = String(rpm);
if (stringRPM.length() < 5) {
Serial.println(rpm, DEC);
}
}