-
Notifications
You must be signed in to change notification settings - Fork 0
/
LF07Arduino.ino
253 lines (229 loc) · 5.43 KB
/
LF07Arduino.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
//www.elegoo.com
//2018.10.25
#include <IRremote.hpp>
#include <LiquidCrystal.h>
#include <dht_nonblocking.h>
#define DHT_SENSOR_TYPE DHT_TYPE_11
#define PHOTORESISTOR_PIN A5
#define WATERLEVEL_PIN A0
// Temp/ Humidity Sensor
static const int DHT_SENSOR_PIN = 2;
DHT_nonblocking dht_sensor( DHT_SENSOR_PIN, DHT_SENSOR_TYPE );
float temperature;
float humidity;
// LCD Display
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
int currentPage = 0;
int maxPages = 3;
// Water Level Detector
int waterLevelValue = 0;
//IR Reciever
IRrecv irrecv(3);
uint32_t last_decodedRawData = 0;
// Warning LEDS
int warningLevel = 0;
int redLed = 4;
int yellowLed = 5;
int greenLed = 6;
String warningMsg = "";
bool warnOnLCD = false;
// Luminosity
int luminosity = 0;
/*
* Initialize the serial port.
*/
void setup( )
{
Serial.begin(9600);
// init lcd
lcd.begin(16, 2);
// init receiver
irrecv.enableIRIn();
// init pins
pinMode(redLed, OUTPUT);
pinMode(yellowLed, OUTPUT);
pinMode(greenLed, OUTPUT);
pinMode(PHOTORESISTOR_PIN, INPUT);
pinMode(WATERLEVEL_PIN, INPUT);
}
void translateIR()
{
if (irrecv.decodedIRData.flags)
{
irrecv.decodedIRData.decodedRawData = last_decodedRawData;
}
switch (irrecv.decodedIRData.decodedRawData)
{
case 0xF807FF00:
currentPageDown();
break;
case 0xF609FF00:
currentPageUp();
break;
case 0xE619FF00:
warnOnLCD = !warnOnLCD;
break;
default:
break;
}
last_decodedRawData = irrecv.decodedIRData.decodedRawData;
delay(500); // Do not get immediate repeat
}
void currentPageUp() {
if(currentPage < maxPages) {
currentPage++;
} else {
currentPage = 0;
}
clearCurrentPage();
}
void currentPageDown() {
if(currentPage > 0) {
currentPage--;
} else {
currentPage = maxPages;
}
clearCurrentPage();
}
void clearCurrentPage(){
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Page: " + String(currentPage));
}
void renderCurrentPage(){
switch(currentPage) {
case(0):
lcd.setCursor(0,0);
lcd.print("Smart Garden");
lcd.setCursor(0,1);
lcd.print("Press UP or DOWN");
break;
case(1):
lcd.setCursor(0, 0);
lcd.print("Temp.: " + String((int)temperature) + "C");
lcd.setCursor(0, 1);
lcd.print("Humidity: " + String((int)humidity) + "%");
break;
case(2):
lcd.setCursor(0, 0);
lcd.print("Water Level: ");
lcd.setCursor(0, 1);
lcd.print(String(waterLevelValue));
break;
case(3):
lcd.setCursor(0, 0);
lcd.print("Luminosity: ");
lcd.setCursor(0, 1);
lcd.print(String(luminosity));
break;
default:
lcd.setCursor(0,0);
lcd.print("404");
break;
}
}
void lightWarningLed() {
switch(warningLevel) {
case(0):
digitalWrite(greenLed, HIGH);
digitalWrite(yellowLed, LOW);
digitalWrite(redLed, LOW);
if(warnOnLCD) {
lcd.setCursor(0,0);
lcd.print("No Problems :)");
}
break;
case(1):
digitalWrite(greenLed, LOW);
digitalWrite(yellowLed, HIGH);
digitalWrite(redLed, LOW);
if(warnOnLCD) {
lcd.setCursor(0, 0);
lcd.print("Warning ");
lcd.setCursor(0, 1);
lcd.print(warningMsg);
}
break;
case(2):
digitalWrite(greenLed, LOW);
digitalWrite(yellowLed, LOW);
digitalWrite(redLed, HIGH);
if(warnOnLCD) {
lcd.setCursor(0, 0);
lcd.print("CRITICAL!!! ");
lcd.setCursor(0, 1);
lcd.print(warningMsg);
}
break;
default:
lcd.setCursor(0,0);
lcd.print("No Problems :)");
break;
}
}
/*
* Poll for a measurement, keeping the state machine alive. Returns
* true if a measurement is available.
*/
static bool measure_environment( float *temperature, float *humidity )
{
static unsigned long measurement_timestamp = millis( );
/* Measure once every second. */
if( millis( ) - measurement_timestamp > 4000ul )
{
if( dht_sensor.measure( temperature, humidity ) == true )
{
measurement_timestamp = millis( );
waterLevelValue = analogRead(WATERLEVEL_PIN);
luminosity = analogRead(PHOTORESISTOR_PIN);
Serial.println(String(*temperature) + "," + String(*humidity) + "," + String(waterLevelValue) + "," + String(luminosity));
return( true );
}
}
return( false );
}
/*
* Main program loop.
*/
void loop( )
{
/* Measure temperature and humidity. If the functions returns
true, then a measurement is available. */
if( measure_environment( &temperature, &humidity ) == true ) {
if(humidity < 30) {
warningLevel = 1;
warningMsg = "Humidity low";
} else if(humidity > 60) {
warningLevel = 1;
warningMsg = "Humidity high";
} else if(temperature >= 30) {
warningLevel = 2;
warningMsg = "Temps. HIGH";
} else if(temperature <= 20) {
warningLevel = 2;
warningMsg = "Temps. LOW";
} else if(waterLevelValue >= 300){
warningLevel = 2;
warningMsg = "WaterLevels HIGH";
} else if(luminosity > 400){
warningLevel = 1;
warningMsg = "Luminosity high";
} else if(luminosity < 50){
warningLevel = 2;
warningMsg = "Luminosity LOW";
} else {
warningLevel = 0;
}
lcd.clear();
}
// decode signal from receiver
if (irrecv.decode())
{
translateIR();
irrecv.resume(); // receive the next value
}
if(!warnOnLCD) {
renderCurrentPage();
}
lightWarningLed();
}