-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode_for__bot.ino
279 lines (238 loc) · 5.51 KB
/
code_for__bot.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
int relay1=6;
int relay2=9;
int relay3=5;
int relay4=3;
int echo_front=7;
int trig_front=8;
int echo_left=10;
int trig_left=11;
int echo_right=12;
int trig_right=13;
int pt=0;
long time_front,distance_front,minimum_front,distance_left,minimum_left,time_left,distance_right,minimum_right,time_right;
void setup()
{
Serial.begin(9600);
pinMode(trig_front,OUTPUT);
pinMode(echo_front,INPUT);
pinMode(trig_left,OUTPUT);
pinMode(echo_left,INPUT);
pinMode(trig_right,OUTPUT);
pinMode(echo_right,INPUT);
pinMode(relay1,OUTPUT);
pinMode(relay2,OUTPUT);
pinMode(relay3,OUTPUT);
pinMode(relay4,OUTPUT);
}
void loop()
{
sensor_front();
sensor_left();
sensor_right();
if(distance_front<=minimum_front)
{
stop_robot();
Serial.println("Robot Stop");
if((distance_right<=minimum_right)&&(!(distance_left<=minimum_left)))
{
reverse_robot();
Serial.println("Robot Back-1");
delay(500);
turn_left();
pt=-1;
Serial.println("Robot Left1");
forward_robot();
Serial.println("Robot Forward1");
}
else if( (distance_left<=minimum_left)&&(!(distance_right<=minimum_right)))
{
reverse_robot();
Serial.println("Robot Back0");
delay(500);
turn_right();
pt=1;
Serial.println("Robot Right1");
forward_robot();
Serial.println("Robot Forward11");
}
else if((distance_left>=minimum_right)&&(distance_right>=minimum_left))
{
reverse_robot();
Serial.println("Robot Back1");
delay(500);
turn_left();
pt=-1;
Serial.println("Robot Left2");
forward_robot();
Serial.println("Robot Forward111");
}
else
{
do
{
reverse_robot();
Serial.println("Robot Back2");
sensor_front();
sensor_left();
sensor_right();
}while((distance_left<=minimum_left)&&(distance_right<=minimum_right));
stop_robot();
goto deadend;
}
}
else if((distance_right<=minimum_right)&&(!(distance_left<=minimum_left)))
{
reverse_robot();
Serial.println("Robot Back22");
turn_left();
pt=-1;
Serial.println("Robot Left4");
forward_robot();
Serial.println("Robot Forward1111");
}
else if((distance_left<=minimum_left)&&(!(distance_right<=minimum_right)))
{
reverse_robot();
turn_right();
pt=1;
Serial.println("Robot Right2");
forward_robot();
Serial.println("Robot Forward11111");
}
else if((distance_left>=minimum_left)&&(distance_right>=minimum_right))
{
reverse_robot();
turn_left();
forward_robot();
pt=-1;
Serial.println("Robot Left5");
forward_robot();
Serial.println("Robot Forward1.6");
}
else
{
forward_robot();
Serial.println("Robot Forward2");
}
if(0)
deadend:
{
if((distance_left>=minimum_left)&&(distance_right>=minimum_right))
{
if(pt==-1)
{
turn_left();
pt=-1;
Serial.println("Robot Left6");
forward_robot();
Serial.println("Robot Forward3");
}
else if(pt==1)
{
turn_right();
pt=1;
Serial.println("Robot Right3");
forward_robot();
Serial.println("Robot Forward4");
}
}
else if((distance_left>=minimum_left))
{
turn_left();
pt=-1;
Serial.println("Robot Left7");
forward_robot();
Serial.println("Robot Forward5");
}
else if((distance_right>=minimum_right))
{
turn_right();
pt=1;
Serial.println("Robot Right4");
forward_robot();
Serial.println("Robot Forward6");
}
}
}
void sensor_front()
{
digitalWrite(trig_front, LOW);
delayMicroseconds(2);
digitalWrite(trig_front, HIGH);
delayMicroseconds(20);
digitalWrite(trig_front, LOW);
time_front=pulseIn(echo_front,HIGH);
distance_front=time_front/58.2;
Serial.println("distance front=");
Serial.println(distance_front);
delayMicroseconds(10);
minimum_front=15;
}
void sensor_left()
{
digitalWrite(trig_left, LOW);
delayMicroseconds(2);
digitalWrite(trig_left, HIGH);
delayMicroseconds(20);
digitalWrite(trig_left, LOW);
time_left=pulseIn(echo_left,HIGH);
distance_left=time_left/58.2;
Serial.println("distance left=");
Serial.println(distance_left);
delayMicroseconds(10);
minimum_left=15;
}
void sensor_right()
{
digitalWrite(trig_right, LOW);
delayMicroseconds(2);
digitalWrite(trig_right, HIGH);
delayMicroseconds(20);
digitalWrite(trig_right, LOW);
time_right=pulseIn(echo_right,HIGH);
distance_right=time_right/58.2;
Serial.println("distance right=");
Serial.println(distance_right);
delayMicroseconds(10);
minimum_right=15;
}
void forward_robot()
{
analogWrite(relay1,0);
analogWrite(relay2,200);
analogWrite(relay3,0);
analogWrite(relay4,200);
delay(1000);
}
void reverse_robot()
{
analogWrite(relay1,200);
analogWrite(relay2,0);
analogWrite(relay3,200);
analogWrite(relay4,0);
delay(500);
}
void stop_robot()
{
analogWrite(relay1,0);
analogWrite(relay2,0);
analogWrite(relay3,0);
analogWrite(relay4,0);
delay(500);
}
void turn_left()
{
analogWrite(relay1,0);
analogWrite(relay2,200);
analogWrite(relay3,200);
analogWrite(relay4,0);
delay(300);
}
void turn_right()
{
analogWrite(relay1,200);
analogWrite(relay2,0);
analogWrite(relay3,0);
analogWrite(relay4,200);
delay(300);
}