-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathiAqua.ino
7562 lines (6721 loc) · 242 KB
/
iAqua.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
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// iAqua Aquarium Controller w/ iPhone-like Interface
// Written by Dan Cunningham, aka AnotherHobby @ plantedtank.net
// Much code was swiped, modified, and integrated or otherwise inspired from other public works
// All code is public domain, feel free to use, abuse, edit, and share
// Written for Arduino Mega 2560
// CURRENT VERSION 1.0.4
// - the smart startup routine has been reworked and fixed by robsworld78 at The Planted Tank forums!
// - added a first run routine that will zero out all EEPROM data that is needed by iAqua
// - see the warning immediately below:
// - ////////////////////////////////////////////////////////////////////////////////////////////
// - // WARNING!! IF YOU HAVE CONFIGURED iAqua BEFORE v 1.0.4 AND DO NOT WANT TO LOOSE YOUR DATA
// - // YOU MUST COMMENT OUT THE FISRT LINE OF THE startup() ROUTINE THAT CALLS firstRunSetup().
// - // IF YOU DO NOT, IT WILL ZERO ALL OF YOUR SETTINGS!!!!
// - /////////////////////////////////////////////////////////////////////////////////////////////
//
// VERSION 1.0.3
// - fixed smartstartup routine bugs
// - fixed dosing display when resevoir goes negative due to forgettig to hit fill
// - added robsworld78 at The Planted Tank's PWM smart startup lighting code
// - fixed issue when temp turned red, and then turned feeding time red
//
// VERSION: 1.0.2
// - more accurate math for how doses were calcualted on home screen
// - updated power schedules to update home screen immediately
// - day name was off using new RTC and Time library
//
// VERSION: 1.0.1
// - created smart startup routine
// - fixed math bug with dosing pump speed saving to eeprom
// - changed PWM pins for dosing pumps to make room for RGBW PWM pins
// - changed from previous RTC library to RTClib.h
//
// VERSION: 1.0
// - initial version
// CODE ORDER:
// variables & setup
// main loop
// routines to draw every screen
// code to capture all touches
// all other routines
// EEPROM locations (saved settings)
// SPACE // DESCRIPTION
// 0 // last feeding data saved (1 for yes)
// 1 // last feeding minute
// 2 // last feeding hour
// 3 // last feeding day
// 4 // last feeding month
// 5 // last feeding year
// 6 // feeding settings saved (1 for yes)
// 7 // feeding minutes setting
// 8 // feeding light 1 (0 off, 1 on)
// 9 // feeding pwr light 2 (0 off, 1 on)
// 10 // feeding pwr filter (0 off, 1 on)
// 11 // feeding pwr circ (0 off, 1 on)
// 12 // feeding pwr heat (0 off, 1 on)
// 13 // feeding pwr co2 (0 off, 1 on)
// 14 // feeding pwr aux 1 (0 off, 1 on)
// 15 // feeding pwr aux 2 (0 off, 1 on)
// 16 // heater settings saved (1 for yes)
// 17 // heater off temp
// 18 // heater on temp
// 19 // heater alarm temp
// 20 // dosing settings saved (1 for yes)
// 21 // dose in mL
// 22 // dosing reseviors capacity in mL
// 23 // pump 1 sec/ml
// 24 // pump 2 sec/ml
// 25 // dosing volume saved (1 for yes)
// 26 //
// 27 //
// 28 // screen: return home
// 29 // screen: autodim level
// 30 // screen: autodim seconds
// 31 // screen: brightness if no dim
// 32 // pump 1 remaining volume high byte ^10th
// 33 // pump 1 remaining volume low byte
// 34 // pump 2 remaining volume high byte ^10th
// 35 // pump 2 remaining volume low byte
// 100-129 // power scheudle
// 220-243 // light ramp schedule
// 300-317 // dosing scheudle
#include <Wire.h> // needed by tons of stuff
#include <EEPROM.h> // used to store and retrieve settings from memory
#include <UTFT.h> // used to interface with the TFT display
#include <UTouch.h> // used to interface with the touch controller on the TFT display
#include <tinyFAT.h> // used to acess the SD card
#include <UTFT_tinyFAT.h> // used to read .raw images from the SD card
//#include <DS1307new.h> // library to talk to the RTC chip
#include <RTClib.h>
#include <Time.h> // allows conversion to UNIX time for easier date/time math
#include <TimeAlarms.h> // used to power schedules
#include <IRremote.h> // used to send IR commands to the light, LED must be on pin 9
#include <OneWire.h> // network library to communicate with the DallasTemperature sensor,
#include <DallasTemperature.h> // library for the Temp sensor itself
// libraries I was using at one time, but not any more. Leaving them here in case I need them again
//#include<stdlib.h>
//#include <avr/pgmspace.h>
// Declare which fonts we will be using
extern uint8_t Sinclair_S[];
extern uint8_t arial_bold[];
// define relay pins
// all analog to save on digital pins
int pwrLight1Pin = A0;
int pwrLight2Pin = A1;
int pwrFilterPin = A2;
int pwrCircPin = A3;
int pwrHeatPin = A4;
int pwrCO2Pin = A5;
int pwrAux2Pin = A6;
int pwrAux1Pin = A7;
int alarmPin = 2; //(was 2)
int screenBrightPin = 8; // pwm pin for the LCD backlight (was 4)
int lightSensorPin = A8; // analog pin for the ambient light sensor
int pressureSensorPin = A9; // analog pin for the CO2 pressure sensor
byte backLight = 255; // startup brightness to 100%
boolean backlightTouch = true; // initial setting of true to allow the screen to stay bright after boot
//define pump pins
int dosingPump1 = 10;
int dosingPump2 = 11;
// screen settings corresponding to eeprom values 28-31
byte screenRetHome, screenDimLevel, screenDimSec, screenBrightMem;
// Pins for temperature sensor
#define ONE_WIRE_BUS_W 47 //water sensor on pin 47
// for time
RTC_DS1307 RTC;
// for time calcuation, we need to know the current time zone offset
//int UTC_Offset=-5;
// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWireW(ONE_WIRE_BUS_W);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensorW(&oneWireW); //water sensor
UTFT myGLCD(SSD1289,38,39,40,41); // start up an instance of the TFT screen
UTouch myTouch(46,45,44,43,42); // start up an instance of for touch
//UTouch myTouch(6, 5, 4, 3, 2);
UTFT_tinyFAT myFiles(&myGLCD); // start up an instance to read images from the SD card
// create an IR instance
IRsend irsend;
byte dispScreen=1; // screens are listed below
// 1-home, 2-feeding, , 3-power, 4-extras, 5-lights
// 6-clock, 7-feeding sched, 8-schedule, 9-heater
// 10-dosing, 11-pwer schedule, 12-schedule item
byte scheduleItem; // track which item is being scheduled
boolean updateTime=true; // keep track of when to udpate the clock
byte currentLightMode=0; //0=high sun, 1=mid sun, 2=low sun, 3=moon, 4=transition, 5=unknown
byte lightEditing=0; // track if we are actively editing lights
// the next 5 variables will be set at the start of a lighting fade
int fadeDurationSeconds = 0;
unsigned long fadeStartingSeconds = 0;
unsigned long fadeTimeLeft;
boolean fadeInProgress = false;
byte fadeFromMode = 0; //0=high sun, 1=mid sun, 2=low sun, 3=moon
byte fadeToMode = 0; //0=high sun, 1=mid sun, 2=low sun, 3=moon
struct RGBW // for storing light intensity values
{
int Red;
int Green;
int Blue;
int White;
};
typedef struct RGBW LightColor;
LightColor currentColor = {
0,0,0,0}; // The current color of the light (used for fading)
LightColor lastColor = {
0,0,0,0}; // The previous color of the light (used for fading)
LightColor targetColor = {
0,0,0,0}; // The target color of the light (used for fading)
LightColor lightHighSun = {
0,0,0,0}; // store the RGBW values for the CS+ M1 button
LightColor lightMidSun = {
0,0,0,0}; // store the RGBW values for the CS+ M2 button
LightColor lightLowSun = {
0,0,0,0}; // store the RGBW values for the CS+ M3 button
LightColor lightMoon = {
0,0,0,0}; // store the RGBW values for the CS+ M4 button
// selected lights mode buttons for lights screen
char *lightModeS[] = {
"5hsunS.raw","5msunS.raw","5lsunS.raw","5moonS.raw"};
// off lights mode buttons for lights screen
char *lightModeF[] = {
"5hmsunF.raw","5lsunF.raw","5moonF.raw"};
// neutral lights mode buttons for lights screen
char *lightMode[] = {
"5hsun.raw","5msun.raw","5lsun.raw","5moon.raw"};
// lights adjustment buttons for lights screen (RGBW up and down)
char *lightWhite[] = {
"5Wup.raw", "5Wdown.raw"};
char *lightRed[] = {
"5Rup.raw", "5Rdown.raw"};
char *lightGreen[] = {
"5Gup.raw", "5Gdown.raw"};
char *lightBlue[] = {
"5Bup.raw", "5Bdown.raw"};
char *lightGray[] = {
"5Fup.raw", "5Fdown.raw"}; // disabled button
// editing buttons for the lights screen, disabled and enabled
char *lightEdit[] = {
"5editF.raw", "5editN.raw"};
char *lightSave[] = {
"5saveF.raw", "5saveN.raw"};
char *lightResync[] = {
"5resynF.raw", "5resynN.raw"};
char *lightCancel[] = {
"5canF.raw", "5canN.raw"};
// large power buttons for the power screen and the feeding configuration screen, off and on
char *pwrLightIcon[] = {
"3light_F.raw","3light_N.raw"};
char *pwrFilterIcon[] = {
"3filt_F.raw","3filt_N.raw"};
char *pwrCircIcon[] = {
"3circ_F.raw","3circ_N.raw"};
char *pwrHeatIcon[] = {
"3heat_F.raw","3heat_N.raw"};
char *pwrCO2Icon[] = {
"3co2_F.raw","3co2_N.raw"};
char *pwrAux1Icon[] = {
"3aux1_F.raw","3aux1_N.raw"};
char *pwrAux2Icon[] = {
"3aux2_F.raw","3aux2_N.raw"};
// on off power dot under each power button on the power screen and feeding config screen
char *pwrDot[] = {
"3dotR.raw","3dotG.raw"};
// small power icons for the home screen, off and on
char *pwrLightIconS[] = {
"1lightF.raw","1lightN.raw"};
char *pwrFilterIconS[] = {
"1filtF.raw","1filtN.raw"};
char *pwrCircIconS[] = {
"1circF.raw","1circN.raw"};
char *pwrHeatIconS[] = {
"1heatF.raw","1heatN.raw"};
char *pwrCO2IconS[] = {
"1co2F.raw","1co2N.raw"};
char *pwrAux1IconS[] = {
"1aux1F.raw","1aux1N.raw"};
char *pwrAux2IconS[] = {
"1aux2F.raw","1aux2N.raw"};
// small light mode icons for home screen
char *lightModeSm[] = {
"1hsun.raw","1msun.raw","1lsun.raw","1moon.raw"};
// 24 pixel up and down arrow buttons used on several screens
char *arrowButton[] = {
"24whUp.raw", "24whDn.raw"};
// enabled or not enabled small check boxes for the power schedule screen
char *schedActive[] = {
"11dis.raw","11enab.raw"};
// enabled or not enabled large check boxes for the power item schedule screen
char *schedActiveB[] = {
"11disB.raw","11enabB.raw"};
// days and month character strings for displaing at the top of the screen
char *Day[] = {
"","Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
char *Mon[] = {
"","Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
// used for time
struct RTC_T
{
int tHour;
int tMinute;
int tSecond;
int tDow;
int tDay;
int tMonth;
int tYear;
}
tmpRTC, prevRTC, saveRTC;
// used for storing power states of relays
struct PWR
{
byte pwrLight1;
byte pwrLight2;
byte pwrFilter;
byte pwrCirc;
byte pwrHeat;
byte pwrCO2;
byte pwrAux1;
byte pwrAux2;
}
feedPower, preFeedPower, globalPower;
// holds the schedule for power relays and light ramping
struct PWRSCHED
{
byte active;
byte onHour;
byte onMinute;
byte offHour;
byte offMinute;
}
schedLights1, schedLights2, schedCirc, schedCo2, schedAux1, schedAux2, ramp1, ramp2, ramp3, ramp4, ramp5, ramp6;
// holds the schedulin for 2 dosing pumps
struct PUMPSCHED
{
byte onHour;
byte onMinute;
byte Sunday;
byte Monday;
byte Tuesday;
byte Wednesday;
byte Thursday;
byte Friday;
byte Saturday;
}
pump1, pump2;
int x, y; //touch coordinates
boolean feedingActive=false; // track if feeding is currently active
byte feedingMins=0; // stores how long the feeding should be
boolean heaterWarning=false; // keeps track if there is an active overheating issue
boolean heaterWarningCleared=true; // keeps track if we clear the warning, impacts the icon on the home screen
byte heatOffTemp, heatOnTemp, coldWarnTemp;
// various millis to keep track of
unsigned long prevMillisTouch = 0; // track time between touches
unsigned long prevMillis5sec = 0; // track 5 seconds for refreshing clock and temp
unsigned long feedingMillis = 0; // track how long we've been feeding
unsigned long millisDim = 0; // used for brightness adjustment
unsigned long millisHome = 0; // used for returning home after configured time
float tempC = 0; // water temperature
char tempstring[7]; // water temperature as a string
DeviceAddress waterSensor;
int freeRam ()
{
// Returns available SRAM
extern int __heap_start, *__brkval;
int v;
return (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval);
}
// if you have a Current Satellite Plus, this is true
// if you are controlling your lights directly with PWM, this is false
boolean lightCSP = true;
int maxRGBW = 100;
// define RGBW PWM pins
int ledRedPin = 6;
int ledBluePin = 5;
int ledGreenPin = 4;
int ledWhitePin = 3;
// REMOTE CONTROL CODES FOR CURRENT SATELLITE PLUS
const unsigned long POWER = 0x20DF02FD;
const unsigned long FULLORANGE = 0x20DF3AC5;
const unsigned long FULLLIGHTBLUE = 0x20DFBA45;
const unsigned long FULLPURPLE = 0x20DF827D;
const unsigned long FULLWHITE = 0x20DF1AE5;
const unsigned long FULLYELLOW = 0x20DF9A65;
const unsigned long FULLBLUE = 0x20DFA25D;
const unsigned long REDUP = 0x20DF2AD5;
const unsigned long REDDOWN = 0x20DF0AF5;
const unsigned long GREENUP = 0x20DFAA55;
const unsigned long GREENDOWN = 0x20DF8A75;
const unsigned long BLUEUP = 0x20DF926D;
const unsigned long BLUEDOWN = 0x20DFB24D;
const unsigned long WHITEUP = 0x20DF12ED;
const unsigned long WHITEDOWN = 0x20DF32CD;
const unsigned long M1 = 0x20DF38C7;
const unsigned long M2 = 0x20DFB847;
const unsigned long M3 = 0x20DF7887;
const unsigned long M4 = 0x20DFF807;
const unsigned long MOONLIGHT = 0x20DF18E7;
const unsigned long MOONDARK = 0x20DF9867;
const unsigned long MOONCLOUDS = 0x20DF58A7;
const unsigned long SUNRISE = 0x20DFD827;
const unsigned long CLOUDS1 = 0x20DF28D7;
const unsigned long CLOUDS2 = 0x20DFA857;
const unsigned long CLOUDS3 = 0x20DF6897;
const unsigned long CLOUDS4 = 0x20DFE817;
const unsigned long CLOUDS5 = 0x20DFC837;
const unsigned long STORM1 = 0x20DF08F7;
const unsigned long STORM2 = 0x20DF8877;
const unsigned long STORM3 = 0x20DF48B7;
/*
// REMOTE CONTROL CODES FOR ECOXOTIC E-SERIES
const unsigned long POWER = 0x20DF02FD;
const unsigned long FULLORANGE = 0x20DF3AC5; // NOT UPDATED YET
const unsigned long FULLLIGHTBLUE = 0x20DFBA45; // NOT UPDATED YET
const unsigned long FULLPURPLE = 0x20DF827D; // NOT UPDATED YET
const unsigned long FULLWHITE = 0x20DF1AE5; // NOT UPDATED YET
const unsigned long FULLYELLOW = 0x20DF9A65; // NOT UPDATED YET
const unsigned long FULLBLUE = 0x20DFA25D; // NOT UPDATED YET
const unsigned long REDUP = 0x20DF0AF5;
const unsigned long REDDOWN = 0x20DF38C7;
const unsigned long GREENUP = 0x20DF8A75;
const unsigned long GREENDOWN = 0x20DFB847;
const unsigned long BLUEUP = 0x20DFB24D;
const unsigned long BLUEDOWN = 0x20DF7887;
const unsigned long WHITEUP = 0x20DF32CD;
const unsigned long WHITEDOWN = 0x20DFF807;
const unsigned long M1 = 0x20DF58A7;
const unsigned long M2 = 0x20DF9867;
const unsigned long M3 = 0x20DF18E7;
const unsigned long M4 = 0x20DFD827;
const unsigned long MOONLIGHT = 0x20DF18E7; // NOT UPDATED YET
const unsigned long MOONDARK = 0x20DF9867; // NOT UPDATED YET
const unsigned long MOONCLOUDS = 0x20DF58A7; // NOT UPDATED YET
const unsigned long SUNRISE = 0x20DFD827; // NOT UPDATED YET
const unsigned long CLOUDS1 = 0x20DF28D7; // NOT UPDATED YET
const unsigned long CLOUDS2 = 0x20DFA857; // NOT UPDATED YET
const unsigned long CLOUDS3 = 0x20DF6897; // NOT UPDATED YET
const unsigned long CLOUDS4 = 0x20DFE817; // NOT UPDATED YET
const unsigned long CLOUDS5 = 0x20DFC837; // NOT UPDATED YET
const unsigned long STORM1 = 0x20DF08F7; // NOT UPDATED YET
const unsigned long STORM2 = 0x20DF8877; // NOT UPDATED YET
const unsigned long STORM3 = 0x20DF48B7; // NOT UPDATED YET
*/
/////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////
// END GLOBAL VARIABLES //
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
void setup()
{
// set default values for the first ever run, you can comment this out after the first run if you want
firstRunSetup();
// initiate the screen and touch
myGLCD.InitLCD(PORTRAIT);
myTouch.InitTouch(PORTRAIT);
myTouch.setPrecision(PREC_MEDIUM);
// init SD card
file.setSSpin(53);
file.initFAT(SPISPEED_VERYHIGH);
Wire.begin();
RTC.begin();
delay(250);
Serial.begin(9600);
if (! RTC.isrunning())
{
// If no RTC is installed, alert on serial
Serial.println("RTC is NOT running!\n"); // Store this string in PROGMEM
}
else Serial.println("RTC is running!\n"); // Store this string in PROGMEM
//RTC.getTime();
setSyncProvider(syncProvider); // reference our syncProvider function instead of RTC_DS1307::get()
// set all pin modes for output and inputs
pinMode(pwrLight1Pin, OUTPUT);
pinMode(pwrLight2Pin, OUTPUT);
pinMode(pwrFilterPin, OUTPUT);
pinMode(pwrCircPin, OUTPUT);
pinMode(pwrHeatPin, OUTPUT);
pinMode(pwrCO2Pin, OUTPUT);
pinMode(pwrAux2Pin, OUTPUT);
pinMode(pwrAux1Pin, OUTPUT);
pinMode(screenBrightPin, OUTPUT);
pinMode(lightSensorPin, INPUT);
pinMode(pressureSensorPin, INPUT);
pinMode(alarmPin, OUTPUT);
pinMode(dosingPump1, OUTPUT);
pinMode(dosingPump2, OUTPUT);
// used for PWM lighting control
pinMode(ledRedPin, OUTPUT);
pinMode(ledBluePin, OUTPUT);
pinMode(ledGreenPin, OUTPUT);
pinMode(ledWhitePin, OUTPUT);
// PWM lights can go up to 255, so we up this value if we are using PWM
if (lightCSP==false) int maxRGBW = 255;
sensorW.begin(); //start up temperature library
sensorW.getAddress(waterSensor, 0); //get addresses of temperature sensor
// get screen settings from eeprom
screenRetHome=EEPROM.read(28);
screenDimLevel=EEPROM.read(29);
screenDimSec=EEPROM.read(30);
screenBrightMem=EEPROM.read(31);
// get heat warning temps
heatOffTemp=EEPROM.read(17);
heatOnTemp=EEPROM.read(18);
coldWarnTemp=EEPROM.read(19);
analogWrite(screenBrightPin, screenBrightMem); // turn up screen brightness right away
// get feeding settings from EEPROM
feedingMins=EEPROM.read(7);
feedPower.pwrLight1=EEPROM.read(8);
feedPower.pwrLight2=EEPROM.read(9);
feedPower.pwrFilter=EEPROM.read(10);
feedPower.pwrCirc=EEPROM.read(11);
feedPower.pwrHeat=EEPROM.read(12);
feedPower.pwrCO2=EEPROM.read(13);
feedPower.pwrAux1=EEPROM.read(14);
feedPower.pwrAux2=EEPROM.read(15);
/*
// turn on/off power relays, you can just change between On and Off to change these
AlarmPwrLight1_On();
AlarmPwrLight2_On();
AlarmPwrFilter_On();
AlarmPwrCirc_On();
AlarmPwrHeat_On();
AlarmPwrCO2_On();
AlarmPwrAux1_Off();
AlarmPwrAux2_Off();
*/
// read in power schedule
readPowerSchedule();
// read in light ramping schedule
readRampSchedule();
// read in the dosing schedule
readDosingSchedule();
// synchronize the lights and read in the saved settings
resyncLights();
// clear the screen
myGLCD.clrScr();
// boot up logo
myFiles.loadBitmap(26, 80, 188, 72, "iAqua.raw");
myFiles.loadBitmap(53, 188, 134, 25, "Copy.raw");
delay(1000);
// create all alarams
updateAlarms();
myGLCD.clrScr();
updateTimeDate();
// display home screen
screenHome();
// start up all power and set lighting mode according to schedudule
smartStartup();
// Print available SRAM for debugging, comment out if you want
//Serial.print("\nReady...\n");
Serial.print("SRAM: ");
Serial.print(freeRam());
Serial.print("\n");
millisDim=millis(); // update millis to keep screen bright
Serial.print("millisDim: ");
Serial.print(millisDim);
Serial.print("\n");
/*
Serial.print("lightHighSun RGBW: ");
Serial.print(lightHighSun.Red);
Serial.print(", ");
Serial.print(lightHighSun.Green);
Serial.print(", ");
Serial.print(lightHighSun.Blue);
Serial.print(", ");
Serial.print(lightHighSun.White);
Serial.print("\n");
Serial.print("lightMidSun RGBW: ");
Serial.print(lightMidSun.Red);
Serial.print(", ");
Serial.print(lightMidSun.Green);
Serial.print(", ");
Serial.print(lightMidSun.Blue);
Serial.print(", ");
Serial.print(lightMidSun.White);
Serial.print("\n");
Serial.print("lightLowSun RGBW: ");
Serial.print(lightLowSun.Red);
Serial.print(", ");
Serial.print(lightLowSun.Green);
Serial.print(", ");
Serial.print(lightLowSun.Blue);
Serial.print(", ");
Serial.print(lightLowSun.White);
Serial.print("\n");
Serial.print("lightMoon RGBW: ");
Serial.print(lightMoon.Red);
Serial.print(", ");
Serial.print(lightMoon.Green);
Serial.print(", ");
Serial.print(lightMoon.Blue);
Serial.print(", ");
Serial.print(lightMoon.White);
Serial.print("\n");
*/
}
time_t syncProvider()
{
//this does the same thing as RTC_DS1307::get()
return RTC.now().unixtime();
}
void loop()
{
unsigned long currentMillis = millis(); // get current millis
// if the feeding cycle is active, we keep an eye on it and stop it when the configured minutes have passed
if (feedingActive==true)
{
millisDim=millis(); // keep the screen bright duing feeding
unsigned long feedingTotalMillis = feedingMins; // calculate duration based on saved config
feedingTotalMillis=(feedingTotalMillis*1000*60);
if ((currentMillis-feedingMillis)>feedingTotalMillis) // keep looping until feeding is done
{
feedingStop();
}
}
// check for touch events
if (myTouch.dataAvailable()) {
if (currentMillis - prevMillisTouch > 500) // make sure it's been .5 sec between touches
{
// set backlight touch if not already set and turn up brightness
if (backlightTouch==false) // backlight touch is for adjusting brightness after touch
{
backLight=255;
analogWrite(screenBrightPin, backLight);
backlightTouch=true;
}
prevMillisTouch=currentMillis; // reset the touch timer
millisDim=currentMillis; // reset screen dim timer
millisHome=currentMillis; // reset return home timer
Serial.print("Updating millisHome to: ");
Serial.print(millisHome);
Serial.print("\n");
processMyTouch();
}
}
// update time, temp, and feeding every 5 seconds
if (currentMillis - prevMillis5sec > 5000)
{
prevMillis5sec=millis();
updateTimeDate();
if (dispScreen==1) checkTemp();
if ((dispScreen==1)||(dispScreen==2)) checkFeeding();
}
// this is only used for testing, so you can enter codes in the serial window
// and get output during code testing
if (Serial.available() > 0) {
delay(5); //Wait for transmission to finish
TestCodes(SerialReadInt()); // Go serial input code and process
}
// adjust brightness automatically unless touch event
if (screenDimSec!=0) // if set to 0, we won't dim
{
if (screenDimLevel!=0) // if set to 0, we won't dim
{
if (backlightTouch==true)
{
unsigned long pastMillis=(currentMillis - millisDim);
if (pastMillis > (1000*screenDimSec)) backlightTouch=false;
}
else
{
autoBrightness();
}
}
}
// if we aren't on the home or feeding screen, we return after preset time of no interaction
if (screenRetHome != 0) // if set to 0, we don't return home
{
if ((dispScreen!=1)&&(dispScreen!=2))
{
unsigned long pastMillis=(currentMillis - millisHome);
if (pastMillis > (60000*screenRetHome))
{
if (dispScreen==9) updateAlarms(); // this will rebuild all of the schedules
screenHome();
}
}
}
// Get the time in seconds (since 1970)
unsigned long rightNow = now();
// check on the fading of the lights
if (fadeInProgress==true)
{
if(rightNow > fadeStartingSeconds + fadeDurationSeconds) // if we have just finished the fade...
{
fadeInProgress = false;
currentLightMode=fadeToMode; // If a color fade has been completed, set the final mode
if (lightCSP==true) // for the CSP, we send a hard code to lock in the setting and avoid drift
{
// send IR command for correct lighting mode
if (currentLightMode == 0) irsend.sendNEC(M1,32);
else if (currentLightMode == 1) irsend.sendNEC(M2,32);
else if (currentLightMode == 2) irsend.sendNEC(M3,32);
else if (currentLightMode == 3) irsend.sendNEC(M4,32);
}
if (dispScreen==1) screenHome(); // redraw the home screen if we are home
}
else // if the fade is still running
{
// If there is a fade in progress, check if there are any IR commands that should be sent
checkLightFade(rightNow - fadeStartingSeconds, fadeDurationSeconds);
}
}
Alarm.delay(5); // must use Alarm delay to use the TimeAlarms library
}
void screenHome() // draw main home screen showing overview info
{
if (dispScreen!=1) myGLCD.clrScr(); // clear if not home
dispScreen=1; // set screen so we can know what screen was touched later
myGLCD.setColor(0,0,0);
myGLCD.fillRect(0,32,239,254); // clear the screen between the header and the dock
// draw dock, home icon, and header
myFiles.loadBitmap(0, 254, 240, 66, "dock.raw");
myFiles.loadBitmap(2, 2, 30, 30, "1home.raw");
myGLCD.setFont(arial_bold);
myGLCD.setColor(34, 81, 255);
myGLCD.print("HOME ", 36, 12);
// draw lines to divide screen
myGLCD.setColor(130, 130, 130);
myGLCD.drawLine(40, 31, 239, 31); // under header
myGLCD.drawLine(0, 110, 239, 110); // across screen below temp
myGLCD.drawLine(56, 110, 56, 237); // 1st cutting into 4ths
myGLCD.drawLine(105, 110, 105, 237); // 2nd cutting into 4ths
myGLCD.drawLine(168, 110, 168, 237); // 3rd cutting into 4ths
myGLCD.drawLine(0, 237, 239, 237); // across above dock
// draw water temp logo
if (heaterWarningCleared==true) // if no warning is active, or has been acknowledged
{
myFiles.loadBitmap(50, 44, 60, 51, "1therm.raw");
}
else if (heaterWarningCleared==false) // if warning hasn't been acknowledged
{
myFiles.loadBitmap(50, 44, 60, 51, "1thermR.raw");
}
// check temp and draw to screen
checkTemp();
// load °F image
myFiles.loadBitmap(202, 66, 14, 12, "f.raw");
// display feeding info
checkFeeding();
myFiles.loadBitmap(5, 172, 46, 46, "1feed.raw");
// display lighting info
checkLighting();
// get remainding doses
checkDosing();
// draw power status of outputs
myFiles.loadBitmap(178, 121, 24, 24, pwrLightIconS[globalPower.pwrLight1]);
myFiles.loadBitmap(206, 121, 24, 24, pwrLightIconS[globalPower.pwrLight2]);
myFiles.loadBitmap(178, 149, 24, 24, pwrFilterIconS[globalPower.pwrFilter]);
myFiles.loadBitmap(206, 149, 24, 24, pwrCircIconS[globalPower.pwrCirc]);
myFiles.loadBitmap(178, 177, 24, 24, pwrHeatIconS[globalPower.pwrHeat]);
myFiles.loadBitmap(206, 177, 24, 24, pwrCO2IconS[globalPower.pwrCO2]);
myFiles.loadBitmap(178, 205, 24, 24, pwrAux1IconS[globalPower.pwrAux1]);
myFiles.loadBitmap(206, 205, 24, 24, pwrAux2IconS[globalPower.pwrAux2]);
}
void screenFeeding() // start the feeding cycle and draw the feeding screen
{
myGLCD.clrScr();
dispScreen=2;
updateTimeDate();
// draw header and footer
myGLCD.setColor(130, 130, 130);
myGLCD.drawLine(40, 31, 239, 31); // under header
myGLCD.drawLine(0, 307, 239, 307); // at footer
myFiles.loadBitmap(2, 2, 30, 30, "2feed.raw");
myGLCD.setFont(arial_bold);
myGLCD.setColor(0, 184, 19);
myGLCD.print("FEEDING ", 36, 12);
myFiles.loadBitmap(107, 294, 26, 26, "foothome.raw");
myGLCD.setFont(arial_bold);
myGLCD.setColor(0, 184, 19);
myGLCD.print("TIME REMAINING", CENTER, 60);
myGLCD.setColor(240, 240, 255);
unsigned long nowMillis = millis();
unsigned long feedingTotalMillis = feedingMins;
feedingTotalMillis=(feedingTotalMillis*1000*60);
int feedingMinsLeft=((feedingTotalMillis-(nowMillis-feedingMillis))/1000)/60;
char char3[3];
itoa(feedingMinsLeft, char3, 10);
myGLCD.print(char3, 40, 80);
myGLCD.print("MINUTES", 88, 80);
// buttons to stop and restart feeding
myFiles.loadBitmap(67, 223, 48, 48, "2stop.raw");
myFiles.loadBitmap(124, 223, 48, 48, "2restart.raw");
// picture of fish eating
myFiles.loadBitmap(74, 110, 92, 92, "2feeding.raw");
// start feeding cycle if not already active
if (feedingActive==false)
{
// capture current power status so we can return to it
preFeedPower.pwrLight1=globalPower.pwrLight1;
preFeedPower.pwrLight2=globalPower.pwrLight2;
preFeedPower.pwrFilter=globalPower.pwrFilter;
preFeedPower.pwrCirc=globalPower.pwrCirc;
preFeedPower.pwrHeat=globalPower.pwrHeat;
// there is a catch to make sure CO2 isn't turned back on if the schedule turns it off during feeding
preFeedPower.pwrCO2=globalPower.pwrCO2;
preFeedPower.pwrAux1=globalPower.pwrAux1;
preFeedPower.pwrAux2=globalPower.pwrAux2;
// fire power relays as configured
if (feedPower.pwrLight1==1) AlarmPwrLight1_On();
else if (feedPower.pwrLight1==0) AlarmPwrLight1_Off();
if (feedPower.pwrLight2==1) AlarmPwrLight2_On();
else if (feedPower.pwrLight2==0) AlarmPwrLight2_Off();
if (feedPower.pwrFilter==1) AlarmPwrFilter_On();
else if (feedPower.pwrFilter==0) AlarmPwrFilter_Off();
if (feedPower.pwrCirc==1) AlarmPwrCirc_On();
else if (feedPower.pwrCirc==0) AlarmPwrCirc_Off();
if (feedPower.pwrHeat==1) AlarmPwrHeat_On();
else if (feedPower.pwrHeat==0) AlarmPwrHeat_Off();
if (feedPower.pwrCO2==1) AlarmPwrCO2_On();
else if (feedPower.pwrCO2==0) AlarmPwrCO2_Off();
if (feedPower.pwrAux1==1) AlarmPwrAux1_On();
else if (feedPower.pwrAux1==0) AlarmPwrAux1_Off();
if (feedPower.pwrAux2==1) AlarmPwrAux2_On();
else if (feedPower.pwrAux2==1) AlarmPwrAux2_Off();
feedingActive=true;
feedingMillis = millis(); // mark when feeding started
// save feeding time to EEPROM
//RTC.getTime();
RTC.now().unixtime();
EEPROM.write(0, 1); // 0 // last feeding data saved (0 for no, 1 for yes)
//EEPROM.write(1, RTC.minute); // 1 // last feeding minute
//EEPROM.write(2, RTC.hour); // 2 // last feeding hour
//EEPROM.write(3, RTC.day); // 3 // last feeding day
//EEPROM.write(4, RTC.month); // 4 // last feeding month
//EEPROM.write(5, (RTC.year-2000)); // 5 // last feeding year
EEPROM.write(1, minute()); // 1 // last feeding minute
EEPROM.write(2, hour()); // 2 // last feeding hour
EEPROM.write(3, day()); // 3 // last feeding day
EEPROM.write(4, month()); // 4 // last feeding month
EEPROM.write(5, (year()-2000)); // 5 // last feeding year
}
}
void screenPower() // draw the screen to turn power outputs on/off
{
dispScreen=3;
myGLCD.clrScr();
// draw footer
myGLCD.setColor(130, 130, 130);
myGLCD.drawLine(40, 31, 239, 31); // under header
myGLCD.drawLine(0, 307, 104, 307); // left footer
myGLCD.drawLine(136, 307, 239, 307); // right footer
myFiles.loadBitmap(107, 294, 26, 26, "foothome.raw");
updateTimeDate();
// draw header
myFiles.loadBitmap(2, 2, 30, 30, "3power.raw");
myGLCD.setFont(arial_bold);
myGLCD.setColor(222, 8, 51);
myGLCD.print("POWER ", 36, 12);
myGLCD.setColor(255, 255, 255);
myGLCD.print("MASTER", CENTER, 52);
// all on and all off buttons
myFiles.loadBitmap(73, 77, 40, 40, "3allon.raw");
myFiles.loadBitmap(128, 77, 40, 40, "3alloff.raw");
// load all power icons and power dots
myFiles.loadBitmap(15, 139, 48, 48, pwrLightIcon[globalPower.pwrLight1]);
myFiles.loadBitmap(34, 192, 10, 11, pwrDot[globalPower.pwrLight1]);
myFiles.loadBitmap(69, 139, 48, 48, pwrLightIcon[globalPower.pwrLight2]);
myFiles.loadBitmap(88, 192, 10, 11, pwrDot[globalPower.pwrLight2]);
myFiles.loadBitmap(124, 139, 48, 48, pwrFilterIcon[globalPower.pwrFilter]);
myFiles.loadBitmap(143, 192, 10, 11, pwrDot[globalPower.pwrFilter]);
myFiles.loadBitmap(178, 139, 48, 48, pwrCircIcon[globalPower.pwrCirc] );
myFiles.loadBitmap(197, 192, 10, 11, pwrDot[globalPower.pwrCirc]);
myFiles.loadBitmap(15, 212, 48, 48, pwrHeatIcon[globalPower.pwrHeat] );
myFiles.loadBitmap(34, 264, 10, 11, pwrDot[globalPower.pwrHeat]);
myFiles.loadBitmap(69, 212, 48, 48, pwrCO2Icon[globalPower.pwrCO2] );
myFiles.loadBitmap(88, 264, 10, 11, pwrDot[globalPower.pwrCO2]);
myFiles.loadBitmap(124, 212, 48, 48, pwrAux1Icon[globalPower.pwrAux1] );
myFiles.loadBitmap(143, 264, 10, 11, pwrDot[globalPower.pwrAux1]);
myFiles.loadBitmap(178, 212, 48, 48, pwrAux2Icon[globalPower.pwrAux2] );
myFiles.loadBitmap(197, 264, 10, 11, pwrDot[globalPower.pwrAux2]);
}
void screenSettings() // draw the screen that has all of the extra settings apps
{
dispScreen=4;
myGLCD.clrScr();
// draw header
myFiles.loadBitmap(2, 2, 30, 30, "4extras.raw");
myGLCD.setFont(arial_bold);
myGLCD.setColor(255, 77, 0);
myGLCD.print("SETTINGS ", 36, 12);
myGLCD.setColor(130, 130, 130);
myGLCD.drawLine(40, 31, 239, 31); // under header
myGLCD.drawLine(0, 307, 104, 307); // left footer
myGLCD.drawLine(136, 307, 239, 307); // right footer
myFiles.loadBitmap(107, 294, 26, 26, "foothome.raw"); // footer home button