-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathfan-controller.ino
More file actions
211 lines (173 loc) · 4.25 KB
/
fan-controller.ino
File metadata and controls
211 lines (173 loc) · 4.25 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#include <Arduino.h>
#include "display.h"
#include "buzzer.h"
#include "config.h"
Sensor* sensors;
Fan* fans;
Buzzer buzzer;
Display display;
bool kickStart;
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
analogWrite(ledPin, 0);
pinMode(buttonPin, INPUT_PULLUP);
buzzer = Buzzer(buzzerPin);
sensors = new Sensor[sensorsLength];
for(uint8_t i = 0 ; i < sensorsLength ; i++)
sensors[i] = Sensor(&sensorDefs[i]);
fans = new Fan[fansLength];
for(uint8_t i = 0 ; i < fansLength ; i++){
fans[i] = Fan(&fanDefs[i], &sensors[fanDefs[i].sensorIndex]);
}
if(hasDisplay){
display = Display(sensors, sensorsLength, fans, fansLength);
}
kickStart = true;
}
void loop() {
static uint8_t counter = 0;
static Mode mode = Mode::Auto;
if(kickStart && millis() > 500)
kickStart = false;
// average temp over a second
for(uint8_t i = 0 ; i < sensorsLength ; i++){
sensors[i].measureTemp();
}
// Handle button actions
if(digitalRead(buttonPin) == 0){
auto startPress = millis();
bool longPress = false;
while(digitalRead(buttonPin) == 0){
auto duration = millis() - startPress;
if(duration > 300){
longPress = true;
break;
}
}
if(longPress){
// Change mode
mode = (Mode)(((int)mode + 1) % 4);
if(buzzer.isActive()){
if(hasDisplay)
buzzer.beep(".");
else{
switch(mode){
case Mode::Auto: buzzer.beep(".-"); break;
case Mode::Low: buzzer.beep(".-.."); break;
case Mode::High: buzzer.beep("...."); break;
case Mode::Full: buzzer.beep("..-."); break;
default: buzzer.beep("----------");
}
}
}
// Will force fan speed set immediately with current temp values
counter = 0;
//Switch off warning led
digitalWrite(ledPin, false);
Serial.print("Mode;");
Serial.println(modeToStr(mode));
}
else{
// Display temps & speeds
Serial.print("Mode;");
Serial.println(modeToStr(mode));
for(uint8_t i = 0 ; i < sensorsLength ; i++){
auto& sensor = sensors[i];
Serial.print("Sensor;");
Serial.print(sensor.def->name);
Serial.print(";");
Serial.print(sensor.smoothedTemp());
Serial.print("°C");
Serial.println("");
}
for(uint8_t i = 0 ; i < fansLength ; i++){
auto& fan = fans[i];
Serial.print("Fan;");
Serial.print(fan.def->name);
Serial.print(";");
Serial.print(fan.currentSpeed());
Serial.print("%");
if(fan.hasTacho()){
Serial.print(";");
Serial.print(fan.getRPM());
Serial.print("RPM");
}
Serial.println("");
}
// Beep first temp
if(buzzer.isActive())
buzzer.beepNumber(sensors[0].smoothedTemp());
}
// Wait release
while(digitalRead(buttonPin) == 0){}
}
// Set fan speeds
if(counter == 0){
// Fan control
for(uint8_t i = 0 ; i < fansLength ; i++){
if(kickStart && fans[i].def->kickStart)
fans[i].setSpeed(100);
else
fans[i].setModeSpeed(mode);
}
if(hasDisplay){
// Update display
display.update(mode);
}
bool ledBlink = false;
bool ledFlash = false;
bool buzzerLongBeep = false;
bool buzzerShortBeep = false;
// Sensor temp warning & malfunction detection
for(uint8_t i = 0 ; i < sensorsLength ; i++){
auto temp = sensors[i].smoothedTemp();
if(temp < -273.0 || temp > 273.0){
// -273.1 means the sensor is disconnected
// +273.1 means the sensor is short circuited
buzzerShortBeep = true;
}
auto& maxTemp = sensors[i].def->maxTemp;
if(!isnan(maxTemp) && temp >= maxTemp){
buzzerLongBeep = true;
}
}
if(mode == Mode::Low)
ledBlink = true;
static bool stateSwitch = false;
stateSwitch = !stateSwitch;
// Led
if(ledBlink)
digitalWrite(ledPin, stateSwitch);
else if(ledFlash){
digitalWrite(ledPin, 1);
delay(50);
digitalWrite(ledPin, 0);
}
else{
digitalWrite(ledPin, 0);
}
// Buzzer
if(buzzerLongBeep){
// Buzz if dangerous temp
if(buzzer.isActive()) buzzer.set(stateSwitch);
else buzzer.set(false);
}
else if(buzzerShortBeep){
buzzer.beep(".");
}
// Tachometer counters reset
for(uint8_t i = 0 ; i < fansLength ; i++){
if(fans[i].hasTacho()){
fans[i].resetTacho();
}
}
}
counter = (counter + 1 ) % 20;
if(counter == 0){
for(uint8_t i = 0 ; i < sensorsLength ; i++){
sensors[i].resetTemp();
}
}
delay(50);
}