-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGiuseppina2.0.ino
447 lines (371 loc) · 10.7 KB
/
Giuseppina2.0.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
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 8, 7, 6, 5);
#include <DS1302.h>
// Init the DS1302
DS1302 rtc(2, 3, 4);
const int brightnessPin = 10; //pin to control brightness
//int brightness = 0; //default brightness
int previousHour = 25; //default hour
int currentMinute=0;
#define INTERVAL_SECONDS 2 //10 secondi
#define INTERVAL_MINUTES 10 //10 minuti
#define INTERVAL_WETS_TIMES 5 //numero di volte massimo che posso innaffiare la pianta in un determinato tempo
const int moisturePin=A0; //pin per il lettore capacitivo
const int airValue=445; //valore massimo per il sensore
const int waterValue=196; //valore minimo per il sensore
int soilMoisture=0; //valore inizializzato del sensore
int soilMoisturePercent=0; //valore inizializzato per la percentuale
#define WET_VALUE 30 //Under 30% the plants gets water
const int relayPin=9; //pin per il relay
int wetTimes = 0; //numero di volte che ho innaffiato
#define READINGS 100 //100 letture per la media
int lastReadings[READINGS];
int currentReading = 0; //inizializzo la lettura corrente
int average = 0;
const int buttonPin = 13;
unsigned long currentMillis = millis();
unsigned long DELAY_IN_MILLISECONDS = 600000; //impostato: 10 minuti //un minuto: 60000
int buttonState = 0; //stato del bottone
unsigned long previousMillisAverage = 0; //tempo passato per ogni media effettuata.
void setup() {
lcd.begin(16,2); //inizializzo lo schermo
Serial.begin(9600); //inizializzo il serial monitor
//inizializzo il real time clock
rtc.halt(false);
//inizializzo il pin per il relay
pinMode(relayPin,OUTPUT);
digitalWrite(relayPin,HIGH);
pinMode(buttonPin, INPUT); //inizializzo il pin per il button
pinMode(brightnessPin, OUTPUT); //inizializzo il pin per la luminosità dell' LCD
//digitalWrite(brightnessPin, HIGH);
analogWrite(brightnessPin, 0); //all'inizio l'LCD ha luminosità minima
setTime(); //aggiorno l'orario dell' RTC
previousMillisAverage = millis(); //inizializzo i millisecondi passati
}
void loop() {
readMoisturePercent(); //leggo dal sensore l'umidità del terreno
int temp = getHours(); //leggo l'ora corrente
if(previousHour != temp){ //controllo ogni ora
setBrightness(whatTimeItIs()); //imposto la luminosità corrispondente
previousHour = temp; //aggiorno l'ora precedente per controllare in futuro se sarà cambiata
hourView(); //schermata LCD dell'ora corrente
delay(2000); //un po' di delay, so che a voi piace (-cit)
}
if(currentReading < READINGS){
if(intervalSeconds(2)){
addReading();
delay(500);
}
}else{
getAverage();
currentReading = 0;
previousMillisAverage = millis();
}
checkTimes(); //funzione che serve a controllare se in un determinato intervallo ho innaffiato la pianta piu di n volte.
//Serial.println(currentReading);
if(checkButton()){
displayWatering();
annaffia();
delay(2000);
}
if(intervalSeconds()){
displayDefault();
//########################### BIG BRAIN STUFF ###########################
if(soilMoisturePercent < WET_VALUE){
if(wetTimes < INTERVAL_WETS_TIMES){ //se nell'ultimo intervallo ho innaffiato la pianta meno di n volte.
wetTimes +=1; //incremento le volte che ho innaffiato la pianta
annaffia();
}
}
//########################### BIG BRAIN STUFF ###########################
//delay(1000);
delay(250);
}
}
/*
brightness:
low: 25, 50
medium: 75,100
High: 100>
*/
/*
* Riceve un valore da 0 a 4 dipendente dal momento della giornata
* oppure in modalità override
*/
void setBrightness(int dayTime){
switch(dayTime){
//notte
case 0:
analogWrite(brightnessPin,10);
break;
//mattina
case 1:
analogWrite(brightnessPin,50);
break;
//mezzogiorno
case 2:
analogWrite(brightnessPin,160);
break;
//pomeriggio
case 3:
analogWrite(brightnessPin,100);
break;
//sera
case 4:
analogWrite(brightnessPin,50);
break;
//qualcosa è andato storto
default:
analogWrite(brightnessPin,50); //nel caso la luminosità è quella della mattina (media)
break;
}
}
/**
* Ritorna:
* 0 se è notte
* 1 se è mattina
* 2 se è mezzogiorno
* 3 se è pomeriggio
* 4 se è sera
*
* Periodi:
* 23 - 8 notte
* 8 - 12 mattina
* 12 - 15 mezzogiorno
* 15 - 19 pomeriggio
* 19 - 23 sera
*/
int whatTimeItIs(){
int hour = getHours();
//mattina
if(hour == 8 || hour == 9 ||hour == 10 ||hour == 11){
return 1;
}
if(hour == 12 || hour == 13 || hour == 14){
return 2;
}
if(hour == 15 || hour == 16 || hour == 17 || hour == 18){
return 3;
}
if(hour == 19 || hour == 20 || hour == 21 || hour == 22 || hour == 23){
return 4;
}
if(hour == 24 || hour == 1 || hour == 2 || hour == 3 || hour == 4 || hour == 5 || hour == 6 || hour == 7){
return 0;
}
return 1; //non necessario
}
void printLCD(int value){
lcd.clear();
lcd.setCursor(0,0);
lcd.print(value);
}
//########################### SCHERMATA DI DEFAULT ###########################
void displayDefault(){
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Hum attuale: ");
lcd.print(soilMoisturePercent);
lcd.print("%");
lcd.setCursor(0,1);
lcd.print("Media: ");
lcd.print(average);
lcd.print("%");
lcd.print(" ");
unsigned long tempMS = millis() - previousMillisAverage;
//se i millisecondi passati dall'ultima media sono inferiori al minuto, vengono mostrati i secondi,
//senò vengono mostrati i minuti.
if(tempMS < 60000){
lcd.print((millis() - previousMillisAverage)/1000);
lcd.print("s");
}else{
lcd.print((millis() - previousMillisAverage)/60000);
lcd.print("m");
}
}
//########################### SCHERMATA DI DEFAULT ###########################
//########################### SCHERMATA PER INNAFFIARE ###########################
void displayWatering(){
lcd.clear();
lcd.setCursor(0,0);
lcd.print("!! OVERRIDE !!");
lcd.setCursor(0,1);
lcd.print("Innaffio");
}
//########################### SCHERMATA CHE MOSTRA L'ORA CORRENTE ###########################
/*
*Mostra sull'lcd che momento della giornata è
* e l'ora corrente.
*/
void hourView(){
lcd.clear();
lcd.setCursor(0,0);
switch(whatTimeItIs()){
//notte
case 0:
lcd.print("E' notte!");
break;
//mattina
case 1:
lcd.print("E' mattina!");
break;
//mezzogiorno
case 2:
lcd.print("E' mezzogiorno!");
break;
//pomeriggio
case 3:
lcd.print("E' pomeriggio!");
break;
//sera
case 4:
lcd.print("E' sera!");
break;
//qualcosa è andato storto
default:
lcd.print("!! ERRORE !!");
break;
}
lcd.setCursor(0,1);
lcd.print("Ora: ");
lcd.print(rtc.getTimeStr());
}
bool checkButton(){
// read the state of the pushbutton value
buttonState = digitalRead(buttonPin);
// check if pushbutton is pressed. if it is, the
// buttonState is HIGH
if (buttonState == HIGH) {
return true;
} else {
return false;
}
}
void addReading(){
lastReadings[currentReading] = soilMoisturePercent;
Serial.print("Current read: ");
Serial.println(soilMoisturePercent);
currentReading+=1;
}
void getAverage(){
int temp=0;
for(int i = 0; i<READINGS; i++){
temp += lastReadings[i];
}
average=temp/READINGS;
}
void checkTimes(){
unsigned long mills=millis();
if(mills-currentMillis >= DELAY_IN_MILLISECONDS){
currentMillis=mills;
wetTimes=0; //inizializzo le volte che ho innaffiato dato che il periodo è passato
}
}
int getMinutes(){
String timeString = rtc.getTimeStr(); //returns 50
return (timeString.substring(3,5)).toInt();
}
int getHours(){
String timeString = rtc.getTimeStr(); //returns 11
return (timeString.substring(0,2)).toInt();
}
int getSeconds(){
String timeString = rtc.getTimeStr(); //returns 11:50:55
return (timeString.substring(6,8)).toInt();
}
/*The function returns true if the time interval has passed
* returns false if it didn't pass
* There must be a delay after this (if it returns true)*/
bool intervalSeconds(){
if(getSeconds()%INTERVAL_SECONDS == 0){
//delay(1000);
return true;
}
return false;
}
bool intervalSeconds(int seconds){
if(getSeconds()%seconds == 0){
//delay(1000);
return true;
}
return false;
}
/*The function returns true if the time interval has passed
* returns false if it didn't pass
* There must be a delay after this (if it returns true)*/
bool intervalMinutes(){
if(getMinutes()%INTERVAL_MINUTES == 0){
//delay(1000);
return true;
}
return false;
}
void readMoisturePercent(){
soilMoisture=analogRead(moisturePin);
soilMoisturePercent=map(soilMoisture,airValue,waterValue,0,100);
if(soilMoisturePercent>100){
soilMoisturePercent=100;
}else if(soilMoisturePercent<0){
soilMoisturePercent=0;
}
}
//Waters the plant
void annaffia(){
digitalWrite(relayPin,LOW);
delay(1000);
digitalWrite(relayPin,HIGH);
delay(1000);
}
//Sets the time for the real time clock
void setTime(){
String timeString = __TIME__ ;
int hourInt = (timeString.substring(0,2)).toInt();
int minuteInt = (timeString.substring(3,5)).toInt();
int secondInt = (timeString.substring(6,8)).toInt();
String dayString = __DATE__;
String monthString = (dayString.substring(0,3));
int dayInt = (dayString.substring(4,6).toInt());
int yearInt = (dayString.substring(7,11).toInt());
rtc.setTime(hourInt, minuteInt, secondInt);
rtc.setDate(dayInt, getMonthIndex(monthString), yearInt);
}
//JanFebMarAprMayJunJulAugSepOctNovDec
int getMonthIndex(String month){
if(month.equals("Jan")){
return 1;
}
if(month.equals("Feb")){
return 2;
}
if(month.equals("Mar")){
return 3;
}
if(month.equals("Apr")){
return 4;
}
if(month.equals("May")){
return 5;
}
if(month.equals("Jun")){
return 6;
}
if(month.equals("Jul")){
return 7;
}
if(month.equals("Aug")){
return 8;
}
if(month.equals("Sep")){
return 9;
}
if(month.equals("Oct")){
return 10;
}
if(month.equals("Nov")){
return 11;
}
if(month.equals("Dec")){
return 12;
}
return 1;
}