-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathautomated-aquaponics-control-system.ino
253 lines (207 loc) · 7.48 KB
/
automated-aquaponics-control-system.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
/* Automated Aquaponics Control System
* Version 0.1.0
*
* This Sketch controlls the timing for an automated aquaponics system.
* Copyright (C) 2015 Simon Lehmann
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
// Incude Libraries for Timer
#include <EventFuse.h> // Install with .zip from https://github.com/davidknaack/EventFuse
#include <MsTimer2.h> // Install from Arduino IDE Library Manager
// Include libary for Isolated Storage
#include <EEPROM.h>
// Timer #define statements
#define OUTPUT_COUNT 4
#define OFF_TIME 0
#define ON_TIME 1
#define OUTPUT_PIN 2
String ver = "0.1.0";
String repo_url = "https://github.com/simonlehmann/automated-aquaponics-control-system";
/*----- TIMER -----*/
// The outputs array defines how long each output will
// be turned off, on, and what pin to use for that output.
// The off and on values are in units of 'ticks'. The length
// of a tick is controlled by the setup of MsTimer2.
// off on pin
byte outputs[OUTPUT_COUNT][3] = {{ 1, 1, 6}, // Output A - valve timer for grow bed ON/OFF cycle
{ 0, 0, 0}, // Output B - vacant
{ 0, 0, 0}, // Output C - vacant
{ 0, 0, 0}}; // Output D - vacant
// at start-up of sketch relay is in OFF mode for
// period of time of the OFF time for Output A (valve)
void OutputHandler(FuseID fuseID, int& outputID){
// look up the pin associated with this output
byte pin = outputs[outputID][OUTPUT_PIN];
// get and invert the current pin state and write
// it back to the port to invert the current pin state.
int state = 1&~digitalRead(pin);
digitalWrite(pin, state);
// Reset the fuse length with a new interval. The current state
// of the pin is used to determine which interval should be used.
EventFuse::fuses[fuseID].fuseLen = outputs[outputID][state];
}
void timerTick(){
EventFuse::burn();
}
/*----- END TIMER -----*/
/*----- SWITCH -----*/
// Input pins for switching running mode
int inPinRun = 12;
int inPinAuto = 11;
int inPinStop = 10;
// Output pins for running mode LEDs
int outPinRun = 4;
int outPinAuto = 3;
int outPinStop = 2;
// Putput pins for relay control
int outPinPumpRelay = 6; // TODO: Not used yet
int state = HIGH; // the current state of the output pin
int reading; // the current reading from the input pin
int previous = LOW; // the previous reading from the input pin
// the follow variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long time = 0; // the last time the output pin was toggled
long debounce = 200; // the debounce time (miliseconds), increase if the output flickers
/*----- END SWITCH -----*/
void setup() {
Serial.begin(9600);
while (!Serial);
Serial.println("Automated Aquaponics Control System");
Serial.println("Version: " + ver);
Serial.println("Repo URL: " + repo_url);
Serial.println();
Serial.println("Starting...");
// Disable onboard L LED
pinMode(13, OUTPUT);
digitalWrite(outPinStop, LOW);
// Set up and init all outputs to off
for(byte i = 0; i < OUTPUT_COUNT; i++){
//pinMode(outputs[i][OUTPUT_PIN], OUTPUT);
digitalWrite(outputs[i][OUTPUT_PIN], LOW);
// Set up an event fuse for this output.
EventFuse::newFuse(i, outputs[i][OFF_TIME], INF_REPEAT, OutputHandler);
}
// Set pin modes
pinMode(inPinRun, INPUT);
pinMode(inPinAuto, INPUT);
pinMode(inPinStop, INPUT);
pinMode(outPinRun, OUTPUT);
pinMode(outPinAuto, OUTPUT);
pinMode(outPinStop, OUTPUT);
// Run lamp test to test all LEDs on controll panel
Serial.println("Lamp test...");
digitalWrite(outPinStop, HIGH);
digitalWrite(outPinAuto, HIGH);
digitalWrite(outPinRun, HIGH);
delay(2000);
digitalWrite(outPinStop, LOW);
digitalWrite(outPinAuto, LOW);
digitalWrite(outPinRun, LOW);
delay(500);
Serial.println("Lamp test complete.");
// Set MsTimer2 for one second per tick (in milliseconds,
MsTimer2::set(1000, timerTick);
Serial.println("System Ready.");
Serial.println();
// Read last running mode from EEPROM (if one exists)
if (EEPROM.read(0) == 1){
Serial.println("Resuming last run mode: Run.");
pumpRun();
} else if (EEPROM.read(0) == 2){
Serial.println("Resuming last run mode: Auto.");
pumpAuto();
} else if (EEPROM.read(0) == 3){
Serial.println("Resuming last run mode: Stop.");
pumpStop();
} else {
// If no previous running mode found, remain stopped
Serial.println("Unknown last run mode. Defaulting to run mode: Stop.");
pumpStop();
}
}
void loop(){
// Running modes
// 1 = Run
// 2 = Automatic
// 3 = Stopped
// Check if any running mode control buttons are pressed,
// if so, change 'reading' to respective running mode int
if (digitalRead(inPinRun) == HIGH){
reading = 1;
} else if (digitalRead(inPinAuto) == HIGH){
reading = 2;
} else if (digitalRead(inPinStop) == HIGH){
reading = 3;
} else {
// if no button pressed, set 'reading' to 0
reading = 0;
}
//Serial.println(reading);
// if the input just went from LOW and HIGH and we've waited long enough
// to ignore any noise on the circuit, toggle the output pin and remember
// the time
// Check for button state changed
if (reading == 1 && previous != 1 && millis() - time > debounce) {
pumpRun();
time = millis();
}
if (reading == 2 && previous != 2 && millis() - time > debounce) {
pumpAuto();
time = millis();
}
if (reading == 3 && previous != 3 && millis() - time > debounce) {
pumpStop();
time = millis();
}
}
// RUN mode method
void pumpRun(){
// Update control panel LEDs to confirm running mode change
digitalWrite(outPinRun, HIGH);
digitalWrite(outPinAuto, LOW);
digitalWrite(outPinStop, LOW);
// Write running mode change to EEPROM so it can be recovered on next boot
EEPROM.update(0, 1);
previous = 1;
// Stop MsTimer
MsTimer2::stop();
Serial.println("Pump run mode changed to RUN!");
}
// AUTO mode method
void pumpAuto(){
// Update control panel LEDs to confirm running mode change
digitalWrite(outPinAuto, HIGH);
digitalWrite(outPinRun, LOW);
digitalWrite(outPinStop, LOW);
// Write running mode change to EEPROM so it can be recovered on next boot
EEPROM.update(0, 2);
previous = 2;
// Start MsTimer
MsTimer2::start();
Serial.println("Pump run mode changed to AUTO!");
}
// STOP mode method
void pumpStop(){
// Update control panel LEDs to confirm running mode change
digitalWrite(outPinStop, HIGH);
digitalWrite(outPinAuto, LOW);
digitalWrite(outPinRun, LOW);
// Write running mode change to EEPROM so it can be recovered on next boot
EEPROM.update(0, 3);
previous = 3;
// Stop MsTimer
MsTimer2::stop();
Serial.println("Pump run mode changed to STOP!");
}