-
Notifications
You must be signed in to change notification settings - Fork 0
/
ACRobotIncubator.ino
638 lines (551 loc) · 11.8 KB
/
ACRobotIncubator.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
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
#include "LCD.h"
#include "Interval.h"
#include "DHT.h"
#include "Config.h"
#include "Button.h"
#define DEBUG
using namespace ACRobot;
#define HOUR_SECS (60*60)
#define DAY_SECS (HOUR_SECS*24UL)
const uint8_t rightSideBtnPin = A0;
const uint8_t rightBtnPin = A1;
const uint8_t downBtnPin = A2;
const uint8_t upBtnPin = A3;
const uint8_t leftBtnPin = A4;
const uint8_t leftSideBtnPin = A5;
const uint8_t lightPin = 13;
const uint8_t eggPin = 10;
const uint8_t fanPin = 9;
const uint8_t hotPin = 8;
const uint8_t vntPin = 7;
const uint8_t dhtPin = 6;
const char *Name = "ACRobotI";
const uint8_t Version = 1;
struct Settings
{
uint8_t stage;
uint32_t time_offset;
};
enum Stages
{
Init,
Stage1,
Stage2_1,
Stage2_2,
Stage3,
Stage4,
NumberOfStages
};
Settings settings = { Init, 0 };
Config<Settings> config(Name, Version, settings);
LCD<LCD_1602A_STD> lcd;
struct Status
{
uint32_t duration;
float min_temp; // temperature
float max_temp;
uint8_t min_hum; // humidity
uint8_t max_hum;
bool turn; // turning
uint32_t turn_interval;
uint32_t turn_duration;
bool vent; // ventilation
uint32_t vent_interval;
uint32_t vent_duration;
const char *desc;
};
Status status[] = {
{ 0, 37.5, 38.5, 60.0, 70.0, false, 0, 0, false, 0, 0, "Init" },
{ DAY_SECS * 4, 38.5, 38.5, 80.0, 85.0, false, 0, 0, false, 0, 0, "Stage1" },
{ DAY_SECS * 8, 37.9, 38.3, 60.0, 65.0, true, HOUR_SECS * 4, 900, true, HOUR_SECS * 4, 60, "Stage2_1" },
{ DAY_SECS * 3, 37.9, 38.3, 60.0, 65.0, true, HOUR_SECS * 4, 900, false, 0 , 0, "Stage2_2" },
{ DAY_SECS * 3, 37.5, 37.5, 80.0, 90.0, false, 0, 0, true, HOUR_SECS * 4, 360, "Stage3" },
{ DAY_SECS * 3, 37.5, 37.5, 80.0, 90.0, false, 0, 0, true, HOUR_SECS * 4, 360, "Stage4" },
};
uint32_t durations[NumberOfStages];
uint32_t total_secs;
uint8_t total_days;
enum IntervalList {
GLOBAL = 0,
SCREEN = 1,
SECOND = 2,
CONFIG = 3,
EGG_TURNING = 4,
};
const unsigned long GLOBAL_INTERVAL = 50;
const unsigned long SCREEN_INTERVAL = 500;
const unsigned long SECOND_INTERVAL = 1000;
const unsigned long CONFIG_INTERVAL = 30000;
const uint8_t NUMBER_OF_INTERVALS = 5;
Intervals<NUMBER_OF_INTERVALS> intervals;
enum SecondsList {
VENTILATION_STARTING = 0,
EGG_TURN_STARTING = 1,
};
const uint8_t NUMBER_OF_SECONDS_INTERVALS = 2;
Intervals<NUMBER_OF_SECONDS_INTERVALS> seconds_intervals;
bool heat_is_on = false;
bool fan_continue_on = false;
bool ventilation_on = false;
bool egg_turning_on = false;
SwitchButton<RattlePressButton> modeSw(rightBtnPin);
DigitalSwitch<RattlePressButton> lightSw(rightSideBtnPin, lightPin);
ControlDigitalButton<Button> eggBtn(leftSideBtnPin, eggPin);
ControlDigitalButton<Button> vntBtn(leftBtnPin, vntPin);
RattlePressButton upBtn(upBtnPin);
RattlePressButton downBtn(downBtnPin);
enum SelectMode
{
NONE_MODE,
STAGE_MODE,
DAY_MODE,
HOUR_MODE,
MINUTE_MODE,
MAX_SELECT_MODE
};
SequenceButton<RattlePressButton> selectBtn(leftBtnPin, MAX_SELECT_MODE);
uint8_t selectMode = NONE_MODE;
Interval fan_continue = 5; // 5 seconds after heating
Interval ventilation;
Interval egg_turning;
DHT dht(dhtPin, DHT11);
uint32_t current_millis;
int poll()
{
current_millis = millis();
modeSw.poll();
upBtn.poll();
downBtn.poll();
selectBtn.poll();
lightSw.poll();
eggBtn.poll();
vntBtn.poll();
config.poll();
return intervals.status(current_millis);
}
static uint8_t stage = 0;
static uint32_t counter = 0;
static const char *str = "Init";
static FlipFlop dhtFlipFLop;
static float hum = 0.0;
static float temp = 0.0;
void init_constants()
{
uint32_t _duration = 0;
for (uint8_t i = 0; i < NumberOfStages; i++)
{
_duration += status[i].duration;
durations[i] = _duration;
}
total_secs = _duration;
total_days = _duration / DAY_SECS;
}
void update_settings()
{
for (uint8_t i = 0; i < NumberOfStages; i++)
{
if (counter < durations[i]) {
stage = i;
break;
}
}
settings.stage = stage;
settings.time_offset = counter;
if (status[stage].turn_interval != seconds_intervals[EGG_TURN_STARTING])
seconds_intervals[EGG_TURN_STARTING] = status[stage].turn_interval;
if (status[stage].turn_duration != egg_turning)
egg_turning = status[stage].turn_duration;
if (status[stage].vent_interval != seconds_intervals[VENTILATION_STARTING])
seconds_intervals[VENTILATION_STARTING] = status[stage].vent_interval;
if (status[stage].vent_duration != ventilation)
ventilation = status[stage].vent_duration;
}
void setup()
{
#ifdef DEBUG
Serial.begin(9600);
#endif
lcd.print("Load settings");
settings = config();
intervals[GLOBAL] = GLOBAL_INTERVAL;
intervals[SCREEN] = SCREEN_INTERVAL;
intervals[SECOND] = SECOND_INTERVAL;
intervals[CONFIG] = CONFIG_INTERVAL;
counter = settings.time_offset;
stage = settings.stage;
init_constants();
str = status[stage].desc;
lcd.clear();
lcd.print("Starting");
pinMode(fanPin, OUTPUT);
pinMode(hotPin, OUTPUT);
pinMode(eggPin, OUTPUT);
pinMode(vntPin, OUTPUT);
dht.begin();
pinMode(upBtnPin, INPUT_PULLUP);
pinMode(downBtnPin, INPUT_PULLUP);
pinMode(rightBtnPin, INPUT_PULLUP);
pinMode(leftBtnPin, INPUT_PULLUP);
pinMode(leftSideBtnPin, INPUT_PULLUP);
#ifdef DEBUG
Serial.println("start");
#endif
}
inline void regulator()
{
if (temp >= status[stage].max_temp) {
digitalWrite(hotPin, LOW);
digitalWrite(fanPin, LOW);
if (heat_is_on) { // after 5 seconds
fan_continue_on = true;
fan_continue.reset(counter);
}
heat_is_on = false;
}
if (temp < status[stage].min_temp) {
digitalWrite(hotPin, HIGH);
digitalWrite(fanPin, HIGH);
heat_is_on = true;
}
}
bool next_stage()
{
bool retval = false;
if (stage < (NumberOfStages-1)) {
counter = durations[stage];
retval = true;
}
return retval;
}
bool prev_stage()
{
bool retval = false;
if (stage > 0) {
counter = 0;
retval = true;
}
if (stage > 1)
counter = durations[stage-2];
return retval;
}
bool next_day()
{
bool retval = false;
if (counter < (total_secs - DAY_SECS)) {
counter += DAY_SECS;
retval = true;
}
return retval;
}
bool prev_day()
{
bool retval = false;
if (counter > DAY_SECS) {
counter -= DAY_SECS;
retval = true;
}
return retval;
}
bool next_hour()
{
bool retval = false;
if (counter < (total_secs - HOUR_SECS)) {
counter += HOUR_SECS;
retval = true;
}
return retval;
}
bool prev_hour()
{
bool retval = false;
if (counter > HOUR_SECS) {
counter -= HOUR_SECS;
retval = true;
}
return retval;
}
bool next_minute()
{
bool retval = false;
if (counter < (total_secs - 60)) {
counter += 60;
retval = true;
}
return retval;
}
bool prev_minute()
{
bool retval = false;
if (counter > 60) {
counter -= 60;
retval = true;
}
return retval;
}
bool upCommand(uint8_t mode)
{
bool retval = false;
switch (mode) {
case STAGE_MODE:
retval = next_stage();
break;
case DAY_MODE:
retval = next_day();
break;
case HOUR_MODE:
retval = next_hour();
break;
case MINUTE_MODE:
retval = next_minute();
break;
}
return retval;
}
bool downCommand(uint8_t mode)
{
bool retval = false;
switch (mode) {
case STAGE_MODE:
retval = prev_stage();
break;
case DAY_MODE:
retval = prev_day();
break;
case HOUR_MODE:
retval = prev_hour();
break;
case MINUTE_MODE:
retval = prev_minute();
break;
}
return retval;
}
void buttons()
{
static bool prevModeSw = false;
selectMode = selectBtn.next();
if(modeSw) {
if (!prevModeSw) {
prevModeSw = true;
selectBtn.reset();
selectMode = NONE_MODE;
}
if(upBtn) {
if(upCommand(selectMode))
update_settings();
}
if(downBtn) {
if(downCommand(selectMode))
update_settings();
}
} else {
if (prevModeSw) {
prevModeSw = false;
config(settings);
}
}
}
void logic()
{
regulator();
buttons();
// humidity by motor
if (fan_continue_on) {
if (fan_continue.poll(counter))
fan_continue_on = false;
else {
#ifdef DEBUG
Serial.println("post_fan = HIGH");
#endif
digitalWrite(fanPin, HIGH);
}
}
if (!heat_is_on && !fan_continue_on) {
digitalWrite(fanPin, LOW);
}
if (ventilation_on) {
if (ventilation.poll(counter))
ventilation_on = false;
else {
#ifdef DEBUG
Serial.println("ventilation = HIGH");
#endif
vntBtn(true);
}
}
if (!ventilation_on) {
vntBtn(false);
}
if (egg_turning_on) {
if (egg_turning.poll(current_millis))
egg_turning_on = false;
else {
#ifdef DEBUG
Serial.println("egg_turning = HIGH");
#endif
eggBtn(true);
}
}
if (!egg_turning_on) {
eggBtn(false);
}
}
uint8_t day()
{
return counter / DAY_SECS;
}
uint8_t days_left()
{
return total_days - day();
}
void printMeasure(float value, char type)
{
if (value < 10.0)
lcd.print(' ');
lcd.print(value, 1);
lcd.print(type);
}
void printNumber(int value)
{
if (value < 10)
lcd.print('0');
lcd.print(value);
}
void printTime(unsigned long seconds)
{
int second = seconds % 60;
int minute = (seconds / 60) % 60;
int hour = (seconds / HOUR_SECS) % 24;
int day = (seconds / DAY_SECS) % 99;
printNumber(day);
lcd.print(':');
printNumber(hour);
lcd.print(':');
printNumber(minute);
lcd.print(':');
printNumber(second);
}
char modeSymbol(uint8_t mode)
{
char symbol = ' ';
switch (mode) {
case STAGE_MODE:
symbol = 'S';
break;
case DAY_MODE:
symbol = 'd';
break;
case HOUR_MODE:
symbol = 'h';
break;
case MINUTE_MODE:
symbol = 'm';
break;
}
return symbol;
}
void showTime()
{
lcd.clear();
lcd.print(day());
lcd.setCursor(2, 0);
lcd.print('|');
printTime(counter);
lcd.setCursor(14, 0);
lcd.print('|');
lcd.print(stage);
lcd.setCursor(0, 1);
lcd.print(days_left());
lcd.setCursor(2, 1);
lcd.print('|');
printTime(total_secs - counter);
lcd.setCursor(14, 1);
lcd.print('|');
lcd.print(modeSymbol(selectMode));
}
void showStatus()
{
float tmp;
lcd.clear();
lcd.print(day());
lcd.setCursor(2, 0);
lcd.print('|');
printMeasure(status[stage].max_temp, 'C');
lcd.setCursor(8, 0);
lcd.print('|');
printMeasure(temp, 'C');
lcd.setCursor(14, 0);
lcd.print('|');
lcd.print(stage);
lcd.setCursor(0, 1);
lcd.print(days_left());
lcd.setCursor(2, 1);
lcd.print('|');
printMeasure(status[stage].max_hum, '%');
lcd.setCursor(8, 1);
lcd.print('|');
printMeasure(hum, '%');
lcd.setCursor(14, 1);
lcd.print('|');
}
void screen()
{
if (modeSw)
showTime();
else
showStatus();
}
void timer()
{
counter++;
#ifdef DEBUG
Serial.print("counter = ");
Serial.println(counter);
#endif
switch (seconds_intervals.status(counter))
{
case VENTILATION_STARTING:
ventilation_on = true; // check for status
#ifdef DEBUG
Serial.println("ventilation_on");
#endif
ventilation.reset(counter);
break;
case EGG_TURN_STARTING:
egg_turning_on = true; // check for status
#ifdef DEBUG
Serial.println("egg_turning_on");
#endif
egg_turning.reset(current_millis);
break;
}
if (dhtFlipFLop) {
float _hum = dht.readHumidity();
if (!isnan(_hum))
hum = _hum;
} else {
float _temp = dht.readTemperature();
if (!isnan(_temp))
temp = _temp;
}
settings.time_offset = counter;
update_settings();
}
void loop()
{
switch (poll())
{
case GLOBAL:
logic();
break;
case SCREEN:
screen();
break;
case SECOND:
timer();
break;
case CONFIG:
config(settings);
break;
}
}