-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsmartGarden.ino
183 lines (167 loc) · 4.41 KB
/
smartGarden.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
//add DHT sensor library
#include <DHT.h>
//add LCD library
#include <rgb_lcd.h>
//add ChainableLED library to this project
#include <ChainableLED.h>
//add Timelib library
#include <TimeLib.h>
//assign default time as epoch time 1514764800 which is 00:00:00 Jan 1 2018
long DEFAULT_TIME = 1514764800;
long waterTime = DEFAULT_TIME + 86400;
//set the number of leds linked to the chain
#define NUM_LEDS 1
//assign LightSensor as A0
#define LightSensor A0
//set digital pin2 as DHTPIN
#define DHTPIN 2
//set title of pin 5 as tiltSwitch
#define tiltSwitch 5
//assign buzzer as pin 6
#define buzzer 6
//set the sensor type as DHT 11
#define DHTTYPE DHT11
/*assign dht as the name of DHT sensor
set the sensor pin as DHTPIN(pin2),
set the sensor type as DHTTYPE(DHT11)
*/
DHT dht(DHTPIN, DHTTYPE);
/*assign leds as the name of
the ChainableLED set the
pin of the ChainableLED to
pin7(clock pin) and pin8(data pin)
and number of the leds*/
ChainableLED leds(7, 8, NUM_LEDS);
//assign lcd as the name of rgb_lcd screen
rgb_lcd lcd;
void setup()
{
//
setTime(DEFAULT_TIME);
//initialise the dht sensor
dht.begin();
//initialise the lcd screen;
//set up the lcd's number of columns and rows:
lcd.begin(16, 2);
//initialise ChainableLED leds
leds.init();
//set pin 5(tilt switch) as input pin
pinMode(tiltSwitch, INPUT);
delay(1000);
}
int mode = 0;
void loop()
{
//-------------DHT---------------------
//store the humidity value to h
int h = dht.readHumidity();
//store the temperature value to t(in Celsius)
int t = dht.readTemperature();
int value = analogRead(LightSensor);
float value_float = map(value, 0, 800, 50, 0) / 100.0;
leds.setColorHSB(0, 0, 0, value_float);
//initialise mode to 0, then set to case 0;
//temperature exceed 38 degrees, then set to case 1;
if (t > 38) {
mode = 1;
}
//Humidity is less than 40 %, then set to case 2;
if (h < 40)
{
mode = 2;
}
//LightSensor reading value is less than 50, then set to case 3;
if (value < 50)
{
mode = 3;
}
//current time is greate or equals to waterTime(24 hour ahead), then set to case 4;
if (now() >= waterTime ) {
mode = 4;
}
switch (mode) {
case 0:
//set the LCD cursor to column 0, line 0
lcd.clear();
lcd.setCursor(0, 0);
//Print text temperature: to the LCD
lcd.print("Temperature:");
//set the LCD cursor to column 12, line 0
lcd.setCursor(12, 0);
//Print temperature value t to the LCD
lcd.print(t);
//set the LCD cursor to column 14, line 0
lcd.setCursor(14, 0);
//Print temperature º is character 223 on lookup table
lcd.write(223);
//Print C to the LCD
lcd.print("C");
//set the LCD cursor to column 0, line 1
lcd.setCursor(0, 1);
//Print text Humidity: to the LCD
lcd.print("Humidity: ");
//set the LCD cursor to column 10, line 1
lcd.setCursor(10, 1);
//Print humidity value h to the LCD
lcd.print(h);
//set the LCD cursor to column 12, line 1
lcd.setCursor(12, 1);
//Print sign % to the LCD
lcd.print("%");
break;
case 1:
tone(buzzer, 262, 300);
leds.setColorRGB(0, 255, 0, 0);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Temperature too ");
lcd.setCursor(0, 1);
lcd.print("High!!");
if (HIGH == digitalRead(tiltSwitch))
{
leds.setColorRGB(0, 0, 0, 0);
mode = 0;
}
break;
case 2:
tone(buzzer, 294, 300);
leds.setColorRGB(0, 255, 0, 0);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Warning! Too Dry");
if (HIGH == digitalRead(tiltSwitch))
{
leds.setColorRGB(0, 0, 0, 0);
mode = 0;
}
break;
case 3:
tone(buzzer, 330, 300);
leds.setColorRGB(0, 255, 0, 0);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Not Enough Light");
lcd.setCursor(0, 1);
lcd.print("Check the LED..");
if (HIGH == digitalRead(tiltSwitch))
{
leds.setColorRGB(0, 0, 0, 0);
mode = 0;
}
break;
case 4:
tone(buzzer, 349, 300);
leds.setColorRGB(0, 255, 0, 0);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Time to water");
lcd.setCursor(0, 1);
lcd.print("the plants");
if (HIGH == digitalRead(tiltSwitch))
{
waterTime = now() + 86400;
mode = 0;
}
break;
}
}