-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharduino_code.ino
309 lines (279 loc) · 6.94 KB
/
arduino_code.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
// FINALE ROBOTICS PROJECT:
// Submitted By:
// Ran Ben Melech , ID: 204296792
// Tamar Giladi , ID: 207491549
#include <DHT.h> // lib for digital tmp and humidity sensor
#define Type DHT11
#define RELAY 9
int a;
#include <SoftwareSerial.h>
SoftwareSerial MyBlue(6,7); // RX | TX
//define step motor connections and speed to arduino
const int s1 = 10;
const int s2 = 11;
const int s3 = 12;
const int s4 = 13;
int motorSpeed = 1500; // motor speed between 1000-4000. small number is bigger speed
//a function that opens the main tap of the system
void openTap()
{
int watring_time=10000;
digitalWrite(RELAY, HIGH);
delay(watring_time);
digitalWrite(RELAY, LOW);
closeLeftTap();
return;
}
//step motor
void motorOff(){ // turn off motor
digitalWrite(s1, LOW);
digitalWrite(s2, LOW);
digitalWrite(s3, LOW);
digitalWrite(s4, LOW);
}
void back(){ // function to move 1 step back
digitalWrite(s1, HIGH);
digitalWrite(s2, LOW);
digitalWrite(s3, LOW);
digitalWrite(s4, LOW);
delayMicroseconds(motorSpeed);
digitalWrite(s1, HIGH);
digitalWrite(s2, HIGH);
digitalWrite(s3, LOW);
digitalWrite(s4, LOW);
delayMicroseconds(motorSpeed);
digitalWrite(s1, LOW);
digitalWrite(s2, HIGH);
digitalWrite(s3, LOW);
digitalWrite(s4, LOW);
delayMicroseconds(motorSpeed);
digitalWrite(s1, LOW);
digitalWrite(s2, HIGH);
digitalWrite(s3, HIGH);
digitalWrite(s4, LOW);
delayMicroseconds(motorSpeed);
digitalWrite(s1, LOW);
digitalWrite(s2, LOW);
digitalWrite(s3, HIGH);
digitalWrite(s4, LOW);
delayMicroseconds(motorSpeed);
digitalWrite(s1, LOW);
digitalWrite(s2, LOW);
digitalWrite(s3, HIGH);
digitalWrite(s4, HIGH);
delayMicroseconds(motorSpeed);
digitalWrite(s1, LOW);
digitalWrite(s2, LOW);
digitalWrite(s3, LOW);
digitalWrite(s4, HIGH);
delayMicroseconds(motorSpeed);
digitalWrite(s1, HIGH);
digitalWrite(s2, LOW);
digitalWrite(s3, LOW);
digitalWrite(s4, HIGH);
delayMicroseconds(motorSpeed);
}
void forward(){ // function to move 1 step forward
digitalWrite(s1, HIGH);
digitalWrite(s2, LOW);
digitalWrite(s3, LOW);
digitalWrite(s4, HIGH);
delayMicroseconds(motorSpeed);
digitalWrite(s1, LOW);
digitalWrite(s2, LOW);
digitalWrite(s3, LOW);
digitalWrite(s4, HIGH);
delayMicroseconds(motorSpeed);
digitalWrite(s1, LOW);
digitalWrite(s2, LOW);
digitalWrite(s3, HIGH);
digitalWrite(s4, HIGH);
delayMicroseconds(motorSpeed);
digitalWrite(s1, LOW);
digitalWrite(s2, LOW);
digitalWrite(s3, HIGH);
digitalWrite(s4, LOW);
delayMicroseconds(motorSpeed);
digitalWrite(s1, LOW);
digitalWrite(s2, HIGH);
digitalWrite(s3, HIGH);
digitalWrite(s4, LOW);
delayMicroseconds(motorSpeed);
digitalWrite(s1, LOW);
digitalWrite(s2, HIGH);
digitalWrite(s3, LOW);
digitalWrite(s4, LOW);
delayMicroseconds(motorSpeed);
digitalWrite(s1, HIGH);
digitalWrite(s2, HIGH);
digitalWrite(s3, LOW);
digitalWrite(s4, LOW);
delayMicroseconds(motorSpeed);
digitalWrite(s1, HIGH);
digitalWrite(s2, LOW);
digitalWrite(s3, LOW);
digitalWrite(s4, LOW);
delayMicroseconds(motorSpeed);
}
//using step motor back and forwerd to open and close the left tap
void openLeftTap()
{
for (int i=0; i<=150; i++){ // go forward one circle, 509 steps
forward();
}
return;
}
void closeLeftTap()
{
for (int i=0; i<=150; i++){ // go back one circle.
back();
}
return;
}
int day_or_night1(int light_sensor)
{
if(light_sensor<300)
{
return(0);
}
else
{
return (1);
}
}
//a function that will dictate if and which tap to open
int checkStatus(int leftHumidity,int rightHumdity)
{
//res possible outcomes:
//0 - dont open taps
//1 - open left tap
//2 - open both taps
int res=0;
//sensor1=left tap
//sensor2=right tap
if (leftHumidity<300)
{
res=2;
}
else if (rightHumdity<300)
{
res=1;
}
return (res);
}
//declaring varibles that we will use in loop or setup
int sensePin=5;//digital temp and humidity sensor pin
DHT HT(sensePin,Type);
float humidity;
float tempC;
float tempF;
int day_or_night;
float res[5];// array that contains all of the sensor reading values
int res1=0;
int leftRes=0;
String tempe;
String humidity1;
char *sensorName[]={"humidity:","temperature:","first ground moisture:","second ground moisture:","light:"};
int sensorPin = A0; // input pin for LDR
int sensorPin1 = A1;//input pin for first ground humidity sensor
int sensorPin2=A2;//input pin for second ground humidity sensor
int sensorValue = 0; // variable to store the value coming from the sensor
int sensorValue1=0;
int sensorValue2 = 0;
void getValuesToArr(DHT HT,int sensorPin,int sensorPin1,int sensorPin2,float res[])
{
humidity=HT.readHumidity();
tempC=HT.readTemperature();
sensorValue = analogRead(sensorPin); // read the value from the sensor
sensorValue1 = analogRead(sensorPin1); // read the value from the sensor
sensorValue2=analogRead(sensorPin2);
res[0]=humidity;
res[1]=tempC;
res[2]=sensorValue1;//first ground moisture sensor
res[3]=sensorValue2;//second ground moisture sensor
res[4]=sensorValue;//light sensor
return;
}
void setup()
{
Serial.begin(9600); //sets serial port for communication
MyBlue.begin(9600);
pinMode(RELAY, OUTPUT);
HT.begin();
delay(500);
pinMode(s1, OUTPUT);
pinMode(s2, OUTPUT);
pinMode(s3, OUTPUT);
pinMode(s4, OUTPUT);
}
void loop()
{
humidity=HT.readHumidity();
tempC=HT.readTemperature();
tempe=String(tempC);
humidity1=String(humidity);
sensorValue = analogRead(sensorPin); // read the value from the sensor
sensorValue1=analogRead(sensorPin1);
sensorValue2=analogRead(sensorPin2);
delay(5000);
getValuesToArr(HT,sensorPin,sensorPin1,sensorPin2,res);
day_or_night=day_or_night1(sensorValue);
if (day_or_night==0)
{
res1=checkStatus(sensorValue1,sensorValue2);
if (res1==2)
{
openLeftTap();
delay(5000);
openTap();
closeLeftTap();
}
else if (res1==1)
{
openTap();
delay(5000);
}
}
else
{
res1=checkStatus(sensorValue1,sensorValue2);
}
int i=0;
if (MyBlue.available())
{
String toSend=(String(humidity) + ',' + String(tempC)+ ',');
a=MyBlue.read();
a=int (a);
char str[12];
sprintf(str, "%f", humidity);
char str1[12];
sprintf(str1, "%f", tempC);
//MyBlue.write(tempC); //Serial.write will send only one byte at a time
//MyBlue.write(humidity); //Serial.write will send only one byte at a time
delay(1000);
Serial.print(tempC);
//MyBlue.write(tempC);
delay(100);
MyBlue.print(toSend);
Serial.print(humidity);
delay(400);
//Serial.print(str);
//Serial.print(str1);
if(a==2)
{
openLeftTap();
openTap();
closeLeftTap();
}
if (a==1)
{
closeLeftTap();
openTap();
}
}
for (i=0;i<5;i++)
{
Serial.print(sensorName[i]);
Serial.println(res[i]);
}
}