-
Notifications
You must be signed in to change notification settings - Fork 2
/
PathFinder-Refurb.ino
479 lines (355 loc) · 11 KB
/
PathFinder-Refurb.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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>
// Specify Sonar Sensor Pins and Variable
const int trigPin = 22 ; // Sonar sensor pin Trigger
const int echoPin = 23 ; // Sonar sensor pin Echo
long duration ; // Sonar Duration Variable
int objectDistance;
// Specify Path Sensor Variables and Pins
const int pathS1 = 52;
const int pathS2 = 50;
const int pathS3 = 48;
const int pathS4 = 46;
const int pathS5 = 44;
const int touchPin = 42; // Touch Sensor on the 5 Channel Line Tracker Sensor.
int S1, S2, S3, S4, S5, S6 ;
// Specify Pins For Motor 1
const int Motor1A = 2;
const int Motor1B = 3;
// Specify Pins For Motor 2
const int Motor2A = 5;
const int Motor2B = 4;
// Specify PWM Pins
const int PWM1 = 6; // PWM Pin for Motor 1
const int PWM2 = 7; // PWM Pin for Motor2
// Specify LED Pins
const int ledLeft = 31; // signals as the robot turns Left....
const int ledRight = 30; // signals as the robot turns Right....
// Specify Buzzer Pin
const int buzzerPin = 10; //hooting
// Initialize loop_counter variable
int loop_counter = 0;
#define I2C_ADDR 0x3F //Define I2C Address
#define BACKLIGHT_PIN 3
#define En_pin 2
#define Rw_pin 1
#define Rs_pin 0
#define D4_pin 4
#define D5_pin 5
#define D6_pin 6
#define D7_pin 7
LiquidCrystal_I2C lcd(I2C_ADDR, En_pin, Rw_pin, Rs_pin, D4_pin, D5_pin, D6_pin, D7_pin);
void setup() {
Serial.begin(9600); // connect serial
pinMode(Motor1A, OUTPUT);
pinMode(Motor1B, OUTPUT);
pinMode(Motor2A, OUTPUT);
pinMode(Motor2B, OUTPUT);
pinMode(PWM1, OUTPUT);
pinMode(PWM2, OUTPUT);
pinMode( pathS1 , INPUT);
pinMode( pathS2 , INPUT);
pinMode( pathS3 , INPUT);
pinMode( pathS4 , INPUT);
pinMode( pathS5 , INPUT);
pinMode( touchPin , INPUT);
pinMode( ledLeft , OUTPUT);
pinMode( ledRight , OUTPUT);
pinMode( buzzerPin , OUTPUT);
digitalWrite( ledLeft , LOW );
digitalWrite( ledRight , LOW );
digitalWrite( buzzerPin , LOW );
digitalWrite( Motor1A, LOW );
digitalWrite( Motor1B, LOW );
digitalWrite( Motor2A, LOW );
digitalWrite( Motor2B, LOW );
pinMode( trigPin, OUTPUT );
pinMode( echoPin, INPUT );
lcd.begin (20, 4);
//Switch on the backlight
lcd.setBacklightPin(BACKLIGHT_PIN, POSITIVE);
lcd.setBacklight(HIGH);
//goto first column (column 0) and first line (Line 0)
lcd.setCursor(0, 0);
lcd.print(" -RIARA UNIVERSITY- ");
//goto first column (column 0) and second line (line 1)
lcd.setCursor(0, 1);
lcd.print(" Robotics Contest ");
lcd.setCursor(0, 2);
lcd.print(" - - - - -- - - - - ");
lcd.setCursor(0, 3);
delay(5000);
lcd.setCursor(0, 2);
lcd.print(" -ROBOT NAVIGATING- ");
analogWrite(buzzerPin, 0);
searchPath();
}
void loop() {
checkObjectDistance();
while ( objectDistance > 20 ) {
digitalWrite(ledRight, LOW); // Switch off signals
digitalWrite(ledLeft, LOW);
readpathsensor();
checkObjectDistance();
// Robot moving on the path - While there is no object on the path.
while (S1 == 0 && S2 == 1 && S3 == 1 && S4 == 1 && S5 == 0 && ( objectDistance > 20 ))
{
digitalWrite(ledRight, LOW);
digitalWrite(ledLeft, LOW);
analogWrite(PWM1, 200);
analogWrite(PWM2, 180);
lcd.setCursor(0,1);
lcd.print(" ");
lcd.setCursor(0, 2);
lcd.print(" ON PATH ");
lcd.setCursor(0, 3);
lcd.print(" ");
digitalWrite(Motor1A, LOW); //
digitalWrite(Motor1B, HIGH); //
digitalWrite(Motor2A, LOW); //
digitalWrite(Motor2B, HIGH); //
readpathsensor();
checkObjectDistance();
}
// Robot deviating to the left
while ((S1 == 0 && S2 == 1 && S3 == 1 && S4 == 1 && S5 == 1 ) || (S1 == 0 && S2 == 0 && S3 == 1 && S4 == 1 && S5 == 1 ) || (S1 == 0 && S2 == 0 && S3 == 0 && S4 == 1 && S5 == 1 ) || (S1 == 0 && S2 == 0 && S3 == 0 && S4 == 0 && S5 == 1 ) && ( objectDistance > 20 ))
{
digitalWrite(ledRight, HIGH);
digitalWrite(ledLeft, LOW);
analogWrite(PWM1, 80);
analogWrite(PWM2, 145);
lcd.setCursor(0, 2);
lcd.print(" Deviating Left ");
digitalWrite(Motor1A, LOW); //
digitalWrite(Motor1B, HIGH); //
digitalWrite(Motor2A, LOW); //
digitalWrite(Motor2B, HIGH); //
readpathsensor();
checkObjectDistance();
}
// Robot deviating to the Right
while ( ( S1 == 1 && S2 == 1 && S3 == 1 && S4 == 1 && S5 == 0 ) || ( S1 == 1 && S2 == 1 && S3 == 1 && S4 == 0 && S5 == 0 ) || ( S1 == 1 && S2 == 1 && S3 == 0 && S4 == 0 && S5 == 0 ) || ( S1 == 1 && S2 == 0 && S3 == 0 && S4 == 0 && S5 == 0 ) && ( objectDistance > 20 ))
{
digitalWrite(ledRight, LOW );
digitalWrite(ledLeft, HIGH);
analogWrite(PWM1, 130);
analogWrite(PWM2, 60);
lcd.setCursor(0, 2);
lcd.print(" Deviating Right ");
digitalWrite(Motor1A, LOW); //
digitalWrite(Motor1B, HIGH); //
digitalWrite(Motor2A, LOW); //
digitalWrite(Motor2B, HIGH); //
readpathsensor();
checkObjectDistance();
}
while ((S1 == 0 && S2 == 0 && S3 == 0 && S4 == 0 && S5 == 0) && (objectDistance > 20)) {
// All Black Go forward || if loop_counter == 2 do a u-turn
digitalWrite(ledRight, LOW);
digitalWrite(ledLeft, LOW);
lcd.setCursor(0, 2);
lcd.print(" -- FINDING PATH-- ");
digitalWrite(Motor1A, HIGH);
digitalWrite(Motor1B, LOW);
digitalWrite(Motor2A, HIGH);
digitalWrite(Motor2B, LOW);
readpathsensor();
checkObjectDistance();
}
// No path available - The robot should move foward
while ((S1 == 1 && S2 == 1 && S3 == 1 && S4 == 1 && S5 == 1) && (objectDistance > 20)) {
if (loop_counter < 2) {
digitalWrite(ledRight, LOW);
digitalWrite(ledLeft, LOW);
lcd.setCursor(0, 2);
lcd.print(" -PATH NOT DEFINED- ");
digitalWrite(Motor1A, HIGH);
digitalWrite(Motor1B, LOW);
digitalWrite(Motor2A, HIGH);
digitalWrite(Motor2B, LOW);
readpathsensor();
checkObjectDistance();
loop_counter += 1;
} else {
robotTurnRight();
}
}
}
objectFound();
}
void checkObjectDistance() {
digitalWrite(trigPin, LOW);
delayMicroseconds(5);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);
long cm = (duration / 2) / 29.1;
Serial.print(cm);
Serial.print("cm");
Serial.println();
objectDistance = cm;
}
void hoot() {
tone(buzzerPin, 400, 1000);
delay(300);
tone(buzzerPin, 450, 1000);
delay(300);
tone(buzzerPin, 500, 1500);
noTone(buzzerPin);
}
void readpathsensor() {
S1 = digitalRead(pathS1);
S2 = digitalRead(pathS2);
S3 = digitalRead(pathS3);
S4 = digitalRead(pathS4);
S5 = digitalRead(pathS5);
}
void brake() {
digitalWrite(Motor1A, HIGH);
digitalWrite(Motor1B, HIGH);
digitalWrite(Motor2A, HIGH);
digitalWrite(Motor2B, HIGH);
}
void robotTurnRight() {
analogWrite(PWM1, 160);
analogWrite(PWM2, 160);
digitalWrite(Motor1A, HIGH); //
digitalWrite(Motor1B, LOW); //
digitalWrite(Motor2A, LOW); //
digitalWrite(Motor2B, HIGH); //
delay(200);
readpathsensor();
while ( S1 == 0 && S2 == 0 && S3 == 0 && S4 == 0 && S5 == 0 ) {
digitalWrite(ledRight, LOW );
digitalWrite(ledLeft, HIGH);
analogWrite(PWM1, 180);
analogWrite(PWM2, 200);
lcd.setCursor(0, 2);
lcd.print(" Turning Right ");
digitalWrite(Motor1A, HIGH); //
digitalWrite(Motor1B, LOW); //
digitalWrite(Motor2A, LOW); //
digitalWrite(Motor2B, HIGH); //
readpathsensor();
}
delay(200);
digitalWrite(Motor1A, LOW); //
digitalWrite(Motor1B, LOW); //
digitalWrite(Motor2A, LOW); //
digitalWrite(Motor2B, LOW); //
delay(100);
return;
}
void searchPath() {
lcd.setCursor(0, 1);
lcd.print("Robot searching path");
lcd.setCursor(0, 2);
lcd.print(" .................. ");
lcd.setCursor(0, 3);
lcd.print(" - PLEASE WAIT. - ");
readpathsensor();
checkObjectDistance();
if (S1 == 0 && S2 == 1 && S3 == 1 && S4 == 1 && S5 == 0) {
brake();
readpathsensor();
checkObjectDistance();
return;
}
while (objectDistance > 20) {
readpathsensor();
checkObjectDistance();
if (S1 == 0 && S2 == 0 && S3 == 0 && S4 == 0 && S5 == 0 && (objectDistance > 20)) {
analogWrite(PWM1, 160);
analogWrite(PWM2, 160);
// Move Motors Forward
digitalWrite(Motor1A, LOW);
digitalWrite(Motor1B, HIGH);
digitalWrite(Motor2A, LOW);
digitalWrite(Motor2B, HIGH);
readpathsensor();
checkObjectDistance();
}
if (S1 == 1 && S2 == 0 && S3 == 0 && S4 == 0 && S5 == 0 && (objectDistance > 20) ) {
checkObjectDistance();
while ( S3 != 1 ) {
analogWrite(PWM1, 160);
analogWrite(PWM2, 160);
digitalWrite(Motor1A, LOW); //
digitalWrite(Motor1B, HIGH); //
digitalWrite(Motor2A, LOW); //
digitalWrite(Motor2B, HIGH); //
readpathsensor();
checkObjectDistance();
}
brake();
return;
}
if (S1 == 0 && S2 == 0 && S3 == 0 && S4 == 0 && S5 == 1 && (objectDistance > 20))
{
checkObjectDistance();
while ( S3 != 1 )
{
analogWrite(PWM1, 160);
analogWrite(PWM2, 160);
digitalWrite(Motor1A, LOW);
digitalWrite(Motor1B, HIGH);
digitalWrite(Motor2A, LOW);
digitalWrite(Motor2B, HIGH);
readpathsensor();
checkObjectDistance();
}
brake();
return;
}
if (S1 == 1 && S2 == 1 && S3 == 1 && S4 == 1 && S5 == 1 && (objectDistance > 20) )
{
brake();
robotTurnRight();
readpathsensor();
checkObjectDistance();
return;
}
if (S1 == 0 && S2 == 1 && S3 == 1 && S4 == 1 && S5 == 0 && (objectDistance > 20) )
{
brake();
readpathsensor();
checkObjectDistance();
return;
}
}
while (objectDistance < 19) {
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.setCursor(0, 2);
lcd.print("-! OBJECT ON PATH !-");
lcd.setCursor(0, 3);
lcd.print(" ");
brake();
hoot();
readpathsensor();
checkObjectDistance();
robotTurnRight();
delay(200);
}
}
void objectFound() {
// If the Robot finds and oject on the path, It should stop moving, diplay, and hoot
// Then should Resume path navigation once the path is cleared.
checkObjectDistance();
while (objectDistance < 19) {
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.setCursor(0, 2);
lcd.print("-! OBJECT ON PATH !-");
lcd.setCursor(0, 3);
lcd.print(" ");
brake();
hoot();
checkObjectDistance();
delay(200);
}
return;
}