-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfancontrol.ino
66 lines (58 loc) · 1.65 KB
/
fancontrol.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
#include <OneWire.h>
#include <DallasTemperature.h>
#include "Config.h"
OneWire ow(tempSensorPin);
DallasTemperature tempSensors(&ow);
byte fanPWM = 255;
void setup() {
Serial.begin(9600);
tempSensors.begin();
}
void loop() {
/* Get temperature: */
tempSensors.requestTemperatures();
float temp = tempSensors.getTempCByIndex(0);
/* Calculate target fan PWM value: */
byte targetPWM = 0;
if (temp < errTemp) {
/* Error. */
targetPWM = maxPWM;
} else if (temp < minTemp) {
/* Below the minimum temperature. */
targetPWM = 0;
} else if (temp > maxTemp) {
/* Above the maximum temperature. */
targetPWM = maxPWM;
} else {
/* Somewhere in between. */
targetPWM = round((temp - minTemp) * (maxPWM - minPWM) / (maxTemp - minTemp) + minPWM);
}
/* Approach target PWM: */
if (targetPWM < fanPWM) {
/* Current PWM value is higher than our target. */
if ((fanPWM - targetPWM) > minPWMFastApproach) {
/* The difference is high, directly go to target. */
fanPWM = targetPWM;
} else {
/* The difference is low, slowly approach it. */
fanPWM -= maxPWMChange;
}
} else if (targetPWM > fanPWM) {
/* Current PWM value is lower than our target. */
if ((targetPWM - fanPWM) > minPWMFastApproach) {
/* The difference is high, directly go to target. */
fanPWM = targetPWM;
} else {
/* The difference is low, slowly approach it. */
fanPWM += maxPWMChange;
}
}
/* Set fan PWM: */
analogWrite(fanPWMPin, fanPWM);
/* Send debug message: */
Serial.print(temp);
Serial.print(F(", "));
Serial.println(fanPWM);
/* Wait a moment: */
delay(interval);
}