-
Notifications
You must be signed in to change notification settings - Fork 0
/
aquarium-controller.ino
434 lines (375 loc) · 12.1 KB
/
aquarium-controller.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
#include <Time.h> // https://www.pjrc.com/teensy/td_libs_Time.html
#include <TimeLib.h>
#include <TimeAlarms.h> // https://www.pjrc.com/teensy/td_libs_TimeAlarms.html
#include <LiquidCrystal.h> // https://www.arduino.cc/en/Reference/LiquidCrystal
#include <Streaming.h> // http://arduiniana.org/libraries/streaming/
#include <DS1302RTC.h> // http://playground.arduino.cc/Main/DS1302RTC
// Setup RTC pins:
// CE, IO, CLK
DS1302RTC RTC(11, 10, 9);
/*----------------------------------------------------------------------*
* Set the date and time by entering the following on the Arduino *
* serial monitor: *
* year,month,day,hour,minute,second, *
* *
* Where *
* year can be two or four digits, *
* month is 1-12, *
* day is 1-31, *
* hour is 0-23, and *
* minute and second are 0-59. *
* *
* Entering the final comma delimiter (after "second") will avoid a *
* one-second timeout and will allow the RTC to be set more accurately. *
* *
* No validity checking is done, invalid values or incomplete syntax *
* in the input will result in an incorrect RTC setting. *
* *
*----------------------------------------------------------------------*/
// Initialize the display with the numbers of the interface pins:
// RS EN D4 D5 D6 D7
LiquidCrystal lcd(6, 12, 7, 2, 3, 4);
const int LIGHTS_PWM_PIN = 5;
const int CO2_PIN = 8;
const int AIR_PIN = 13;
const int CO2_ON_HOUR = 12;
const int CO2_ON_MINUTE = 1;
const int CO2_OFF_HOUR = 22;
const int CO2_OFF_MINUTE = 1;
String CO2_STATE = "Off";
const int AIR_ON_HOUR = 10;
const int AIR_ON_MINUTE = 1;
const int AIR_OFF_HOUR = 12;
const int AIR_OFF_MINUTE = 1;
String AIR_STATE = "Off";
const int LIGHTS_ON_HOUR = 13;
const int LIGHTS_ON_MINUTE = 1;
const int LIGHTS_OFF_HOUR = 22;
const int LIGHTS_OFF_MINUTE = 31;
// This is the number of milliseconds between each increment of brightness when ramping
// E.g a value of 7000 means 7x255 is roughly 30 minutes of ramp
const long MILLIS_PER_INCREMENT = 7000; // Roughly 30 minutes
bool LIGHTS_ON = false;
// Max brightness is 255
int currentBrightness = 0;
const long SCREEN_REFRESH_INTERVAL = 10000;
bool INITIALISED_SUCCESS = false;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
pinMode(LIGHTS_PWM_PIN, OUTPUT);
digitalWrite(LIGHTS_PWM_PIN, LOW);
pinMode(CO2_PIN, OUTPUT);
digitalWrite(CO2_PIN, LOW);
pinMode(AIR_PIN, OUTPUT);
digitalWrite(AIR_PIN, LOW);
// set up the LCD's number of columns and rows:
lcd.begin(16, 4);
initialise();
}
void loop() {
// put your main code here, to run repeatedly:
Alarm.delay(0);
readTimeFromSerial();
if (INITIALISED_SUCCESS) {
// Only write current time to serial and update lights and display every 1000ms
static unsigned long since;
if (millisHavePassedSince(1000, since)) {
printDateTime(now());
Serial << endl;
handleLights();
updateDisplay();
}
}
}
void initialise() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(F("Starting..."));
if (!RTC.writeEN()) {
Serial.println(F("The DS1302 is write protected. This normal."));
}
Alarm.delay(1000);
if (RTC.haltRTC()) {
lcd.setCursor(0, 1);
lcd.print(F("RTC Stopped"));
lcd.setCursor(0, 2);
lcd.print(F("Init Failed!"));
Serial.println(F("The DS1302 is stopped. Please set time"));
} else {
// Print a message to the LCD.
// Set the cursor to column 0, line 0
// (note: line 1 is the second row, since counting begins with 0):
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(F("Clock Sync..."));
Serial.println(F("Clock Sync..."));
// setSyncProvider() causes the Time library to synchronize with the
// external RTC by calling RTC.get() every five minutes by default.
setSyncProvider(RTC.get);
setSyncInterval(300);
if (timeStatus() == timeSet) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(F("Time synced"));
Serial.println(F("Time synced"));
delay(1000);
initialiseAlarms();
initialiseCo2();
initialiseAir();
initialiseLights();
lcd.setCursor(0, 2);
lcd.print(F("Initialised"));
Serial.println(F("Initialised"));
INITIALISED_SUCCESS = true;
delay(1000);
lcd.clear();
} else {
Serial.println(F("Time Sync Failed"));
lcd.setCursor(0, 1);
lcd.print(F("Time Sync Failed"));
lcd.setCursor(0, 2);
lcd.print(F("Init Failed!"));
}
}
}
// We prefix 'since' with ampersand in order to use a pointer, rather than clone that parameter.
boolean millisHavePassedSince(unsigned long millisDelay, unsigned long &since) {
unsigned long currentMillis = millis();
if (since == NULL || currentMillis >= since + millisDelay) {
since = currentMillis;
return true;
} else {
return false;
}
}
void initialiseAlarms() {
// N.B by default the Alarms library only supports up to 6 alarms.
// This limit can be extended by editing the library (apparently).
static AlarmID_t lightsOnAlarm = Alarm.alarmRepeat(LIGHTS_ON_HOUR, LIGHTS_ON_MINUTE, 0, lightsOn);
static AlarmID_t lightsOffAlarm = Alarm.alarmRepeat(LIGHTS_OFF_HOUR, LIGHTS_OFF_MINUTE, 0, lightsOff);
static AlarmID_t airOnAlarm = Alarm.alarmRepeat(AIR_ON_HOUR, AIR_ON_MINUTE, 0, airOn);
static AlarmID_t airOffAlarm = Alarm.alarmRepeat(AIR_OFF_HOUR, AIR_OFF_MINUTE, 0, airOff);
static AlarmID_t co2OnAlarm = Alarm.alarmRepeat(CO2_ON_HOUR, CO2_ON_MINUTE, 0, co2On);
static AlarmID_t co2OffAlarm = Alarm.alarmRepeat(CO2_OFF_HOUR, CO2_OFF_MINUTE, 0, co2Off);
// We have to re-enable the existing alarms every time we set the time for some reason.
Alarm.enable(lightsOnAlarm);
Alarm.enable(lightsOffAlarm);
Alarm.enable(airOnAlarm);
Alarm.enable(airOffAlarm);
Alarm.enable(co2OnAlarm);
Alarm.enable(co2OffAlarm);
lcd.setCursor(0, 1);
lcd.print("Alarms set");
Serial.println("Alarms set");
}
void readTimeFromSerial() {
//check for input to set the RTC, minimum length is 12, i.e. yy,m,d,h,m,s
if (Serial.available() >= 12) {
time_t t;
tmElements_t tm;
//note that the tmElements_t Year member is an offset from 1970,
//but the RTC wants the last two digits of the calendar year.
//use the convenience macros from Time.h to do the conversions.
int y = Serial.parseInt();
if (y >= 100 && y < 1000) {
Serial << F("Error: Year must be two digits or four digits!") << endl;
} else {
if (y >= 1000) {
tm.Year = CalendarYrToTm(y);
} else { //(y < 100)
tm.Year = y2kYearToTm(y);
}
tm.Month = Serial.parseInt();
tm.Day = Serial.parseInt();
tm.Hour = Serial.parseInt();
tm.Minute = Serial.parseInt();
tm.Second = Serial.parseInt();
t = makeTime(tm);
//use the time_t value to ensure correct weekday is set
if (RTC.set(t) == 0) { // Success
setTime(t);
Serial << F("RTC set to: ");
printDateTime(t);
Serial << endl;
if (timeStatus() == timeSet) {
Serial.println(F("Time successfully synchronised!"));
initialise();
} else {
Serial.println(F("Time not synchronised!"));
}
} else {
Serial.println(F("RTC set failed!"));
}
//dump any extraneous input
while (Serial.available() > 0) Serial.read();
}
}
}
//print date and time to Serial
void printDateTime(time_t t)
{
printDate(t);
Serial << ' ';
printTime(t);
}
//print time to Serial
void printTime(time_t t)
{
printI00(hour(t), ':');
printI00(minute(t), ':');
printI00(second(t), ' ');
}
//print date to Serial
void printDate(time_t t)
{
printI00(day(t), 0);
Serial << monthShortStr(month(t)) << _DEC(year(t));
}
//Print an integer in "00" format (with leading zero),
//followed by a delimiter character to Serial.
//Input value assumed to be between 0 and 99.
void printI00(int val, char delim)
{
if (val < 10) Serial << '0';
Serial << _DEC(val);
if (delim > 0) Serial << delim;
return;
}
void co2On() {
digitalWrite(CO2_PIN, HIGH);
CO2_STATE = "On ";
}
void co2Off() {
digitalWrite(CO2_PIN, LOW);
CO2_STATE = "Off";
}
void airOn() {
digitalWrite(AIR_PIN, HIGH);
AIR_STATE = "On ";
}
void airOff() {
digitalWrite(AIR_PIN, LOW);
AIR_STATE = "Off ";
}
void initialiseCo2() {
int currentHour = hour();
int currentMin = minute();
if ((currentHour > CO2_ON_HOUR || (currentHour == CO2_ON_HOUR && currentMin >= CO2_ON_MINUTE))
&& (currentHour < CO2_OFF_HOUR || (currentHour == CO2_OFF_HOUR && currentMin < CO2_OFF_MINUTE))
) {
co2On();
} else {
co2Off();
}
}
void initialiseAir() {
int currentHour = hour();
int currentMin = minute();
if ((currentHour > AIR_ON_HOUR || (currentHour == AIR_ON_HOUR && currentMin >= AIR_ON_MINUTE))
&& (currentHour < AIR_OFF_HOUR || (currentHour == AIR_OFF_HOUR && currentMin < AIR_OFF_MINUTE))
) {
airOn();
} else {
airOff();
}
}
void initialiseLights() {
int currentHour = hour();
int currentMin = minute();
if ((currentHour > LIGHTS_ON_HOUR || (currentHour == LIGHTS_ON_HOUR && currentMin >= LIGHTS_ON_MINUTE))
&& (currentHour < LIGHTS_OFF_HOUR || (currentHour == LIGHTS_OFF_HOUR && currentMin < LIGHTS_OFF_MINUTE))
) {
currentBrightness = 255;
lightsOn();
} else {
currentBrightness = 0;
lightsOff();
}
analogWrite(LIGHTS_PWM_PIN, currentBrightness);
}
void lightsOn() {
LIGHTS_ON = true;
Serial.println(F("Lights on"));
}
void lightsOff() {
LIGHTS_ON = false;
Serial.println(F("Lights off"));
}
void handleLights() {
static unsigned long since;
if (LIGHTS_ON) {
Serial.print(F("Lights: On. Brightness: "));
Serial.print(currentBrightness);
Serial.print('\n');
if (currentBrightness < 255) {
if (millisHavePassedSince(MILLIS_PER_INCREMENT, since)) {
currentBrightness++;
analogWrite(LIGHTS_PWM_PIN, currentBrightness);
}
}
} else {
Serial.print(F("Lights: Off. Brightness: "));
Serial.print(currentBrightness);
Serial.print('\n');
if (currentBrightness > 0) {
if (millisHavePassedSince(MILLIS_PER_INCREMENT, since)) {
currentBrightness--;
analogWrite(LIGHTS_PWM_PIN, currentBrightness);
}
}
}
}
// Prints the date & time to the display
void updateDisplay() {
// Clear the display every so often
static unsigned long since;
if (millisHavePassedSince(SCREEN_REFRESH_INTERVAL, since)) {
lcd.clear();
}
lcd.setCursor(0, 0);
lcd.print(F("Date: "));
printTwoDigits(day());
lcd.print(F("/"));
printTwoDigits(month());
lcd.print(F("/"));
lcd.print(year());
lcd.setCursor(0, 1);
lcd.print(F("Time: "));
printTwoDigits(hour());
lcd.print(F(":"));
printTwoDigits(minute());
lcd.print(F(":"));
printTwoDigits(second());
lcd.setCursor(0, 2);
lcd.print(F("CO2:"));
lcd.print(CO2_STATE);
lcd.setCursor(8, 2);
lcd.print(F("Air:"));
lcd.print(AIR_STATE);
lcd.setCursor(0, 3);
lcd.print(F("Lights: "));
if (currentBrightness == 255) {
lcd.print(F("On "));
} else if (currentBrightness == 0) {
lcd.print(F("Off"));
} else {
printBrightness(currentBrightness);
}
}
// Prefixes single digits with 0
void printTwoDigits(int number) {
if (number >= 0 && number < 10) {
lcd.print(F("0"));
}
lcd.print(number);
}
// Suffixes with spaces to clear trailing characters on the display.
void printBrightness(int brightness) {
lcd.print(brightness);
if (brightness >= 0 && brightness < 10) {
lcd.print(F(" "));
} else if (brightness < 100) {
lcd.print(F(" "));
}
}