-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_code.ino
153 lines (147 loc) · 3.47 KB
/
test_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
int time1,time2,time3;
int distance1, distance2, distance3;
int soil_moisture_pin = A0;
int base_moisture = 300;
void setup()
{
pinMode(2,OUTPUT); // Left Ultra 2 Trig
pinMode(4,INPUT); // Left Ultra 4 Echo
pinMode(5,OUTPUT); // Motor Side 1 PWM (Left Motors)
pinMode(6,OUTPUT); // Motor Side 1 PWM (Left Motors)
pinMode(9,OUTPUT); // Motor Side 2 PWM (Right Motors)
pinMode(10,OUTPUT); // Motor Side 2 PWM (Right Motors)
pinMode(7,OUTPUT); // Center Ultra 7 Trig
pinMode(8,INPUT); // Center Ultra 8 Echo
pinMode(12,OUTPUT); // Right Ultra 12 Trig
pinMode(13,INPUT); // Right Ultra 13 Echo
pinMode(3,OUTPUT); // Soil Moisture Motor
pinMode(11,OUTPUT); // Soil Moisture Motor
pinMode(soil_moisture_pin,INPUT);
Serial.begin(9600);
}
void loop()
{
digitalWrite (2,LOW);
delayMicroseconds(2);
digitalWrite (2,HIGH);
delayMicroseconds(10);
digitalWrite(2,LOW);
time1 = pulseIn (4, HIGH);
distance1 = (time1 * 0.034) / 2;
Serial.print (" Distance1= ");
Serial.println (distance1);
digitalWrite (7,LOW);
delayMicroseconds(2);
digitalWrite (7,HIGH);
delayMicroseconds(10);
digitalWrite(7,LOW);
time2= pulseIn (8, HIGH);
distance2 = (time2 * 0.034) / 2;
Serial.print (" Distance2= ");
Serial.println (distance2);
digitalWrite (12,LOW);
delayMicroseconds(2);
digitalWrite (12,HIGH);
delayMicroseconds(10);
digitalWrite(12,LOW);
time3 = pulseIn (13, HIGH);
distance3 = (time3 * 0.034) / 2;
Serial.print (" Distance3= ");
Serial.println (distance3);
if ((distance1 <= 80) && (distance2 <= 80) && (distance3 <= 80))
{
analogWrite(5,255);
analogWrite(6,0);
analogWrite(9,255);
analogWrite(10,0);
Serial.println("GO BACK");
}
else if ((distance1 <= 80) && (distance2 <= 80))
{
analogWrite(5,0);
analogWrite(6,255);
analogWrite(9,0);
analogWrite(10,10);
Serial.println("RIGHT TURN");
}
else if ((distance2 <= 80) && (distance3 <= 80))
{
analogWrite(5,0);
analogWrite(6,10);
analogWrite(9,0);
analogWrite(10,255);
Serial.println("LEFT TURN");
}
else if (distance1 <= 80)
{
analogWrite(5,0);
analogWrite(6,255);
analogWrite(9,0);
analogWrite(10,10);
Serial.println("RIGHT TURN");
}
else if (distance2 <= 80)
{
analogWrite(5,255);
analogWrite(6,0);
analogWrite(9,255);
analogWrite(10,0);
Serial.println("GO BACK");
}
else if (distance3 <= 80)
{
analogWrite(5,0);
analogWrite(6,10);
analogWrite(9,0);
analogWrite(10,255);
Serial.println("LEFT TURN");
}
else
{
analogWrite(5,0);
analogWrite(6,255);
analogWrite(9,0);
analogWrite(10,255);
Serial.println("ALL HIGH FORWARDS");
}
SM_Motor();
SM_Sensor();
}
void SM_Motor()
{
digitalWrite(11,0);
digitalWrite(3,255);
delay(80);
digitalWrite(11,0);
digitalWrite(3,0);
delay(5000);
digitalWrite(11,255);
digitalWrite(3,0);
delay(100);
digitalWrite(11,0);
digitalWrite(3,0);
delay(5000);
}
void SM_Sensor()
{
int soil_moisture_val = analogRead(soil_moisture_pin);
Serial.print("Moisture = ");
Serial.print(soil_moisture_val);
soil_moisture_val = map(soil_moisture_val,1023,0,0,100);
Serial.print(", Moisture = ");
Serial.print(soil_moisture_val);
Serial.println("%");
if (soil_moisture_val > base_moisture)
{
Serial.print("Good Amount of Moisture");
}
else if (soil_moisture_val < 50)
{
Serial.print("Very Less Amount of Moisture");
}
else
{
Serial.print("Normal Amount of Moisture");
}
delay(1000);
}