-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathFanController.ino
383 lines (334 loc) · 11 KB
/
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
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
//
// Lucky Resistor's Fan Controller
// ---------------------------------------------------------------------------
// (c)2016 by Lucky Resistor. See LICENSE for details.
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//
#include <Adafruit_MCP23017.h>
#include <Adafruit_RGBLCDShield.h>
#include <DHT.h>
#include <RTClib.h>
#include <SD.h>
#include <SPI.h>
#include "PIDController.h"
// Constants
const uint8_t cFanControlPin = 5; // The pin where the fan PWM is connected.
const uint8_t cTemperatureSensor1Pin = 2; // The pin for the first temperature sensor data.
const uint8_t cTemperatureSensor2Pin = 4; // The pin for the second temperature sensor data.
const uint8_t cSdCardChipSelectPin = 10; // The pin where the SD card chip select is connected.
const uint16_t cUpdateDelay = 2200; // The update every ~2.2 seconds.
const uint8_t cStartFanSpeed = 0x80; // The initial fan speed.
const float cControlTargetTemperature = 32.0f; // The maximum target temperature.
// Custom characters for the LCD display.
const uint8_t cFanCharacterMask[] PROGMEM = {
0b01100,
0b01100,
0b00101,
0b11011,
0b11011,
0b10100,
0b00110,
0b00110,
};
const char cFanCharacter = '\x05';
const uint8_t cTemp1CharacterMask[] PROGMEM = {
0b01000,
0b10100,
0b10100,
0b10100,
0b10100,
0b11101,
0b11101,
0b01000
};
const char cTemp1Character = '\x06';
const uint8_t cTemp2CharacterMask[] PROGMEM = {
0b01000,
0b10101,
0b10101,
0b10100,
0b10100,
0b11101,
0b11101,
0b01000
};
const char cTemp2Character = '\x07';
// The instance to access the LCD display.
Adafruit_RGBLCDShield lcd = Adafruit_RGBLCDShield();
// The two temperature sensors
DHT temperatureSensor1(cTemperatureSensor1Pin, DHT22);
DHT temperatureSensor2(cTemperatureSensor2Pin, DHT22);
// The real time clock
RTC_DS1307 rtc;
// The current fan speed.
uint8_t fanSpeed = cStartFanSpeed;
// The PID controller instance.
lr::PIDController pidController;
/// Setup everything.
///
void setup() {
// Set the pin for the fan PWM control.
pinMode(cFanControlPin, OUTPUT);
analogWrite(cFanControlPin, 0x00);
// Initialize the serial output.
Serial.begin(115200);
while (!Serial) { _NOP(); } // Wait until it is ready.
Serial.println(F("Starting Fan Controller"));
// Setup the LCD display.
lcd.begin(16, 2);
lcd.setBacklight(0x7);
// Add custom characters for the fan as bar display.
uint8_t bars[] = {B10000, B11000, B11100, B11110, B11111};
uint8_t character[8];
for (uint8_t i = 0; i < 5; ++i) {
for (uint8_t j = 0; j < 8; ++j) character[j] = bars[i];
lcd.createChar(i, character);
}
// Add custom characters from the masks.
memcpy_P(character, cFanCharacterMask, 8);
lcd.createChar(cFanCharacter, character);
memcpy_P(character, cTemp1CharacterMask, 8);
lcd.createChar(cTemp1Character, character);
memcpy_P(character, cTemp2CharacterMask, 8);
lcd.createChar(cTemp2Character, character);
// Setup the temperature sensors
temperatureSensor1.begin();
temperatureSensor2.begin();
// Setup the PID controller.
pidController.begin(2.0f, 1.0f, 0.5f);
pidController.setOutputLimits(0.0f, 255.0f);
pidController.setOutputReverse(true);
pidController.setOutputDrift(-0.05f);
pidController.setOutputValue(fanSpeed);
pidController.setTargetValue(cControlTargetTemperature);
// Initialize the SD card library
if (!SD.begin(cSdCardChipSelectPin)) {
Serial.println(F("Initialize SD card failed."));
lcd.print("SD Card Error!");
delay(5000);
lcd.clear();
}
// Write some welcome message and wait 2 seconds.
lcd.print(F(" Fan Controller"));
lcd.setCursor(0, 1);
lcd.print(F(" Welcome!"));
delay(2000);
lcd.clear();
}
/// Print a decimal number padded with an optional zero to the LCD.
///
/// @param value The value to display.
///
void lcdPrintPaddedDecimal(uint8_t value) {
if (value < 10) {
lcd.print('0');
}
lcd.print(value, DEC);
}
/// A special loop for manual control mode.
///
void manualLoop() {
uint8_t displayUpdateDelay = 0; // A delay to keep the update delay.
bool manualDisplay = false; // A flag to switch between the "manual" message and the date/time
uint8_t lastButtonState = 0; // The last button state for comparision
// Loop until "break".
while (true) {
// Get the current button state.
const uint8_t currentButtonState = lcd.readButtons();
// Check which buttons got a press.
const uint8_t pressedButtons = (lastButtonState ^ currentButtonState) & currentButtonState;
// If left increase the fan speed.
if (pressedButtons & BUTTON_LEFT) {
if (fanSpeed < 0x10) {
fanSpeed = 0;
} else {
fanSpeed -= 0x10;
}
analogWrite(cFanControlPin, fanSpeed);
displayUpdateDelay = 0; // force display update.
} else if (pressedButtons & BUTTON_RIGHT) { // if right decrease the fan speed.
if (fanSpeed >= 0xf0) {
fanSpeed = 0xff;
} else {
fanSpeed += 0x10;
}
analogWrite(cFanControlPin, fanSpeed);
displayUpdateDelay = 0; // force display update.
} else if (pressedButtons & BUTTON_DOWN) { // if down, exit this loop.
pidController.setOutputValue(fanSpeed); // Make sure the PID controller knows the new fan speed.
break;
}
lastButtonState = currentButtonState; // Store the current button state for the next loop.
// Every few seconds, refresh the display.
if (displayUpdateDelay == 0) {
// Get the current time.
DateTime now = rtc.now();
// Measure the current temperatures.
const float temperature1 = temperatureSensor1.readTemperature();
const float temperature2 = temperatureSensor2.readTemperature();
// Build the display.
updateTemperatureDisplay(temperature1, temperature2);
updateFanSpeedDisplay();
// Switch between the "manual control" message and the date/time display.
if (manualDisplay) {
lcd.setCursor(0, 1);
lcd.print(F(" Manual Control "));
} else {
updateTimeDisplay(now);
}
manualDisplay = !manualDisplay;
}
// Make sure the display is refreshed as specified in cUpdateDelay.
if (++displayUpdateDelay >= (cUpdateDelay/100)) {
displayUpdateDelay = 0;
}
delay(100);
}
}
/// Update the temperature display
///
/// @param temperature1 The first temperature value in degree celsius.
/// @param temperature2 The second temperature value in degree celsius.
///
void updateTemperatureDisplay(float temperature1, float temperature2) {
lcd.setCursor(0, 0);
lcd.print(cTemp1Character);
lcd.print(static_cast<uint8_t>(round(temperature1)), DEC);
lcd.print('\xdf');
lcd.print('C');
lcd.print(' ');
lcd.print(cTemp2Character);
lcd.print(static_cast<uint8_t>(round(temperature2)), DEC);
lcd.print('\xdf');
lcd.print('C');
}
/// Update the time display.
///
/// @param now The time to display.
///
void updateTimeDisplay(const DateTime &now) {
lcd.setCursor(0, 1);
lcd.print(now.year(), DEC);
lcd.print('-');
lcdPrintPaddedDecimal(now.month());
lcd.print('-');
lcdPrintPaddedDecimal(now.day());
lcd.print(' ');
lcdPrintPaddedDecimal(now.hour());
lcd.print(':');
lcdPrintPaddedDecimal(now.minute());
}
/// Print a single bar character of the given value (0-5).
///
/// @param value The bar value to display (0-5).
///
void lcdPrintBar(const uint8_t value) {
if (value == 0) {
lcd.print(' ');
} else {
lcd.print(static_cast<char>(value-1));
}
}
/// Update the fan speed display.
///
void updateFanSpeedDisplay() {
lcd.setCursor(12, 0);
lcd.print(cFanCharacter);
const uint8_t displayedValue = (fanSpeed>>4);
if (displayedValue > 10) {
lcdPrintBar(5);
lcdPrintBar(5);
lcdPrintBar(displayedValue - 10);
} else if (displayedValue > 5) {
lcdPrintBar(5);
lcdPrintBar(displayedValue - 5);
lcdPrintBar(0);
} else {
lcdPrintBar(displayedValue);
lcdPrintBar(0);
lcdPrintBar(0);
}
}
/// The main loop of the controller
///
void loop() {
// Check if manual mode is requested.
const uint8_t buttonState = lcd.readButtons();
if (buttonState & BUTTON_UP) {
manualLoop(); // go to manual loop, until left back to this loop.
}
// Get the current time from the RTC.
DateTime now = rtc.now();
// First measure the current temperatures.
const float temperature1 = temperatureSensor1.readTemperature();
const float temperature2 = temperatureSensor2.readTemperature();
// Calculate the new fan speed using the PID controller.
const float maximumTemperature = max(temperature1, temperature2);
fanSpeed = static_cast<uint8_t>(pidController.calculateOutput(maximumTemperature));
analogWrite(cFanControlPin, fanSpeed);
// Refresh the display.
updateTemperatureDisplay(temperature1, temperature2);
updateFanSpeedDisplay();
updateTimeDisplay(now);
// Write all values into a log file on the SD card.
writeLogEntry(now, temperature1, temperature2, fanSpeed);
// Wait "cUpdateDelay" miliseconds for the next measurement.
delay(cUpdateDelay);
}
/// Write a single log entry.
///
/// @param time The time to write.
/// @param temperature1 The first temperature to write.
/// @param temperature2 The second temperature to write.
/// @param fanSpeed The fan speed to write.
///
void writeLogEntry(const DateTime &time, const float temperature1, const float temperature2, uint8_t fanSpeed) {
// Generate the log file name with the date and time.
char *fileName = new char[32]; // temperature_yyyy-mm-dd.log
snprintf(fileName, 32, "td%02d%02d%02d.log", (time.year() % 100), time.month(), time.day());
// Open the file on the sd card.
File logFile = SD.open(fileName, FILE_WRITE);
delete fileName;
// Generate the log line.
char line[80]; // yyyy-mm-ddThh:mm:ss,30.76,25.09,128
snprintf(
line,
80,
"%04d-%02d-%02dT%02d:%02d:%02d,%d.%02d,%d.%02d,%d",
time.year(),
time.month(),
time.day(),
time.hour(),
time.minute(),
time.second(),
static_cast<int>(temperature1),
static_cast<int>(temperature1*100.0f)%100,
static_cast<int>(temperature2),
static_cast<int>(temperature2*100.0f)%100,
static_cast<int>(fanSpeed));
// Check if the file could be opened for writing.
if (logFile) {
// Write it into the file.
logFile.println(line);
logFile.close();
} else {
// Display a ! character on the display to signal a problem with log writing.
lcd.setCursor(11, 0);
lcd.print('!');
}
// Send the same log line to the serial line.
Serial.println(line);
}