-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathChauvet16.ino
312 lines (302 loc) · 7.26 KB
/
Chauvet16.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
//////////////////////////////////////////////////////
// CHAUVET16 Program for the CHAUVET16 CONTROLLER //
// Copyright (c) 2013 by Jerry Sy aka d0ughb0y //
// mail-to: j3rry5y@gmail.com //
// Commercial use of this software without //
// permission is absolutely prohibited. //
// make sure to edit config.h first before //
// uploading to the mega. //
//////////////////////////////////////////////////////
#include <OneWire.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Time.h>
#include <DS1307RTC.h>
#include <SdFat.h>
#include <SPI.h>
#include <Ethernet.h>
#include <Dns.h>
#include <Flash.h>
#include <stdlib.h>
#include "TinyWebServer.h"
#include "Sensor.h"
#include "config.h"
static uint32_t currentTime = 0;
static uint32_t previousTime = 0;
static uint16_t deltaTime = 0;
static uint16_t counter = 0;
SdFat sd;
SdFile file_;
volatile uint32_t debug1=0;
volatile uint32_t debug2=0;
LiquidCrystal_I2C lcd(LCD_ADDR,LCD_EN,LCD_RW,LCD_RS,LCD_D4,LCD_D5,LCD_D6,LCD_D7,LCD_BACKLIGHT,LCD_POLARITY);
#ifdef LCD_BACKLIGHT_OFF_DELAY_MINS
unsigned long lcdontime = 0;
boolean backlighton = true;
#endif
enum outlet {OUTLETDEFS, Feeder=20, Doser0=23, Doser1=24, Doser2=25, Doser3=26, Pump0=30, Pump1=31, Pump2=32, Pump3=33, PWMFan0=40, PWMFan1=41, PWMFan2=42, End=255};
volatile uint8_t _ActiveMacro = 4;
volatile time_t _MacroTime = 0;
volatile time_t _MacroCountDown = 0;
volatile conf_t conf;
volatile int tz = STDTZOFFSET;
volatile OutLogRec_t _outlog[OUTLOGSZ];
volatile uint8_t _head = 0;
volatile uint8_t _tail = 0;
volatile boolean alarm = false;
#ifdef AUTODST
volatile boolean testDst = false;
#endif
volatile boolean emailFlag = false;
volatile boolean logalarmFlag = false;
volatile boolean logSensorsFlag = false;
volatile boolean netCheckFlag = false;
volatile boolean updateRTCFlag = false;
volatile uint16_t sonaravg = 0;
volatile uint8_t displaymode = 0;
volatile uint8_t maxsensors = 0;
#ifdef _TEMP
TempSensorDef_t tempdata[] = TEMPDEF;
#endif
#ifdef _PH
Sensor* ph[MAXPH];
#if (MAXPH>=1)
#if defined(_PH1_SERIAL)
SensorSerial ph1 = SensorSerial(_PH1_NAME,Sensor::_ph,&_PH1_SERIAL,_PH1_EZO);
#endif
#if defined (_PH1_I2C)
SensorI2C ph1 = SensorI2C(_PH1_NAME,Sensor::_ph,_PH1_I2C);
#endif
#endif
#if (MAXPH==2)
#if defined(_PH2_SERIAL)
SensorSerial ph2 = SensorSerial(_PH2_NAME,Sensor::_ph,&_PH2_SERIAL,_PH2_EZO);
#endif
#if defined (_PH2_I2C)
SensorI2C ph2 = SensorI2C(_PH2_NAME,Sensor::_ph,_PH2_I2C);
#endif
#endif
#endif
#ifdef _ORP
#ifdef _ORP_SERIAL
SensorSerial orp = SensorSerial(_ORP_NAME,Sensor::_orp,&_ORP_SERIAL,_ORP_EZO);
#endif
#ifdef _ORP_I2C
SensorI2C orp = SensorI2C(_ORP_NAME,Sensor::_orp,_ORP_I2C);
#endif
#endif
#ifdef _COND
#ifdef _COND_SERIAL
SensorSerial cond = SensorSerial(_COND_NAME,Sensor::_cond,&_COND_SERIAL,true);
#endif
#ifdef _COND_I2C
SensorI2C cond = SensorI2C(_COND_NAME,Sensor::_cond,_COND_I2C);
#endif
#endif
#ifdef _DOSER
volatile boolean doseractive[MAXDOSERS];
volatile uint32_t dosercounter[MAXDOSERS];
volatile uint32_t dosercountmatch[MAXDOSERS];
volatile uint32_t dosedvolume[MAXDOSERS];
volatile boolean updateDoserStatusFlag = false;
volatile boolean dosercalibrating = false;
volatile uint16_t calibrationcount = 0;
#endif
void setup() {
#ifdef _PH
#if (MAXPH>=1)
ph[0]=&ph1;
#endif
#if (MAXPH==2)
ph[1]=&ph2;
#endif
#endif
initBuzzer();
initTimer();
beep();
initializeEEPROM();
#if defined(_FEEDER) || defined(_FEEDER_V2)
initFeeder();
#endif
#ifdef _PWMFAN
initPWMFan();
#endif
initATO();
#ifdef _SONAR
initSonar();
#endif
enablePCINT();
initUtils(); //init led, i2c pullup, lcd, SD
lightOn();
initNetwork(); //ethernet, clock, webserver, start logging here
initSensors(); //temp and ph
initOutlets();
#ifdef _DOSER
initDosers();
#endif
#ifdef _PWMA
initPWMPumps();
#endif
lightOff();
#ifdef _SONAR
for (int i=0;i<255;i++) {
updateSonar();
delay(2);
if (getSonar()>0) {
logMessage(F("ATO water level "),getSonarPct());
break;
}
}
#endif
startTimer();
#ifdef _PWMA
startPumps();
#endif
p(F("Boot Completed. "));
logMessage(F("Initialization Completed."));
logMessage(freeRam());
delay(1000);
beep2();
lcd.clear();
}
void loop() {
currentTime = millis();
deltaTime = currentTime - previousTime;
if (deltaTime > 16) { //60hz ~ every 16ms
counter++;
profile(true);
time_t timenow = now();
static time_t lasttimenow = 0;
if (timenow!=lasttimenow) {
ptime(timenow); //print time once a second
lasttimenow = timenow;
}
static uint8_t lastdisplaymode = 255;
if (displaymode!=lastdisplaymode) {
switch (displaymode) { //switch 2nd line every 5 secs
case 0: if (!psensors()) {
displaymode=255;
}
break;
case 1: poutlets();
break;
case 2: pinputs();
break;
#if defined(_PWMA)
case 3: ppwmpump(0);
break;
#endif
#if defined(_PWMB)
case 4: ppwmpump(2);
break;
#endif
#ifdef SHOWDEBUGONLCD
case LCD_NUM_MSGS-1: pdebug();
break;
#endif
}
lastdisplaymode = displaymode;
#if (LCD_BACKLIGHT_OFF_DELAY_MINS>0)
if (backlighton && (currentTime-lcdontime)>LCD_BACKLIGHT_OFF_DELAY_MINS*60000) {
backlighton=false;
lcd.setBacklight(LOW);
}
#endif
}
#ifdef AUTODST
if (testDst) {
autoDST(timenow);
testDst = false;
}
#endif
if (logSensorsFlag) { //every 10 minutes only
logSensorsFlag = false;
logSensors();
}
if (emailFlag) {
sendEmail();
emailFlag=false;
}
if (logalarmFlag) {
logAlarm();
logalarmFlag=false;
}
if (netCheckFlag) {
netCheck();
netCheckFlag=false;
}
if (updateRTCFlag) {
updateRTC();
updateRTCFlag=false;
}
#ifdef _DOSER
if (updateDoserStatusFlag) {
writeDoserStatus();
updateDoserStatusFlag=false;
}
#endif
#ifdef _SONAR
if (counter%15==0) {
updateSonar();
}
#endif
#ifdef _PWMFAN
if (counter%60==0) {
updatePWMFans();
}
#endif
#ifdef _PH
if (counter%(64/MAXPH)==0) {
static uint8_t phidx = 0;
ph[phidx++]->update();
phidx%=MAXPH;
}
#endif
#ifdef _ORP
if (counter%65==0) {
orp.update();
}
#endif
#ifdef _COND
if (counter%66==0) {
cond.update(
#ifdef _CONDTEMPCOMPENSATE
getTempRaw(0)
#endif
);
}
#endif
logOutlet();
previousTime = currentTime;
profile(false);
}
//do every cycle
#ifdef _TEMP
updateTemp(); //this takes 500us
#endif
webprocess();
}
void profile(boolean start) {
//debug1 and debug2 can be any number you want to display on lcd for debugging purposes
//here I display the current and average execution time in microseconds
//profile function is safe to call inside ISR
//just add profile(true) at the start of function
//and add profile(false) at end of function (or before every return)
static uint32_t time1;
if (start){
time1 = micros();
return;
}
uint8_t saveSREG=SREG;
cli();
debug2 = micros()-time1;
static uint32_t sum = 0;
if (sum) {
sum = (sum-debug1)+debug2;
} else {
if (debug2>0)
sum = debug2 * 60;
}
debug1=sum/60;
SREG=saveSREG;
}