forked from quantenschaum/egg-incubator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathincubator.ino
470 lines (418 loc) · 11.7 KB
/
incubator.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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
#include <avr/wdt.h>
#include <SoftwareServo.h>
#include <LiquidCrystal.h>
#include <DHT.h>
#include <EEPROM.h>
// stty -F /dev/ttyACM0 115200 cs8 cread clocal -hupcl time 30 && tee incubator.log </dev/ttyACM0
#define WDT_TIMEOUT WDTO_8S // if defined, enable hardware watchdog
#define DHTPIN 3 // data pin of the DHT T/H sensor
#define T_OFFSET 0.9 // temperature sensor offset
#define FAN_PIN 2 // fan tacho signal pin
#define FAN_THRES 500 // fan alarm threshold
#define BEEPER A2 // pin where beeper is attached
#define BRIGHTNESS 10 // display brightness pin
#define HEATER A1 // heater MOSFET pin
#define DELAY 2000 // loop delay in ms
#define TS_ADDR 0 // temperature set point EEPROM address
#define HS_ADDR 4 // humidity set point EEPROM address
#define HC_ADDR 8 // humiditiy control mode EEPROM address
#define TI_RESET 1 // integral reset threshold, set integral to 0 when T error greater than this
#define HI_RESET 5 // integral reset threshold, set integral to 0 when H error greater than this
#define ALARM_T 2 // temperature alarm threshold, alert if T error greater than this
#define ALARM_H 8 // humidity alarm threshold, alert if H error greater than this
#define H_AUTO_THRES 3 // disable vent control in auto mode if H error < this
#define H_AUTO_COUNT 200 // disable for n cycles
#define HWAT 0.25 // holt winters parameters for temperature smoothing
#define HWBT 0.2
#define HWAH 0.7 // holt winters parameters for humidity smoothing
#define HWBH 0.5
#define A 0.005 // long average parameter
#define VENTCLOSED 80 // consider vent closed if under this angle
#define VENTOPENMS 480000L // open vent if closed longer
#define VENTRESETMS 600000L // reset vent after this time (>VENTOPENMS!)
SoftwareServo vent;
DHT dht(DHTPIN, DHT22);
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
#define RIGHT 16
#define UP 8
#define DOWN 4
#define LEFT 2
#define SELECT 1
#define NO_KEY 0
byte getKey() {
int key = analogRead(0);
if (key < 50) {
return RIGHT;
} else if (key < 150) {
return UP;
} else if (key < 300) {
return DOWN;
} else if (key < 500) {
return LEFT;
} else if (key < 800) {
return SELECT;
} else {
return NO_KEY;
}
}
void eeread(int address, int length, void* p) {
byte* b = (byte*)p;
for (int i = 0; i < length; i++) {
*b++ = EEPROM.read(address + i);
}
}
void eewrite(int address, int length, void* p) {
byte* b = (byte*)p;
for (int i = 0; i < length; i++) {
EEPROM.write(address + i, *b++);
}
}
void write_byte(int address, byte &value) {
eewrite(address, sizeof(value), &value);
}
byte read_byte(int address) {
byte value;
eeread(address, sizeof(value), &value);
return value;
}
void write_int(int address, int &value) {
eewrite(address, sizeof(value), &value);
}
int read_int(int address) {
int value;
eeread(address, sizeof(value), &value);
return value;
}
void write_float(int address, float &value) {
eewrite(address, sizeof(value), &value);
}
float read_float(int address) {
float value;
eeread(address, sizeof(value), &value);
return value;
}
void heater(boolean on) {
digitalWrite(HEATER, !on ? LOW : HIGH);
}
boolean heater() {
return digitalRead(HEATER) == HIGH;
}
volatile int fancount;
void count() {
++fancount;
}
void beep(unsigned long f, unsigned long l) {
pinMode(BEEPER, OUTPUT);
byte v = 0;
f = 500000 / f;
l = (1000 * l) / f;
for (int i = 0; i < l; ++i) {
digitalWrite(BEEPER, v = !v);
delayMicroseconds(f);
}
pinMode(BEEPER, INPUT);
}
float Ts, Hs; // set points
byte Hcontrol; // H control mode
byte Ts_changed, Hs_changed; // setpoint changed flags
float ET, dETdt, IETdt; // prop/diff/integ terms for T
float EH, dEHdt, IEHdt; // prop/diff/integ terms for H
float T, Tavg = NAN, Tvar, Tstd;
float H, Havg = NAN, Hvar, Hstd;
float Hpower, Hduty; // heater current power, average duty cycle
unsigned long t0, Hon, talarm, tventclosed;
byte displayMode;
byte key, bri = 255, alarm;
boolean ventclosed;
int fanrpm;
void setup() {
#if defined(WDT_TIMEOUT)
wdt_enable(WDT_TIMEOUT);
#endif
pinMode(HEATER, OUTPUT);
heater(0);
pinMode(BRIGHTNESS, OUTPUT);
analogWrite(BRIGHTNESS, bri = 255);
lcd.begin(16, 2);
lcd.noCursor();
lcd.print("Incubator 0.7");
lcd.setCursor(0, 1);
lcd.print(__DATE__);
dht.begin();
vent.setMinimumPulse(800);
vent.setMaximumPulse(2600);
vent.attach(11);
// write_float(TS_ADDR, Ts=37.8); write_float(HS_ADDR, Hs=55);
Ts = read_float(TS_ADDR);
Hs = read_float(HS_ADDR);
Hcontrol = read_byte(HC_ADDR);
pinMode(FAN_PIN, INPUT_PULLUP);
attachInterrupt(0, count, FALLING);
sei();
beep(800, 100);
beep(1000, 100);
beep(1200, 100);
beep(1600, 100);
}
void loop() {
unsigned long t1 = millis();
int dt = t1 - t0;
if (!key) {
key = getKey();
}
if (key) {
analogWrite(BRIGHTNESS, bri = 255);
}
if (t1 - Hon > Hpower * DELAY) {
heater(0);
}
if (Hcontrol && Hcontrol < H_AUTO_COUNT) {
vent.refresh();
}
if (alarm && !(alarm & 8)) {
beep(1000, 50);
beep(1414, 50);
}
if (dt > DELAY || key) {
if (!key) {
// beep(2000, 50);
T = dht.readTemperature() + T_OFFSET;
H = dht.readHumidity();
if ((isnan(T) || T < 10 || T > 60) || (Hcontrol && (isnan(H) || H < 5 || H > 95))) {
heater(0);
lcd.clear();
lcd.print("SENSOR ERROR!");
lcd.setCursor(0, 1);
lcd.print("T=");
lcd.print(T);
lcd.print("C H=");
lcd.print(H, 1);
lcd.print("%");
beep(2000, 1000);
return;
}
float dts = dt * 1e-3;
if (dt > DELAY) {
fanrpm = fancount * 60 / dts;
fancount = 0;
}
if (fanrpm < FAN_THRES) {
alarm |= 4;
} else {
alarm &= ~4;
}
// temperature Holt-Winters smoothing
float E0 = ET;
ET = HWAT * (T - Ts) + (1 - HWAT) * (ET + dETdt * dts); // smoothed T error (deviation from set point)
dETdt = HWBT * (ET - E0) / dts + (1 - HWBT) * dETdt; // smoothed derivative
IETdt += ET * dts; // integral of error
if (abs(ET) > TI_RESET) // reset integral on big deviation
IETdt = 0;
float pidT = 1.1765 * (ET + 0.010526 * IETdt + 23.75 * dETdt); // PID value, adjust coefficients to tune
Hpower = fanrpm > FAN_THRES ? max(0, min(1, -pidT)) : 0;
heater(Hpower > 0.1);
Hon = millis();
if (abs(ET) > ALARM_T) {
alarm |= 1;
vent.write(ET < 0 ? 0 : 180);
if (Hcontrol > 1) {
Hcontrol = 2;
}
} else {
alarm &= ~1;
}
// humidity Holt-Winters smoothing
E0 = EH;
EH = HWAH * (H - Hs) + (1 - HWAH) * (EH + dEHdt * dts); // smoothed H error (deviation from set point)
dEHdt = HWBH * (EH - E0) / dts + (1 - HWBH) * dEHdt; // smoothed derivative
IEHdt += EH * dts; // integral of error
if (abs(EH) > HI_RESET) // reset integral on big deviation
IEHdt = 0;
float pidH = 0.1176 * (EH + 0.09091 * IEHdt + 2.75 * dEHdt); // PID value, adjust coefficients to tune
vent.write(pidH * 180);
if (Hcontrol && abs(EH) > ALARM_H) {
alarm |= 2;
} else {
alarm &= ~2;
}
boolean ventclosed0 = ventclosed;
ventclosed = vent.read() < VENTCLOSED;
if (ventclosed && ventclosed != ventclosed0) {
tventclosed = millis();
}
boolean openvent = ventclosed && millis() - tventclosed > VENTOPENMS;
if (openvent) {
vent.write(180);
if (millis() - tventclosed > VENTRESETMS) {
tventclosed = millis();
}
}
if (Hcontrol > 1) {
if (abs(EH) > H_AUTO_THRES || openvent) {
Hcontrol = 2;
} else {
if (Hcontrol < H_AUTO_COUNT) {
++Hcontrol;
} else {
IEHdt = 0;
}
}
}
// long term averages
Tavg = A * T + (1 - A) * (isnan(Tavg) ? T : Tavg);
Tvar = A * pow(T - Tavg, 2) + (1 - A) * (isnan(Tvar) ? 0 : Tvar);
Tstd = sqrt(Tvar);
Havg = A * H + (1 - A) * (isnan(Havg) ? H : Havg);
Hvar = A * pow(H - Havg, 2) + (1 - A) * (isnan(Hvar) ? 0 : Hvar);
Hstd = sqrt(Hvar);
Hduty = A * Hpower + (1 - A) * Hduty;
if (Ts_changed) {
if (Ts_changed-- == 1)
write_float(TS_ADDR, Ts);
}
if (Hs_changed) {
if (Hs_changed-- == 1)
write_float(HS_ADDR, Hs);
}
}
if (key & SELECT) {
displayMode = ++displayMode % 8;
}
lcd.clear();
lcd.print("T=");
lcd.print(Ts + ET);
lcd.print("C H=");
lcd.print(Hs + EH, 1);
lcd.print("%");
lcd.setCursor(0, 1);
float uptime;
char unit;
switch (displayMode) {
case 0: // raw values
lcd.print("T=");
lcd.print(T);
lcd.print("C H=");
lcd.print(H, 1);
lcd.print("%");
break;
case 1: // temperature setpoint
if (key & (UP | DOWN | LEFT | RIGHT)) {
Ts = max(20, min(50, Ts + (key & (UP | RIGHT) ? +1 : -1) * (key & (UP | DOWN) ? 0.1 : 1)));
Ts_changed = 10;
}
lcd.print("Ts=");
lcd.print(Ts);
lcd.print("C");
break;
case 2: // humidity setpoint
if (key & (UP | DOWN)) {
Hs = max(10, min(90, Hs + (key & UP ? +1 : -1)));
Hs_changed = 10;
}
if (key & RIGHT) {
if (Hcontrol < 2) {
Hcontrol = ++Hcontrol;
} else {
Hcontrol = 0;
}
IEHdt = 0;
write_byte(HC_ADDR, Hcontrol);
}
lcd.print("Hs=");
lcd.print(Hs);
lcd.print("% ");
lcd.print(Hcontrol == 1 ? "on" : (Hcontrol > 1 ? "auto" : "off"));
break;
case 3: // average temperatur
lcd.print("Ta=");
lcd.print(Tavg);
lcd.print("C (");
lcd.print(Tstd);
lcd.print(")");
break;
case 4: // average humidity
lcd.print("Ha=");
lcd.print(Havg);
lcd.print("% (");
lcd.print(Hstd);
lcd.print(")");
break;
case 5: // heater duty cycle
lcd.print("Hd=");
lcd.print(Hduty);
lcd.print(" Hp=");
lcd.print(Hpower);
break;
case 6: // air vent
lcd.print("V=");
lcd.print(vent.read() / 180.0);
lcd.print(" F=");
lcd.print(fanrpm);
break;
case 7: // average humidity
uptime = t1 * 1e-3;
unit = 's';
if (uptime > 60) {
uptime /= 60;
unit = 'm';
if (uptime > 60) {
uptime /= 60;
unit = 'h';
if (uptime > 24) {
uptime /= 24;
unit = 'd';
}
}
}
lcd.print("Up=");
lcd.print(uptime, 1);
lcd.print(unit);
break;
default:;
}
if (alarm & 7) {
if (!talarm) {
talarm = millis();
}
// sound on persistent alarm and fan failure
if (millis() - talarm > 300000L || alarm & 4) {
alarm &= ~8;
}
analogWrite(BRIGHTNESS, bri = 255);
lcd.setCursor(0, 0);
if (alarm & 1)
lcd.print("T ");
if (alarm & 2)
lcd.print("H ");
if (alarm & 4)
lcd.print("F ");
lcd.print("ALARM! ");
if (!(alarm & 8) && key) {
alarm |= 8; // alarm acknowledged
talarm = millis();
}
if (!(alarm & 8)) {
lcd.setCursor(0, 1);
lcd.print("T=");
lcd.print(T);
lcd.print("C H=");
lcd.print(H, 1);
lcd.print("%");
}
} else {
alarm = 0;
talarm = 0;
}
if (key) {
delay((key & SELECT) ? 500 : 200);
}
if (bri) {
analogWrite(BRIGHTNESS, --bri);
}
key = 0;
t0 = t1;
}
#if defined(WDT_TIMEOUT)
wdt_reset();
#endif
}