-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.ino
More file actions
300 lines (262 loc) · 8.88 KB
/
setup.ino
File metadata and controls
300 lines (262 loc) · 8.88 KB
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
void setup()
{
Serial.begin(2000000); //set baud rate for the hardware serial port_0 to 9600
Serial1.begin(9600);
wdt_disable();
// Starting Real Time CLock and Setting time
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
if (! rtc.initialized()) {
Serial.println("RTC is NOT running!");
rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); // set the RTC to the date & time this sketch was compiled
}
// Store MAC address in EEPROM
if (EEPROM.read(44) == '#') {
for (int i = 3; i < 6; i++) {
mac[i] = EEPROM.read(i + 44);
}
Serial.println(F("MAC Address found in EEPROM and read"));
}
else {
Serial.println(F("No MAC Address Found in EEPROM. Generating New MAC."));
for (int i = 3; i < 6; i++) {
mac[i] = TrueRandom.randomByte();
EEPROM.write(i + 44, mac[i]);
}
EEPROM.write(44, '#');
}
snprintf(macstr, 18, "%02x:%02x:%02x:%02x:%02x:%02x", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
LoadParameters();
Setpoint = -1 * phset;
lcd.createChar(0, ol1);
lcd.createChar(1, ol2);
lcd.createChar(2, ol3);
lcd.createChar(3, ol4);
lcd.createChar(4, ol5);
lcd.createChar(5, ol6);
lcd.createChar(6, ol7);
lcd.createChar(7, ol8);
lcd.begin(16, 2);
lcd.write(byte(0));
lcd.write(byte(1));
lcd.write(byte(2));
lcd.write(byte(3));
lcd.print(F(" ONTHANK LAB"));
lcd.setCursor(0, 1);
lcd.write(byte(4));
lcd.write(byte(5));
lcd.write(byte(6));
lcd.write(byte(7));
lcd.print(F(" OA CONTROL"));
delay(1000);
lcd.clear();
max.begin(MAX31865_3WIRE); //start pt100 temperature probe
Serial1.begin(9600); //set baud rate for the software serial port to 9600
inputstring.reserve(10); //set aside some bytes for receiving data from the PC
sensorstring.reserve(30); //set aside some bytes for receiving data from Atlas Scientific pH EZO
Serial1.print("*OK,0"); //Turn off the returning of OK after command to EZO pH
Serial1.print('\r'); //add a <CR> to the end of the string
Serial1.print("C,0"); //Reset pH stamp to continuous measurement: once per second
Serial1.print('\r'); //add a <CR> to the end of the string
pinMode(chiller, OUTPUT);
pinMode(co2reg, OUTPUT);
digitalWrite(chiller, HIGH);
digitalWrite(co2reg, HIGH);
///Check if ethernet is connected/////////////////////////////////////////////////////////////////////
int ethanswer = 0;
int ethstart = millis();
int timdiff = 0;
lcd.clear();
// lcd.print(F("Ethernet cable?"));
// lcd.setCursor(0, 1);
// lcd.print(F("Yes:1 No:2"));
// Serial.print("Ethernet cable? Yes:1 No: 2");
// while (ethanswer == 0 && timdiff <= 10000) {
// char ether = customKeypad.getKey();
// if (ether == '1') {
// ethanswer = 1;
// }
// if (ether == '2') {
// EthConnect = false;
// ethanswer = 1;
// }
// timdiff = millis() - ethstart;
// }
ethanswer = 1; // temporarily assume Ethernet is always connected
//////////////////////////////////////////////////////////////////////////////////////////////////////
if (EthConnect) {
lcd.clear();
lcd.write(byte(0));
lcd.write(byte(1));
lcd.write(byte(2));
lcd.write(byte(3));
lcd.print(F("Ethernet is"));
lcd.setCursor(0, 1);
lcd.write(byte(4));
lcd.write(byte(5));
lcd.write(byte(6));
lcd.write(byte(7));
lcd.print(F("connecting.."));
Serial.print(F("MAC Address: "));
Serial.print(mac[1]);
Serial.print(F(":"));
Serial.print(mac[2]);
Serial.print(F(":"));
Serial.print(mac[3]);
Serial.print(F(":"));
Serial.print(mac[4]);
Serial.print(F(":"));
Serial.print(mac[5]);
Serial.print(F(":"));
Serial.println(mac[6]);
pinMode(4, OUTPUT);
digitalWrite(4, HIGH);
if (Ethernet.begin(mac) == 0) {
Serial.println(F("Failed to configure Ethernet using DHCP"));
Ethernet.begin(mac, ip);
}
// start the ethernetServer
ethernetServer.begin();
Serial.print("ethernetServer is at ");
Serial.println(Ethernet.localIP());
}
//loading Tank ID///////////////////////////////////////////////////////////////////////////////////////
tankid = EEPROM_readDouble(tankidAddress);
if (isnan(tankid))
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(F("Tank ID# is not"));
lcd.setCursor(0, 1);
lcd.print(F("set. Set ID now"));
Serial.println(F("Start Tank ID change"));
delay(3000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(F("New Tank ID:"));
Key = customKeypad.waitForKey();
tankid = (Key - '0') * 10;
lcd.setCursor(0, 1);
lcd.print(Key);
Serial.print(F("Tens place: "));
Serial.println(Key);
Key = customKeypad.waitForKey();
tankid = (Key - '0') + tankid;
lcd.setCursor(1, 1);
lcd.print(Key);
Serial.print(F("Ones place: "));
Serial.println(Key);
EEPROM_writeDouble(tankidAddress, tankid);
delay(1000);
Serial.println(F("Tank ID change End"));
}
// load granularity
sensor_interval = EEPROM_readDouble(granularityAddress);
if (isnan(sensor_interval)) {
sensor_interval = 800; // default granularity for interval between logging
EEPROM_writeDouble(granularityAddress, sensor_interval);
}
// load maxDataAge
maxDataAge = EEPROM_readDouble(maxDataAgeAddress);
if (isnan(maxDataAge)) {
maxDataAge = 800; // default max data age
EEPROM_writeDouble(maxDataAgeAddress, maxDataAge);
}
// load phSeriesSizeAddress;
phSeriesSize = EEPROM_readDouble(phSeriesSizeAddress);
if (isnan(phSeriesSize)) {
phSeriesSize = 0;
EEPROM_writeDouble(phSeriesSizeAddress, phSeriesSize);
}
// load phSeriesPointerAddress;
phSeriesPointer = EEPROM_readDouble(phSeriesPointerAddress);
if (isnan(phSeriesPointer)) {
phSeriesPointer = 0;
EEPROM_writeDouble(phSeriesPointerAddress, phSeriesPointer);
}
// load tempSeriesSizeAddress;
tempSeriesSize = EEPROM_readDouble(tempSeriesSizeAddress);
if (isnan(tempSeriesSize)) {
tempSeriesSize = 0;
EEPROM_writeDouble(tempSeriesSizeAddress, tempSeriesSize);
}
// load tempSeriesPointerAddress;
tempSeriesPointer = EEPROM_readDouble(tempSeriesPointerAddress);
if (isnan(tempSeriesPointer)) {
tempSeriesPointer = 0;
EEPROM_writeDouble(tempSeriesPointerAddress, tempSeriesPointer);
}
// load phInterval;
phInterval = EEPROM_readDouble(phIntervalAddress);
if (isnan(phIntervalAddress)) {
phInterval = 0;
EEPROM_writeDouble(phIntervalAddress, phInterval);
}
// load phDelay;
phDelay = EEPROM_readDouble(phDelayAddress);
if (isnan(phDelayAddress)) {
phDelay = 0;
EEPROM_writeDouble(phDelayAddress, phDelay);
}
// load tempInterval;
tempInterval = EEPROM_readDouble(tempIntervalAddress);
if (isnan(tempIntervalAddress)) {
tempInterval = 0;
EEPROM_writeDouble(tempIntervalAddress, tempInterval);
}
// load tempDelay;
tempDelay = EEPROM_readDouble(tempDelayAddress);
if (isnan(tempDelayAddress)) {
tempDelay = 0;
EEPROM_writeDouble(tempDelayAddress, tempDelay);
}
///Starting the SD Card//////////////////////////////////////////////////////////////////////////////////
pinMode(10, OUTPUT);
digitalWrite(10, HIGH);
SD.begin(4);
File root = SD.open("/");
printDirectory(root, 0);
doDirectoryMaintenance();
///Starting the SD Card//////////////////////////////////////////////////////////////////////////////////
//Setting PID parameters////////////////////////////////////////////////////////////////////////////////////////
myPID.SetTunings(Kp, Ki, Kd);
myPID.SetMode(AUTOMATIC);
myPID.SetSampleTime(1000);
myPID.SetOutputLimits(0, WindowSize);
noInterrupts(); // disable all interrupts
// Run timer2 interrupt every 15 ms
TCCR2A = 0;
TCCR2B = 1 << CS22 | 1 << CS21 | 1 << CS20;
//Timer2 Overflow Interrupt Enable
TIMSK2 |= 1 << TOIE2;
interrupts();
//loading Temp Correction////////////////////////////////////////////////////////////////////////////////////////
tempcorr = EEPROM_readDouble(tempcorrAddress);
if (isnan(tempcorr))
{
tempcorr = 0;
}
//Filling array for temp smoothing///////////////////////////////////////////////////////////////////////
for (int thisReading = 0; thisReading < numReadings; thisReading++) {
readings[thisReading] = 0;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////
byte upArrow[8] = {
0b00100,
0b01110,
0b10101,
0b00100,
0b00100,
0b00100,
0b00100,
0b00100
};
lcd.createChar(0, upArrow);
lcd.clear();
lcd.print(F("pH="));
lcd.setCursor(0, 1) ; //Display position
lcd.print(F("T=")); //display"Temp="
wdt_enable(WDTO_8S);
}