forked from quantenschaum/egg-incubator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathincubator.ino
544 lines (487 loc) · 12.3 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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
#include <avr/wdt.h>
#include <SoftwareServo.h>
//#include <Streaming.h>
//#include <OneWire.h>
//#include <DallasTemperature.h>
//#include <MemoryFree.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
//#define SERIAL_LOGGING
#define DHTPIN 3
//#define ONE_WIRE 3
#define T_OFFSET 0.9
#define FAN_PIN 2
#define FAN_THRES 500
#define BEEPER A2
#define BRIGHTNESS 10
#define HEATER A1
#define DELAY 2000
#define TS_ADDR 0
#define HS_ADDR 4
#define HC_ADDR 8
#define TI_RESET 1
#define HI_RESET 5
#define ALARM_T 2
#define ALARM_H 8
#define H_AUTO_THRES 3
#define H_AUTO_COUNT 200
#define HWAT 0.25 // holt winters parameters for temperature
#define HWBT 0.2
#define HWAH 0.7 // holt winters parameters for humidity
#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 120000L // reset vent after this time (> VENTOPENMS)
SoftwareServo vent;
//OneWire oneWire(ONE_WIRE);
//DallasTemperature sensors(&oneWire);
DHT dht(DHTPIN, DHT22);
//LiquidCrystal lcd(2,3,4,5,6,7);
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;
byte Hcontrol;
byte Ts_changed, Hs_changed;
float ET, dETdt, IETdt;
float EH, dEHdt, IEHdt;
float Tavg = NAN, Tvar;
float Havg = NAN, Hvar;
float Hpower, Hduty;
unsigned long t0, Hon, talarm, tventclosed;
byte c, displayMode;
byte key, bri = 255, alarm;
void setup() {
#if defined(WDT_TIMEOUT)
wdt_enable(WDT_TIMEOUT);
#endif
#if defined(SERIAL_LOGGING)
Serial.begin(115200);
#endif
pinMode(HEATER, OUTPUT);
heater(0);
pinMode(BRIGHTNESS, OUTPUT);
analogWrite(BRIGHTNESS, bri = 255);
lcd.begin(16, 2);
lcd.noCursor();
lcd.print("Incubator 0.5");
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);
}
boolean ventclosed;
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) {
// beep(2000, 50);
float T = dht.readTemperature() + T_OFFSET;
float H = dht.readHumidity();
if (isnan(T) || T < 10 || T > 60 || 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;
int fanrpm = fancount * 60 / dts;
fancount = 0;
float E0 = ET;
ET = HWAT * (T - Ts) + (1 - HWAT) * (ET + dETdt * dts); // double exp smoothing (holt winters)
dETdt = HWBT * (ET - E0) / dts + (1 - HWBT) * dETdt;
IETdt += ET * dts;
if (abs(ET) > TI_RESET)
IETdt = 0;
float pidT = 1.1765 * (ET + 0.010526 * IETdt + 23.75 * dETdt);
Hpower = fanrpm > FAN_THRES ? max(0, min(1, -pidT)) : 0;
heater(Hpower > 0.1);
Hon = millis();
E0 = EH;
EH = HWAH * (H - Hs) + (1 - HWAH) * (EH + dEHdt * dts); // double exp smoothing (holt winters)
dEHdt = HWBH * (EH - E0) / dts + (1 - HWBH) * dEHdt;
IEHdt += EH * dts;
if (abs(EH) > HI_RESET)
IEHdt = 0;
float pidH = 0.1176 * (EH + 0.09091 * IEHdt + 2.75 * dEHdt);
// pidH = max(0, min(1, round(pidH * 10) / 10.0)); // discretize with Hsteps step to avoid mini adjustmnts
vent.write(pidH * 180);
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;
}
}
}
Tavg = A * T + (1 - A) * (isnan(Tavg) ? T : Tavg);
Tvar = A * pow(T - Tavg, 2) + (1 - A) * (isnan(Tvar) ? 0 : Tvar);
float Tstd = sqrt(Tvar);
Havg = A * H + (1 - A) * (isnan(Havg) ? H : Havg);
Hvar = A * pow(H - Havg, 2) + (1 - A) * (isnan(Hvar) ? 0 : Hvar);
float 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 (abs(ET) > ALARM_T) {
alarm |= 1;
vent.write(ET < 0 ? 0 : 180);
if (Hcontrol > 1) {
Hcontrol = 2;
}
} else {
alarm &= ~1;
}
if (abs(EH) > ALARM_H) {
alarm |= 2;
} else {
alarm &= ~2;
}
if (fanrpm < FAN_THRES) {
alarm |= 4;
} else {
alarm &= ~4;
}
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 defined(SERIAL_LOGGING)
// Serial.print("free="); Serial.println(freeMemory());
Serial.print("up=");
Serial.println(t1 / 1000);
Serial.print("dt=");
Serial.println(dts, 4);
Serial.println();
Serial.print("Ts=");
Serial.println(Ts, 4);
Serial.print("Tr=");
Serial.println(T, 4);
Serial.print("T=");
Serial.println(Ts + ET, 4);
Serial.print("ET=");
Serial.println(ET, 4);
Serial.print("dETdt=");
Serial.println(dETdt, 4);
Serial.print("IETdt=");
Serial.println(IETdt, 4);
Serial.print("Tavg=");
Serial.println(Tavg, 4);
Serial.print("Tstd=");
Serial.println(Tstd, 4);
Serial.print("pidT=");
Serial.println(pidT, 4);
Serial.print("Ht=");
Serial.println(Hpower, 4);
Serial.print("Hduty=");
Serial.println(Hduty, 4);
Serial.println();
Serial.print("Hs=");
Serial.println(Hs, 4);
Serial.print("Hcontrol=");
Serial.println(Hcontrol);
Serial.print("Hr=");
Serial.println(H, 4);
Serial.print("H=");
Serial.println(Hs + EH, 4);
Serial.print("EH=");
Serial.println(EH, 4);
Serial.print("dEHdt=");
Serial.println(dEHdt, 4);
Serial.print("IEHdt=");
Serial.println(IEHdt, 4);
Serial.print("Havg=");
Serial.println(Havg, 4);
Serial.print("Hstd=");
Serial.println(Hstd, 4);
Serial.print("pidH=");
Serial.println(pidH, 4);
Serial.print("V=");
Serial.println(vent.read() / 180.0, 4);
Serial.print("Vdeg=");
Serial.println(vent.read());
Serial.print("Fan=");
Serial.println(fanrpm);
Serial.print("vent=");
Serial.println(vent.read());
Serial.print("vent closed=");
Serial.println(ventclosed);
Serial.print("vent closed since=");
Serial.println(millis() - tventclosed);
Serial.println();
#endif
if (key) {
delay(500);
}
if (bri) {
analogWrite(BRIGHTNESS, --bri);
}
key = 0;
t0 = t1;
++c;
}
#if defined(WDT_TIMEOUT)
wdt_reset();
#endif
}