-
Notifications
You must be signed in to change notification settings - Fork 0
/
irrigation.ino
140 lines (127 loc) · 2.13 KB
/
irrigation.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
#include <dht.h>
#define a_dht A0
#define a_soil A1
#define relay 2
#define d_r 13
#define d_o 12
#define d_g 11
dht DHT;
int humidity;
int temp;
int moisture;
int motor = 0;
int ssinit = 2;
int soil = 0;
void setup() {
//setup board
Serial.begin(9600);
delay(1000);
//test boot
Serial.print("Irrigation System\n");
delay(2000);
//led setup
pinMode(d_r,OUTPUT);
pinMode(d_o,OUTPUT);
pinMode(d_g,OUTPUT);
//relay setup
pinMode(relay, OUTPUT);
}
//default loop
void loop() {
if(moisture < 5) {
soil = 0;
loop2();
pump0();
if(ssinit == 2) {
ssinit = 1;
Serial.println("Initializing soil moisture sensor");
} else if(ssinit == 1){
ssinit = 0;
Serial.println("Soil not found or super dry soil found");
}
r1();o1();g1();
delay(1000);
r0();o0();g0();
delay(1000);
} else {
soil = 1;
ssinit = 1;
loop1();
loop2();
if(motor == 0) {
if(moisture >= 5 && moisture <= 60) {
motor = 1;
g1();
pump1();
}
} else {
if(moisture >= 80) {
motor = 0;
pump0();
g0();
}
}
delay(1000);
}
}
//dht loop
void loop1() {
r1();
DHT.read11(a_dht);
humidity = DHT.humidity;
temp = DHT.temperature;
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.print("%\n");
Serial.print("Temperature: ");
Serial.print(temp);
Serial.print("C\n");
r0();
}
//soil loop
void loop2() {
o1();
moisture = analogRead(a_soil);
moisture = map(moisture,1023,375,0,100);
if(soil != 0) {
Serial.print("Moisture: ");
Serial.print(moisture);
Serial.print("%\n");
}
o0();
}
//water pump
void pump0() {
if(soil != 0) {
Serial.println("Pump: off");
}
digitalWrite(relay, HIGH);
}
void pump1() {
if(soil == 0) {
pump0();
}
Serial.println("Pump: on");
digitalWrite(relay, LOW);
}
//red led
void r1() {
digitalWrite(d_r,HIGH);
}
void r0() {
digitalWrite(d_r,LOW);
}
//orange led
void o1() {
digitalWrite(d_o,HIGH);
}
void o0() {
digitalWrite(d_o,LOW);
}
//green led
void g1() {
digitalWrite(d_g,HIGH);
}
void g0() {
digitalWrite(d_g,LOW);
}