-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaze_solver.cpp
576 lines (521 loc) · 11.9 KB
/
maze_solver.cpp
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
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
/*
* maze_solver.cpp (shortest path search algo)
*
* Created: 6/6/2016
* Author: Simran Suresh
*/
char reshortn[10];
char directions[30];
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
// 5V BO - 1300
// 8V BO - 850
// 8V Geared - 650
// 5V geared -DO NOT ATTEMPT
int m1 =0;
int m2 =1;
int m3 =24;
int m4=25;
int l;
int c1;
int c2;
int c3;
int r;
int i,j,flag;
int tdelay=950, fdelay=400;
//~~~~~~~~~~~~~~~Conditions~~~~~~~~~~~~~~
int eosens()
{
readsens();
if(((c1+c2+c3)==1) || ((c1+c2+c3)==2))
return 1;
else
return 0;
}
void forward()
{
digitalWrite(m1,HIGH); //Forward
digitalWrite(m2,LOW);
digitalWrite(m3,HIGH);
digitalWrite(m4,LOW);
//Serial.println("\tMoving FWD");
lcd.print("Moving FWD");
}
void left()
{
digitalWrite(m1,LOW); //Left
digitalWrite(m2,HIGH);
digitalWrite(m3,HIGH);
digitalWrite(m4,LOW);
//Serial.println("\tTaking LEFT");
lcd.print("= left");
}
void smallleft()
{
digitalWrite(m1,LOW); //Left
digitalWrite(m2,LOW);
digitalWrite(m3,HIGH);
digitalWrite(m4,LOW);
//Serial.println("\tTaking LEFT");
lcd.print("= small left");
}
void right()
{
digitalWrite(m1,HIGH); //Right
digitalWrite(m2,LOW);
digitalWrite(m3,LOW);
digitalWrite(m4,HIGH);
//Serial.println("\tTaking RIGHT");
lcd.print("= right");
}
void smallright()
{
digitalWrite(m1,HIGH); //Right
digitalWrite(m2,LOW);
digitalWrite(m3,LOW);
digitalWrite(m4,LOW);
//Serial.println("\tTaking RIGHT");
lcd.print("= right");
}
void halt()
{
digitalWrite(m1,LOW); //STOP
digitalWrite(m2,LOW);
digitalWrite(m3,LOW);
digitalWrite(m4,LOW);
//Serial.println("\tSTOPPED!!!!!!");
lcd.print("= HALT!!!");
}
void readsens()
{
l=digitalRead(A2);
c1=digitalRead(A3);
c2=digitalRead(A4);
c3=digitalRead(A5);
r=digitalRead(A6);
lcd.print(l);
lcd.print("--");
lcd.print(c1);
lcd.print("--");
lcd.print(c2);
lcd.print("--");
lcd.print(c3);
lcd.print("--");
lcd.print(r);
delay (300);
}
void inch()
{
//Serial.println("Inside inch function");
lcd.print("inch function");
digitalWrite(m1,LOW); //STOP
digitalWrite(m2,LOW);
digitalWrite(m3,LOW);
digitalWrite(m4,LOW);
delay(100);
digitalWrite(m1,HIGH); //Forward
digitalWrite(m2,LOW);
digitalWrite(m3,HIGH);
digitalWrite(m4,LOW);
delay(900);
digitalWrite(m1,LOW); //STOP
digitalWrite(m2,LOW);
digitalWrite(m3,LOW);
digitalWrite(m4,LOW);
delay(100);
readsens();
delay(100);
}
void preinch()
{
if((l==1) && (c1==1) || (c3==1) && (r==1))
{
halt();
delay(1000);
forward();
delay(160);
halt();
delay(1000);
readsens();
}
}
void setup()
{
////Serial.begin(38400);
////Serial.println("Initialized");
lcd.begin(16,2);
lcd.print ("Welcome");
delay(500);
lcd.clear();
pinMode(0,OUTPUT);
pinMode(1,OUTPUT);
pinMode(24,OUTPUT);
pinMode(25,OUTPUT);
pinMode(26,INPUT); //l
pinMode(27,INPUT); //c1
pinMode(28,INPUT); //c2
pinMode(29,INPUT); //c3
pinMode(30,INPUT); //r
lcd.print ("initialised");
delay(500);
lcd.clear();
}
void loop()
{
lcd.clear();
readsens();
lcd.clear();
//Serial.println("");
//Serial.println(" | L | C | C | C | R | ");
//Serial.print(" | ");
//Serial.print(l);
//Serial.print(" | ");
//Serial.print(c1);
//Serial.print(" | ");
//Serial.print(c2);
//Serial.print(" | ");
//Serial.print(c3);
//Serial.print(" | ");
//Serial.print(r);
//Serial.print(" | \t");
//Line follower
//Condition for FWD
if((l==0) && (c1==0) && (c2==1) && (c3==0) && (r==0))
{
//Serial.println("\tForward");
lcd.print("Forward");
forward();
}
//Conditions for LEFT
else if(((l==0) && (c1==1) && (c2==0) && (c3==0) && (r==0)) || ((l==0) && (c1==1) && (c2==1) && (c3==0) && (r==0)))
{
//Serial.println("\tLeft");
lcd.print("Left");
smallleft();
}
//Conditions for LEFT
else if(((l==0) && (c1==0) && (c2==0) && (c3==1) && (r==0)) || ((l==0) && (c1==0) && (c2==1) && (c3==1) && (r==0)))
{
//Serial.println("\tRight");
lcd.print("Right");
smallright();
}
//----------------------------------------------------------------------------------------------
else if(((l==1) && (c1==1)) || ((c3==1) && (r==1)))
{
// preinch();
//DECIDE BETWEEN ---- Right only and Straight n right!
if(((l==0) && (c1==0) && (c2==1) && (c3==1) && (r==1)) || ((l==0) && (c1==1) && (c2==1) && (c3==1) && (r==1)))
{
//Serial.println("\tRIGHT ONLY / STRAIGHT AND RIGHT???");
lcd.print("R/StnRT?");
inch();
//Serial.print("Choice = ");
if((l==0) && (c1==0) && (c2==0) && (c3==0) && (r==0))
{
//Serial.println("Right only");
lcd.print("= Right");
right();
delay(tdelay);
}
if((l==0) && (r==0) && (eosens()))
{
//Serial.println("Straight N Right");
lcd.print("= Forward");
directions[i]='S';
i++;
forward();
delay(fdelay);
}
}
//Left and Straight n left
else if(((l==1) && (c1==1) && (c2==1) && (c3==0) && (r==0)) || ((l==1) && (c1==1) && (c2==1) && (c3==1) && (r==0)))
{
//Serial.println("\tLEFT ONLY / STRAIGHT AND LEFT????");
lcd.print("Lt/StnLt ");
inch();
//Serial.print("Choice = ");
lcd.print("C??");
if((l==0) && (c1==0) && (c2==0) && (c3==0) && (r==0))
{
//Serial.println("Left only");
//lcd.print("= Left");
left();
}
if((l==0) && (r==0) && (eosens()))
{
//Serial.println("Straight N left");
lcd.print("= StnLT");
directions[i]='L';
i++;
left();
}
delay(tdelay);
}
//T-int cross end of maze
else if((l==1) && (c1==1) && (c2==1) && (c3==1) && (r==1))
{
//Serial.println("\tT INTERSECTION / END OF MAZE / CLOVERLEAF");
lcd.print("Tint/End/Crs?");
inch();
//Serial.print("Choice = ");
if((l==0) && (r==0) && (eosens()))
{
//Serial.println("CLOVERLEAF");
lcd.print("= crs");
directions[i]='L';
i++;
left();
delay(tdelay);
}
if((l==0) && (c1==0) && (c2==0) && (c3==0) && (r==0))
{
//Serial.println("T-INT");
lcd.print("= tint");
directions[i]='L';
i++;
left();
delay(tdelay);
}
if((l==1) && (c1==1) && (c2==1) && (c3==1) && (r==1))
{
//Serial.println("THE END");
lcd.print("= end");
halt();
delay(2500);
directions[i]='E';
//Serial.println("Maze over!! Existing path is:");
printing(directions);
delay(10000); //10 second window
SPAlgo(directions);
while(1)
{
halt();
}
}
}
}
//Dead end
else if((l==0) && (c1==0) && (c2==0) && (c3==0) && (r==0))
{
//Serial.println("OOPS! I reached a DEADEND - - - TAKING U TURN");
lcd.print("= Uturn");
left();
delay(1800);
directions[i]='U';
i++;
}
}
//~~~~~~ENDOFLOOP~~~~~~~~~~~~~~~~
//PRINTING THE VALUES OF ARRAY
void printing(char prtline[])
{
//Serial.println("");
lcd.clear();
for(i=0;prtline[i]!='E';i++)
{
//Serial.print(prtline[i]);
lcd.print(prtline[i]);
}
}
//~~~~~~SHORTENING PATH ALGO~~~~~~~~~~~
void SPAlgo(char shortn[])
{
//Serial.println("");
for(i=1;shortn[(i+1)]!='E';i++)
{
if(shortn[i]=='U')
{
if((shortn[i-1]=='L') && (shortn[i+1]=='R'))
{
shortn[i-1]='x';
shortn[i]='x';
shortn[i+1]='U';
}
if((shortn[i-1]=='L') && (shortn[i+1]=='S'))
{
shortn[i-1]='x';
shortn[i]='x';
shortn[i+1]='R';
}
if((shortn[i-1]=='R') && (shortn[i+1]=='L'))
{
shortn[i-1]='x';
shortn[i]='x';
shortn[i+1]='U';
}
if((shortn[i-1]=='S') && (shortn[i+1]=='L'))
{
shortn[i-1]='x';
shortn[i]='x';
shortn[i+1]='R';
}
if(shortn[i]=='x')
i++;
if((shortn[i-1]=='S') && (shortn[i+1]=='S'))
{
shortn[i-1]='x';
shortn[i]='x';
shortn[i+1]='U';
}
if((shortn[i-1]=='L') && (shortn[i+1]=='L'))
{
shortn[i-1]='x';
shortn[i]='x';
shortn[i+1]='S';
}
}
}
j=0;
for(i=0;shortn[i]!='E';i++)
{
if(shortn[i]!='x')
{
reshortn[j]=shortn[i];
j++;
}
}
reshortn[j]='E';
//Serial.print("now the path is:");
lcd.print("SPA : ");
printing(shortn);
delay(1000);
for(i=0;reshortn[i]!='E';i++)
{
if(reshortn[i]=='U')
{
flag=1;
}
}
delay(2000);
if(flag==1)
{
j=0;
SPAlgo(reshortn);
flag=0;
}
else
{
//Serial.println("Final path is:");
lcd.clear();
lcd.print("Final =");
printing(reshortn);
finale();
}
/*lcd.print("came here3");
delay(1000);*/
}
void ptg()
{
if(reshortn[i]=='S')
{
forward();
delay(fdelay);
}
if(reshortn[i]=='L')
{
left();
delay(tdelay);
}
if(reshortn[i]=='R')
{
right();
delay(tdelay);
}
i++;
}
//~~~~~~~~~~~~~~~~Shortest Path (second run)~~~~~~~~~~~~~~~~~
void finale()
{
//Serial.println("The Path to Glory");
lcd.clear();
i=0;
while(1)
{
readsens();
if((l==0) && (c1==0) && (c2==1) && (c3==0) && (r==0))
{
//Serial.println("\tForward");
forward();
}
//Conditions for LEFT
else if(((l==0) && (c1==1) && (c2==0) && (c3==0) && (r==0)) || ((l==0) && (c1==1) && (c2==1) && (c3==0) && (r==0)))
{
//Serial.println("\tLeft");
smallleft();
}
//Conditions for LEFT
else if(((l==0) && (c1==0) && (c2==0) && (c3==1) && (r==0)) || ((l==0) && (c1==0) && (c2==1) && (c3==1) && (r==0)))
{
//Serial.println("\tRight");
smallright();
}
else if(((l==1) && (c1==1)) || ((c3==1) && (r==1)))
{
preinch();
if(((l==0) && (c1==0) && (c2==1) && (c3==1) && (r==1)) || ((l==0) && (c1==1) && (c2==1) && (c3==1) && (r==1)))
{
//Serial.println("\tRIGHT ONLY / STRAIGHT AND RIGHT???");
inch();
//Serial.print("Choice = ");
if((l==0) && (c1==0) && (c2==0) && (c3==0) && (r==0))
{
//Serial.println("Right only");
right();
delay(tdelay);
}
else if((l==0) && (r==0) && (eosens()))
{
//Serial.println("Straight N Right");
ptg();
}
}
//Left and Straight n left
else if(((l==1) && (c1==1) && (c2==1) && (c3==0) && (r==0)) || ((l==1) && (c1==1) && (c2==1) && (c3==1) && (r==0)))
{
//Serial.println("\tLEFT ONLY / STRAIGHT AND LEFT????");
inch();
//Serial.print("Choice = ");
if((l==0) && (c1==0) && (c2==0) && (c3==0) && (r==0))
{
//Serial.println("Left only");
//lcd.print("= left");
left();
delay(tdelay);
}
else if((l==0) && (r==0) && (eosens()))
{
//Serial.println("Straight N left");
lcd.print("= stnlt");
ptg();
}
}
//T-int cross end of maze
else if((l==1) && (c1==1) && (c2==1) && (c3==1) && (r==1))
{
//Serial.println("\tT INTERSECTION / END OF MAZE / CLOVERLEAF");
inch();
//Serial.print("Choice = ");
if((l==0) && (r==0) && (eosens()))
{
//Serial.println("CLOVERLEAF");
ptg();
}
else if((l==0) && (c1==0) && (c2==0) && (c3==0) && (r==0))
{
//Serial.println("T-INT");
ptg();
}
if((l==1) && (c1==1) && (c2==1) && (c3==1) && (r==1))
{
//Serial.println("THE END");
while (1)
{
halt();
}
}
}
}
}
}
//~~~~~~~~~~~~~~~~Finish~~~~~~~~~~~~~~~~~