-
Notifications
You must be signed in to change notification settings - Fork 1
/
doser.ino
239 lines (211 loc) · 6.32 KB
/
doser.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
#include <Button.h> //https://github.com/JChristensen/Button
#define RELAY_PIN 3 //Relay for activating dosing
#define BUTTON_PIN 2 //Connect a tactile button switch (or something similar)
//from Arduino pin 2 to ground.
#define PULLUP true //To keep things simple, we use the Arduino's internal pullup resistor.
#define INVERT false //Since the pullup resistor will keep the pin high unless the
//switch is closed, this is negative logic, i.e. a high state
//means the button is NOT pressed. (Assuming a normally open switch.)
#define DEBOUNCE_MS 10 //A debounce time of 20 milliseconds usually works well for tactile button switches.
#define LED_PIN 13 //The standard Arduino "Pin 13" LED.
#define LONG_PRESS 500 //We define a "long press" to be 500 milliseconds.
#define BLINK_INTERVAL 50 //In the BLINK state, switch the LED every 50 milliseconds.
Button myBtn(BUTTON_PIN, PULLUP, INVERT, DEBOUNCE_MS); //Declare the button
//The list of possible states for the state machine.
enum STATES {
HELLO_VERSION,
TO_PH, PH,
MANUAL_DOSE,
TO_CALFOUR, CALFOUR,
TO_CALSEVEN, CALSEVEN,
TO_READONLY, READONLY
};
uint8_t STATE; //The current state machine state
boolean readOnlyState; //The readonly state (false = dont auto dose)
boolean ledState; //The current LED status
unsigned long startMs;
unsigned long ms; //The current time from millis()
unsigned long msLast; //The last time the LED was switched
void setup(void)
{
Serial.begin(9600);
pinMode(RELAY_PIN, OUTPUT); //Set the relay pin as an output
pinMode(LED_PIN, OUTPUT); //Set the LED pin as an output
startMs = 0;
STATE = HELLO_VERSION;
}
void loop(void)
{
ms = millis(); //record the current time
myBtn.read(); //Read the button
switch (STATE) {
case HELLO_VERSION:
turnOnLED();
if (startMs == 0) {
startMs = ms;
}
unsigned long sinceStartup;
sinceStartup = ms - startMs;
if (sinceStartup >= 6000) {
STATE = TO_PH;
}
if (sinceStartup >= 4000) {
// Display "v1.0"
Serial.println("Display: v1.0");
}
else if (sinceStartup >= 2000) {
// Display "tHiS"
Serial.println("Display: tHiS");
}
else {
// Display "dOSE"
Serial.println("Display: dOSE");
}
break;
case TO_PH:
turnOffLED();
if (myBtn.isReleased()) {
Serial.println("Switching to PH view");
STATE = PH;
}
break;
// This is the main screen
case PH:
if (myBtn.wasReleased()) {
STATE = TO_CALFOUR;
}
else if (myBtn.pressedFor(LONG_PRESS)) {
// Manual dosing for pump priming & testing
STATE = MANUAL_DOSE;
}
else if (myBtn.isPressed()) {
fastBlink();
}
else {
// Display current pH. ex: "4.01"
turnOffLED();
}
break;
case MANUAL_DOSE:
if (myBtn.isPressed()) {
// Activate pump as long as button is held down
Serial.println("Display: dOSE");
// Display "dOSE"
fastBlink();
}
else {
// Deactivate pump
STATE = TO_PH;
}
break;
case TO_CALFOUR:
turnOffLED();
if (myBtn.wasReleased()) {
Serial.println("Switching to CAL4 menu");
STATE = CALFOUR;
}
break;
case CALFOUR:
if (myBtn.wasReleased()) {
STATE = TO_CALSEVEN;
}
else if (myBtn.pressedFor(LONG_PRESS)) {
// Execute 4.0 calibration
Serial.println("Calibrating pH 4.0!");
STATE = TO_CALFOUR;
}
else if (myBtn.isPressed()) {
fastBlink();
}
else if (ms - myBtn.lastChange() >= 5000) {
// after 5 seconds go to PH
STATE = TO_PH;
} else {
// Display "CAL4"
slowBlink();
}
break;
case TO_CALSEVEN:
turnOffLED();
if (myBtn.wasReleased()) {
Serial.println("Switching to CAL7 menu");
STATE = CALSEVEN;
}
break;
case CALSEVEN:
if (myBtn.wasReleased()) {
STATE = TO_READONLY;
}
else if (myBtn.pressedFor(LONG_PRESS)) {
// Execute 7.0 calibration
Serial.println("Calibrating pH 7.0!");
STATE = TO_CALSEVEN;
}
else if (myBtn.isPressed()) {
fastBlink();
}
else if (ms - myBtn.lastChange() >= 5000) {
STATE = TO_PH;
}
else {
// Display "CAL7"
slowBlink();
}
break;
case TO_READONLY:
turnOffLED();
if (myBtn.wasReleased()) {
Serial.println("Switching to RO menu");
STATE = READONLY;
}
break;
case READONLY:
if (myBtn.wasReleased() || ms - myBtn.lastChange() >= 5000) {
STATE = TO_PH;
}
else if (myBtn.pressedFor(LONG_PRESS)) {
Serial.println("Inverting RO mode");
readOnlyState = !readOnlyState;
STATE = TO_READONLY;
}
else if (myBtn.isPressed()) {
fastBlink();
}
else {
// Display current readonly state
slowBlink();
}
break;
}
}
//Reverse the current LED state. If it's on, turn it off. If it's off, turn it on.
void switchLED()
{
msLast = ms; //record the last switch time
ledState = !ledState;
digitalWrite(LED_PIN, ledState);
}
void turnOffLED()
{
msLast = ms; //record the last switch time
ledState = false;
digitalWrite(LED_PIN, ledState);
}
void turnOnLED()
{
msLast = ms; //record the last switch time
ledState = true;
digitalWrite(LED_PIN, ledState);
}
//Switch the LED on and off every LONG_PRESS milliseconds.
void slowBlink()
{
if (ms - msLast >= LONG_PRESS)
switchLED();
}
//Switch the LED on and off every BLINK_INTERVAL milliseconds.
void fastBlink()
{
if (ms - msLast >= BLINK_INTERVAL)
switchLED();
}