Repository navigation
Expand file tree
/
Copy pathSmartPlantSystem.ino
More file actions
352 lines (299 loc) · 7.75 KB
/
Copy pathSmartPlantSystem.ino
File metadata and controls
352 lines (299 loc) · 7.75 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
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
// https://github.com/johnrickman/LiquidCrystal_I2C/tree/master
#include <LiquidCrystal_I2C.h>
#include <Servo.h>
// Pin numbers for analogWrite
#define RED_PIN 9
#define GREEN_PIN 5
#define BLUE_PIN 6
// Port bits for DDR banks
#define RED_PORT (1 << DDB1)
#define GREEN_PORT (1 << DDD5)
#define BLUE_PORT (1 << DDD6)
// Reference voltage for ADC sensors
#define REF_VOLTAGE 5.0
// External Oscillator Frequency
#define F_CPU 16000000UL
// Desired Baud Rate (bits per second)
#define BAUD 9600
// Calculating of setting Register to specify corresponding baud rate
//( 16000000/(16*9600)) - 1 = 103.167 (==> ~103).
// Clock difference of 0.3% + every frame sync. Hence could be rounded.
#define MYUBRR F_CPU/16/BAUD-1
// Returns true if data has been received from UART.
#define RECEIVED_UART_DATA (UCSR0A & (1 << RXC0))
// Servo ID
#define SERVO_PIN 10
// User command to begin watering process
#define WATERING_COMMAND 'W'
// Sensor ids
#define SENSOR_SLOT_MOISTURE 0
#define SENSOR_SLOT_TEMP 1
#define SENSOR_SLOT_PHOTORESISTOR 2
#define MAX_ADC_SENSORS 3
// Sensor error flags
#define SENSOR_ERROR_MOISTURE (1 << SENSOR_SLOT_MOISTURE)
#define SENSOR_ERROR_TEMP (1 << SENSOR_SLOT_TEMP)
#define SENSOR_ERROR_LIGHT (1 << SENSOR_SLOT_PHOTORESISTOR)
// Sensor threshold for error
// ADC value below this means the environment is too dry
#define SENSOR_MOISTURE_TOO_DRY_THRESHOLD 500
// Temperature (in celcius) value above this means
// the environment is too hot
#define SENSOR_TEMP_TOO_HIGH_C_THRESHOLD 40
// ADC value below this means the environment is too dark
#define SENSOR_LIGHT_TOO_LOW_THRESHOLD 800
// State machine
enum SystemState
{
STATE_OK = 0,
STATE_TOO_DRY = SENSOR_ERROR_MOISTURE,
STATE_TOO_HOT = SENSOR_ERROR_TEMP,
STATE_TOO_DARK = SENSOR_ERROR_LIGHT,
STATE_DRY_AND_HOT = SENSOR_ERROR_MOISTURE | SENSOR_ERROR_TEMP,
STATE_DRY_AND_DARK = SENSOR_ERROR_MOISTURE | SENSOR_ERROR_LIGHT,
STATE_DARK_AND_HOT = SENSOR_ERROR_LIGHT | SENSOR_ERROR_TEMP,
STATE_ALL_ERRORS = SENSOR_ERROR_MOISTURE | SENSOR_ERROR_TEMP | SENSOR_ERROR_LIGHT
};
// System color based on error
enum SystemColor {
INIT,
OK,
MINOR,
MAJOR,
FATAL
};
// RGB Led color struct
struct RgbColor {
uint8_t r;
uint8_t g;
uint8_t b;
};
// RGB possible colors
const RgbColor SYSTEM_COLORS[] = {
{255, 255, 255}, // INIT (White)
{0, 255, 0}, // OK (Green)
{160, 0, 160}, // MINOR (Purple)
{255, 200, 0}, // MAJOR (Yellow)
{255, 0, 0} // FATAL (Red)
};
// Watering machine
enum WateringState
{
WATER_NO_COMMAND = 0,
WATER_OPEN_PUMP = 1,
WATER_CLOSE_PUMP = 2
};
LiquidCrystal_I2C lcd(0x20, 16, 2);
Servo pumpServo;
volatile uint16_t adcValues[MAX_ADC_SENSORS];
volatile uint8_t currentADCChannel = 0;
uint8_t newSystemState = STATE_OK;
uint8_t systemState = STATE_OK;
WateringState wateringState = WATER_NO_COMMAND;
void setLedColor(uint8_t wantedColor)
{
RgbColor color = SYSTEM_COLORS[wantedColor];
analogWrite(RED_PIN, color.r);
analogWrite(GREEN_PIN, color.g);
analogWrite(BLUE_PIN, color.b);
}
void rgb_setup()
{
// Set RGB pins as output
DDRB |= RED_PORT;
DDRD |= GREEN_PORT | BLUE_PORT;
// White color
setLedColor(INIT);
}
void USART_Init(uint16_t ubrr)
{
// Set baud rate
// Splitting into two registers.
UBRR0H = (uint8_t)(ubrr >> 8);
UBRR0L = (uint8_t)ubrr;
// Enable transmitter and receiver
// Otherwise could be used as standard I/O pins.
UCSR0B = (1 << RXEN0) | (1 << TXEN0);
// Set frame format: 8 data bits, 1 stop bit, no parity (default)
UCSR0C = (1 << UCSZ01) | (1 << UCSZ00);
}
void adc_setup()
{
// Set ADMUX to use AVcc as reference voltage and select ADC0 as input channel
ADMUX |= (1 << REFS0);
// Set ADCSRA to enable ADC
ADCSRA |= (1 << ADEN);
// Set ADC prescaler to 128 (16 MHz / 128 = 125 kHz ADC clock)
ADCSRA |= (1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0);
// Enable ADC Interrupts
ADCSRA |= (1 << ADIE);
}
void setup()
{
// Power on LCD
lcd.init();
lcd.backlight();
// Setup UART
USART_Init(MYUBRR);
// Setup servo
pumpServo.attach(SERVO_PIN); // Attach the servo to Digital Pin 10
pumpServo.write(0);
// Enable interrupts for ADC
cli();
adc_setup();
sei();
// Start next ADC conversion
ADCSRA |= (1 << ADSC);
// Setup RGB LED
rgb_setup();
// Print initial screen data
lcd.setCursor(2,0);
lcd.print("~ PlantMCU ~");
lcd.setCursor(0, 1);
lcd.print("Starting...");
delay(2000);
lcd.clear();
}
ISR(ADC_vect) {
// Save current sensor value into sensor value array
adcValues[currentADCChannel] = ADC;
// Move to the next sensor in cyclic order
currentADCChannel++;
if(currentADCChannel == MAX_ADC_SENSORS)
{
currentADCChannel = 0;
}
ADMUX = (ADMUX & 0xF0) | currentADCChannel;
// Start next ADC conversion
ADCSRA |= (1 << ADSC);
}
void loop()
{
newSystemState = STATE_OK;
float temp = calculateTempFromADC(adcValues[SENSOR_SLOT_TEMP]);
if(adcValues[SENSOR_SLOT_MOISTURE] < SENSOR_MOISTURE_TOO_DRY_THRESHOLD)
{
newSystemState |= SENSOR_ERROR_MOISTURE;
}
if(temp > SENSOR_TEMP_TOO_HIGH_C_THRESHOLD)
{
newSystemState |= SENSOR_ERROR_TEMP;
}
if(adcValues[SENSOR_SLOT_PHOTORESISTOR] < SENSOR_LIGHT_TOO_LOW_THRESHOLD)
{
newSystemState |= SENSOR_ERROR_LIGHT;
}
if(systemState != newSystemState)
{
systemState = newSystemState;
updateLCD();
}
wateringProcess();
updateLed();
delay(2000); // Wait for 2000 millisecond(s)
}
float calculateTempFromADC(uint16_t adcValue)
{
// https://www.analog.com/media/en/technical-documentation/data-sheets/TMP35_36_37.pdf
// Based on page 8 functional description of TMP36.
// Ref voltage is 5.0V according to arduino.
float voltage = adcValue * (REF_VOLTAGE / 1023.0);
return (voltage - 0.5) * 100 ;
}
// The watering process is called every loop (2s)
void wateringProcess()
{
if(RECEIVED_UART_DATA)
{
// User commanded to water the planet
if(UDR0 == WATERING_COMMAND)
{
wateringState = WATER_OPEN_PUMP;
}
}
switch(wateringState)
{
case WATER_OPEN_PUMP:
// Open pump
pumpServo.write(90);
updateLCD();
wateringState = WATER_CLOSE_PUMP;
break;
case WATER_CLOSE_PUMP:
// Close pump
pumpServo.write(0);
wateringState = WATER_NO_COMMAND;
updateLCD();
break;
}
}
// Updates LCD based on the new state
void updateLCD()
{
lcd.clear();
lcd.setCursor(2,0);
lcd.print("~ PlantMCU ~");
lcd.setCursor(0,1);
if(wateringState == WATER_OPEN_PUMP)
{
lcd.setCursor(2,1);
lcd.print("~ Watering ~");
return;
}
switch(systemState)
{
case STATE_OK:
lcd.setCursor(1,1);
lcd.print("~ SYSTEM: OK ~");
break;
case STATE_TOO_DRY:
lcd.print("MINOR: TOO DRY");
break;
case STATE_TOO_HOT:
lcd.print("MINOR: TOO HOT");
break;
case STATE_TOO_DARK:
lcd.print("MINOR: TOO DARK");
break;
case STATE_DRY_AND_HOT:
lcd.print("MAJOR: DRY&HOT");
break;
case STATE_DRY_AND_DARK:
lcd.print("MAJOR: DRY&DARK");
break;
case STATE_DARK_AND_HOT:
lcd.print("MAJOR: HOT&DARK");
break;
case STATE_ALL_ERRORS:
lcd.print("BAD:DRY&HOT&DARK");
}
}
// Updates RGB Led based on the new state
void updateLed()
{
if(wateringState != WATER_NO_COMMAND)
{
analogWrite(RED_PIN, 0);
analogWrite(GREEN_PIN, 0);
analogWrite(BLUE_PIN, 255);
return;
}
switch(systemState)
{
case STATE_OK:
setLedColor(OK);
break;
case STATE_TOO_DRY:
case STATE_TOO_HOT:
case STATE_TOO_DARK:
setLedColor(MINOR);
break;
case STATE_DRY_AND_HOT:
case STATE_DRY_AND_DARK:
case STATE_DARK_AND_HOT:
setLedColor(MAJOR);
break;
case STATE_ALL_ERRORS:
setLedColor(FATAL);
}
}