-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathLatestOne(Buggy).ino
824 lines (725 loc) · 21.6 KB
/
LatestOne(Buggy).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
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
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
/*
Known Bugs:
1- Threshold for Ping Ultrasonic Sensor Needs to be adjusted.
2- Array Function needs to be implemented so that we can store descions
3- Line Following needs to be implemeted with the Replay function
4-Redo the bool
5-In X's function implement Ultrasonic sensors
*/
#include <BiColorLED.h>
#include <Servo.h>
#define leapTime 85
#define LED 13
int buttonPin =13;
BiColorLED led = BiColorLED(A0, A1); // (pin 1, pin 2)
int frontUltraSonicSensor = 8, backUltraSonicSensor = 9, leftUltraSonicSensor = 10, rightUltraSonicSensor = 11; //Intializing Sensors with standard numbers
int frontQti = 4, backQti = 5, leftQti = 6, rightQti = 7;
int pins = 0;
int vL, vR, vB;
//int LWspeedF = 1550, LWspeedB = 1350, RWspeedF = 1350, RWspeedB = 1550, BWspeedF = 1550, BWspeedB = 1350; //Intializing speeds
int WheelInactive = 1500;
//Declaring wheels
Servo leftWheel;
Servo rightWheel;
Servo backWheel;
//Parallax Ultrasonic sensors and QTI Light Sensors Threshold
const int ultrasonic_Thershold = 25;
const int qtiThreshold = 50;
char mapp[150];
int replayStep = 0;
int directioncount = 0;
int shortDone;
bool replaystage = false;
void setup()
{
// pinMode(buttonPin, INPUT);
led.setColor(1);
//delay(5000);//Light the LED
led.setColor(0);
pinMode(buttonPin, INPUT); // sets the digital pin as input to read switch
leftWheel.attach(2);
rightWheel.attach(3);
backWheel.attach(12);
Serial.begin(9600);
}
void loop()
{
Serial.println("\t_______________________START___________________\t");
/*
buttonState =digitalRead(buttonPin);
if (butto nState == HIGH)
{
}
else {
while(1)
{
Serial.println("Turned Off");
}
}
*/
//Serial.println(frontUltrasonicTest());
checkForReplay();
if (frontUltrasonicTest() == 1 && //If we do not see a left or right turn and still on path
(backQtiTest() == 'b' && frontQtiTest() == 'b') &&
(leftQtiTest() == 'w' && rightQtiTest() == 'w'))
{
Serial.println("My Only Option is Front\n Moving Front");
straightForward();
}
else if ((backQtiTest() == 'b' && frontQtiTest() == 'b') &&
(leftQtiTest() == 'b' && rightQtiTest() == 'b')) {
//moveStandby();
leftHandWall();
Serial.println("Found an Intersection");
}
else if (leftQtiTest() == 'w'&& rightQtiTest() == 'w' && frontQtiTest() == 'b'&&backQtiTest() == 'b'
&& frontUltrasonicTest() == 0 && leftUltrasonicTest() == 0 && rightUltrasonicTest() == 0)
{
moveBack();
Serial.println("I am blocked need to go backwards");
}
else
{
Serial.println("I guess that I am off line");
straightForward();
}
delay(25);
}
//////////////////////////Line following function//////////////////////////
void straightForward()
{
bool flag = 1;
do{
DDRD |= B11110000; // Set direction of Arduino pins D4-D7 as OUTPUT
PORTD |= B11110000; // Set level of Arduino pins D4-D7 to HIGH
delayMicroseconds(230); // Short delay to allow capacitor charge in QTI module
DDRD &= B00001111; // Set direction of pins D4-D7 as INPUT
PORTD &= B00001111; // Set level of pins D4-D7 to LOW
delayMicroseconds(230); // Short delay
int pins = PIND; // Get values of pins D0-D7
pins >>= 4; // Drop off first four bits of the port; keep only pins D4-D7
Serial.println(pins, BIN); // Display result of D4-D7 pins in Serial Monitor
// Determine how to steer based on state of the four QTI sensor
switch (pins) // Compare pins to known line following states
{
case B0011:
vL = 103; // -100 to 100 indicate course correction values
vR = 100;
vB = 0;
Serial.println("Foward Case"); //Debug // -100: full reverse; 0=stopped; 100=full forward
break;
case B0111: //Sliding Right Case
vL = 100;
vR = 135;
vB = 0;
Serial.println("Sliding Right Case");
break;
case B1011: //Sliding left Case
vL = 135;
vR = 100;
vB = 0;
Serial.println("Sliding Left Case");
break;
case B1111: // Intersection Case
vL = 0;
vR = 0;
vB = 0;
flag = 0;
Serial.println("Intersection Case");
break;
case B0000: // White space case
vL = 0;
vR = 0;
vB = 0;
Serial.println("White Case");
break;
case B1010: // Diagonal Left case
vL = 120;
vR = 100;
vB = 0;
Serial.println("Diagonal Left Case");
break;
case B0110: //Diagonal Right
vL = 100;
vR = 120;
vB = 0;
Serial.println("Diagonal Right Case");
break;
case B0100: // Way off right case
vL = -80;
vR = 110;
vB = -90;
Serial.println("Way off Right Case");
break;
case B1000: //Way off left case
vL = 110;
vR = -80;
vB = 90;
Serial.println("Way off Left Case");
break;
case B1001: //Near off Left Case
vL = 135;
vR = 100;
vB = 0;
Serial.println("Near off Left Case");
break;
case B0101: //Near off Right Case
vL = 100;
vR = 135;
vB = 0;
Serial.println("Near off Right Case");
break;
}
leftWheel.writeMicroseconds(1500 + vL); // Steer robot to recenter it over the line
rightWheel.writeMicroseconds(1500 - vR);
backWheel.writeMicroseconds(1500 - vB);
checkForReplay();//Checking For Replay
if (frontUltrasonicTest() == 0)
{
flag = 0;
break;
}
delay(25);
} while (flag/*&&frontUltrasonicTest() == 1*/);
/*/
if (lefQtiTest() == 'w'&& rightQtiTest() == 'w' && frontQtiTest() == 'w')
{
readSensors();
delay(leapTime); //leap
if (leftQtiTest() == 'w'&& rightQtiTest() == 'w' && frontQtiTest() == 'w')
done();
}
*/
}
/////////////////////End Line following function//////////////////////////
//////////////////////////ULTRASONIC SENSOR TEST///////////////////////////
int leftUltrasonicTest()
{
unsigned int duration, centimeters;
pinMode(leftUltraSonicSensor, OUTPUT); // Set pin to OUTPUT
digitalWrite(leftUltraSonicSensor, LOW); // Ensure pin is low
delayMicroseconds(2);
digitalWrite(leftUltraSonicSensor, HIGH); // Start ranging
delayMicroseconds(5); // with 5 microsecond burst
digitalWrite(leftUltraSonicSensor, LOW); // End ranging
pinMode(leftUltraSonicSensor, INPUT); // Set pin to INPUT
duration = pulseIn(leftUltraSonicSensor, HIGH); // Read echo pulse
centimeters = duration / 29 / 2; // Convert to centimeters
Serial.println("Checking Left Ultra Sonic Sensor: ");
if (centimeters>ultrasonic_Thershold)
{
Serial.println("Not Blocked");
return 1; //There Is no walll
}
else
{
Serial.println("Blocked");
return 0;
}
}
int rightUltrasonicTest()
{
unsigned int duration, centimeters;
pinMode(rightUltraSonicSensor, OUTPUT); // Set pin to OUTPUT
digitalWrite(rightUltraSonicSensor, LOW); // Ensure pin is low
delayMicroseconds(2);
digitalWrite(rightUltraSonicSensor, HIGH); // Start ranging
delayMicroseconds(5); // with 5 microsecond burst
digitalWrite(rightUltraSonicSensor, LOW); // End ranging
pinMode(rightUltraSonicSensor, INPUT); // Set pin to INPUT
duration = pulseIn(rightUltraSonicSensor, HIGH); // Read echo pulse
centimeters = duration / 29 / 2; // Convert to centimeters
Serial.println("Checking right Ultrasonic sensor");
if (centimeters>ultrasonic_Thershold)
{
Serial.println("Not Blocked");
return 1;
}
else{
Serial.println("Blocked");
return 0;
}
}
int frontUltrasonicTest()
{
unsigned int duration, centimeters;
pinMode(frontUltraSonicSensor, OUTPUT); // Set pin to OUTPUT
digitalWrite(frontUltraSonicSensor, LOW); // Ensure pin is low
delayMicroseconds(2);
digitalWrite(frontUltraSonicSensor, HIGH); // Start ranging
delayMicroseconds(5); // with 5 microsecond burst
digitalWrite(frontUltraSonicSensor, LOW); // End ranging
pinMode(frontUltraSonicSensor, INPUT); // Set pin to INPUT
duration = pulseIn(frontUltraSonicSensor, HIGH); // Read echo pulse
centimeters = duration / 29 / 2; // Convert to centimeters
Serial.println("Checking Front Ultrasonic Sensor");
if (centimeters>ultrasonic_Thershold)
{
Serial.println("Not Blocked");
return 1;
}
else{
Serial.println("Blocked");
return 0;
}
}
int backUltrasonicTest()
{
unsigned int duration, centimeters;
pinMode(backUltraSonicSensor, OUTPUT); // Set pin to OUTPUT
digitalWrite(backUltraSonicSensor, LOW); // Ensure pin is low
delayMicroseconds(2);
digitalWrite(backUltraSonicSensor, HIGH); // Start ranging
delayMicroseconds(5); // with 5 microsecond burst
digitalWrite(backUltraSonicSensor, LOW); // End ranging
pinMode(backUltraSonicSensor, INPUT); // Set pin to INPUT
duration = pulseIn(backUltraSonicSensor, HIGH); // Read echo pulse
centimeters = duration / 29 / 2; // Convert to centimeters
Serial.println("Checking back Ultrasonic Sensor");
if (centimeters>ultrasonic_Thershold)
{
Serial.println("Not Blocked");
return 1;
}
else{
Serial.println("Blocked");
return 0;
}
}
//////////////////////////END ULTRASONIC SENSOR TEST///////////////////////
/////////////////////////QTI LIGHT SENSORS////////////////////////////////
char frontQtiTest()
{
long duration = 0;
pinMode(frontQti, OUTPUT); // Make pin OUTPUT
digitalWrite(frontQti, HIGH); // Pin HIGH (discharge capacitor)
delay(1); // Wait 1ms
pinMode(frontQti, INPUT); // Make pin INPUT
digitalWrite(frontQti, LOW); // Turn off internal pullups
while (digitalRead(frontQti)){ // Wait for pin to go LOW
duration++;
}
Serial.println("Checking Front Qti ");
if (duration>qtiThreshold)
{
Serial.println("Black");
return 'b';
}
else
{
Serial.println("White");
return 'w';
}
}
char backQtiTest()
{
long duration = 0;
pinMode(backQti, OUTPUT); // Make pin OUTPUT
digitalWrite(backQti, HIGH); // Pin HIGH (discharge capacitor)
delay(1); // Wait 1ms
pinMode(backQti, INPUT); // Make pin INPUT
digitalWrite(backQti, LOW); // Turn off internal pullups
while (digitalRead(backQti)){ // Wait for pin to go LOW
duration++;
}
Serial.println("Checking Back Qti: ");
if (duration>qtiThreshold)
{
Serial.println("Black");
return 'b';
}
else
{
Serial.println("White");
return 'w';
}
}
char leftQtiTest()
{
long duration = 0;
pinMode(leftQti, OUTPUT); // Make pin OUTPUT
digitalWrite(leftQti, HIGH); // Pin HIGH (discharge capacitor)
delay(1); // Wait 1ms
pinMode(leftQti, INPUT); // Make pin INPUT
digitalWrite(leftQti, LOW); // Turn off internal pullups
while (digitalRead(leftQti)){ // Wait for pin to go LOW
duration++;
}
Serial.println("Checking Left Qti: ");
if (duration>qtiThreshold)
{
Serial.println("Black");
return 'b';
}
else
{
Serial.println("White");
return 'w';
}
}
char rightQtiTest()
{
long duration = 0;
pinMode(rightQti, OUTPUT); // Make pin OUTPUT
digitalWrite(rightQti, HIGH); // Pin HIGH (discharge capacitor)
delay(1); // Wait 1ms
pinMode(rightQti, INPUT); // Make pin INPUT
digitalWrite(rightQti, LOW); // Turn off internal pullups
while (digitalRead(rightQti)){ // Wait for pin to go LOW
duration++;
}
Serial.println("Checking Right Qti: ");
if (duration>qtiThreshold)
{
Serial.println("Black");
return 'b';
}
else
{
Serial.println("White");
return 'w';
}
}
///////////////////////QTI LIGHT SENSORS END/////////////////////////////
///////////////////////Wheels Movement//////////////////////////////////
void moveStandby() //putting wheels on standby
{
leftWheel.writeMicroseconds(WheelInactive);
rightWheel.writeMicroseconds(WheelInactive);
backWheel.writeMicroseconds(WheelInactive);
}
void moveFront()
{
bool intersectionFlag = true;
while (/*frontUltrasonicTest() == 1 &&*/ intersectionFlag){
Serial.println("Moving Front");
DDRD |= B11110000; // Set direction of Arduino pins D4-D7 as OUTPUT
PORTD |= B11110000; // Set level of Arduino pins D4-D7 to HIGH
delayMicroseconds(230); // Short delay to allow capacitor charge in QTI module
DDRD &= B00001111; // Set direction of pins D4-D7 as INPUT
PORTD &= B00001111; // Set level of pins D4-D7 to LOW
delayMicroseconds(230); // Short delay
int pins = PIND; // Get values of pins D0-D7
pins >>= 4; // Drop off first four bits of the port; keep only pins D4-D7
Serial.println(pins, BIN); // Display result of D4-D7 pins in Serial Monitor
// Determine how to steer based on state of the four QTI sensor
switch (pins) // Compare pins to known line following states
{
case B0011:
vL = 103; // -100 to 100 indicate course correction values
vR = 100;
vB = 0;
Serial.println("Foward Case"); //Debug // -100: full reverse; 0=stopped; 100=full forward
break;
case B0111: //Sliding Right Case
vL = 100;
vR = 135;
vB = 0;
Serial.println("Sliding Right Case");
break;
case B1011: //Sliding left Case
vL = 135;
vR = 100;
vB = 0;
Serial.println("Sliding Left Case");
break;
case B1111: // Intersection Case
vL = 103; // -100 to 100 indicate course correction values
vR = 100;
vB = 0;
Serial.println("Intersection Case");
intersectionFlag = false;
break;
case B0000: // White space case
vL = 0;
vR = 0;
vB = 0;
Serial.println("White Case");
break;
case B1010: // Diagonal Left case
vL = 120;
vR = 100;
vB = 0;
Serial.println("Diagonal Left Case");
break;
case B0110: //Diagonal Right
vL = 100;
vR = 120;
vB = 0;
Serial.println("Diagonal Right Case");
break;
case B0100: // Way off right case
vL = -80;
vR = 110;
vB = -90;
Serial.println("Way off Right Case");
break;
case B1000: //Way off left case
vL = 110;
vR = -80;
vB = 90;
Serial.println("Way off Left Case");
break;
case B1001: //Near off Left Case
vL = 135;
vR = 100;
vB = 0;
Serial.println("Near off Left Case");
break;
case B0101: //Near off Right Case
vL = 100;
vR = 135;
vB = 0;
Serial.println("Near off Right Case");
break;
}
leftWheel.writeMicroseconds(1500 + vL); // Steer robot to recenter it over the line
rightWheel.writeMicroseconds(1500 - vR);
backWheel.writeMicroseconds(1500 - vB);
checkForReplay(); // Checking for Replay Hopefully works !!!
if (frontUltrasonicTest() == 0)
{
intersectionFlag = 0; break;
}
delay(25);
}
}
void moveBack()
{
Serial.println("Turning Back");
moveRight();
moveRight();
}
void moveLeft()
{
Serial.println("Turning Left");
Serial.println(pins, BIN); // Display result of D4-D7 pins in Serial Monitor
vL = -120;
vR = 120;
vB = 0;
do{
leftWheel.writeMicroseconds(1500 + vL); // Steer robot to recenter it over the line
rightWheel.writeMicroseconds(1500 - vR);
backWheel.writeMicroseconds(1500 - vB);
Serial.println("Left");
//delay(435);
} while (backQtiTest() != 'b' && frontQtiTest() != 'b');
pushFront();
if (replaystage == 0)
{
Serial.println("Your left value is stored inside of the array :D !!!");
mapp[directioncount] = 'L';
directioncount++;
if (mapp[directioncount - 2] == 'B')
{
shortPath();
}
}
}
void moveRight()
{
Serial.println("Turning Right");
vL = 120;
vR = -120;
vB = 0;
leftWheel.writeMicroseconds(1500 + vL); // Steer robot to recenter it over the line
rightWheel.writeMicroseconds(1500 - vR);
backWheel.writeMicroseconds(1500 - vB);
Serial.println("Right");
delay(435);
pushFront();
if (replaystage == 0)
{
Serial.println("Your right value is stored inside of the array :D !!!");
mapp[directioncount] = 'R';
directioncount++;
if (mapp[directioncount - 2] == 'B')
{
shortPath();
}
}
}
///////////////////////End Wheel Movement//////////////////////////////
void readSensors()//For debugging purposes
{ //reading the sensors
rightQtiTest();
leftQtiTest();
backQtiTest();
frontQtiTest();
frontUltrasonicTest();
backUltrasonicTest();
leftUltrasonicTest();
rightUltrasonicTest();
}
//////////////////////START LEFT HANDWALL/////////////////////////////
void leftHandWall()
{
Serial.println("LEFT Handwall");
readSensors();
if (leftQtiTest() == 'b'&& rightQtiTest() == 'b' && frontQtiTest() == 'b'&&backQtiTest() == 'b'&& leftUltrasonicTest() == 1)
{
moveLeft();
}
else if (leftQtiTest() == 'b'&& rightQtiTest() == 'b' && frontQtiTest() == 'b'&&backQtiTest() == 'b'&& leftUltrasonicTest() == 0 && frontUltrasonicTest() == 1)
{
moveFront();
if (replaystage == 0)
{
mapp[directioncount] = 'F';
directioncount++;
if (mapp[directioncount - 2] == 'B')
{
shortPath();
}
}
}
else if (leftQtiTest() == 'b'&& rightQtiTest() == 'b' && frontQtiTest() == 'b'&&backQtiTest() == 'b'&& leftUltrasonicTest() == 0 && rightUltrasonicTest() == 1 && frontUltrasonicTest() == 0)
{
moveRight();
}
else if (leftQtiTest() == 'b'&& rightQtiTest() == 'b' && frontQtiTest() == 'b'&&backQtiTest() == 'b'&& leftUltrasonicTest() == 0 && rightUltrasonicTest() == 0 && frontUltrasonicTest() == 0)
{
moveBack();
if (replaystage == 0)
{
mapp[directioncount] = 'B';
directioncount++;
if (mapp[directioncount - 2] == 'B')
{
shortPath();
}
}
}
else if (leftQtiTest() == 'w'&& rightQtiTest() == 'w' && frontQtiTest() == 'b'&&backQtiTest() == 'b'&& frontUltrasonicTest() == 0)
{
moveBack();
}
// delay(leapTime); //leap
}
//////////////////////ENDLEFTHANDWALL////////////////////////////////
////////////////////START SHORTPATh//////////////////////////////////
void shortPath(){
int shortDone = 0;
if (mapp[directioncount - 3] == 'L' && mapp[directioncount - 1] == 'R'){
directioncount -= 3;
mapp[directioncount] = 'B';
shortDone = 1;
}
else if (mapp[directioncount - 3] == 'L' && mapp[directioncount - 1] == 'F' && shortDone == 0){
directioncount -= 3;
mapp[directioncount] = 'R';
shortDone = 1;
}
else if (mapp[directioncount - 3] == 'R' && mapp[directioncount - 1] == 'L' && shortDone == 0){
directioncount -= 3;
mapp[directioncount] = 'B';
shortDone = 1;
}
else if (mapp[directioncount - 3] == 'F' && mapp[directioncount - 1] == 'L' && shortDone == 0){
directioncount -= 3;
mapp[directioncount] = 'R';
shortDone = 1;
}
else if (mapp[directioncount - 3] == 'F' && mapp[directioncount - 1] == 'F' && shortDone == 0){
directioncount -= 3;
mapp[directioncount] = 'B';
shortDone = 1;
}
else if (mapp[directioncount - 3] == 'L' && mapp[directioncount - 1] == 'L' && shortDone == 0){
directioncount -= 3;
mapp[directioncount] = 'F';
shortDone = 1;
}
mapp[directioncount + 1] = 'D';
mapp[directioncount + 2] = 'D';
directioncount++;
}
//////////////////////ENDSHORTPATH////////////////////////////////////////////
//////////////////////DONE////////////////////////////////////////////////////
void done()
{
//Putting all wheels on standby
leftWheel.writeMicroseconds(1500);
rightWheel.writeMicroseconds(1500);
backWheel.writeMicroseconds(1500);
replaystage = 1; // Setting The replaystage condition to True
mapp[directioncount] = 'D';
directioncount++;
while ((leftQtiTest() == 'w') && (rightQtiTest() == 'w'))
{
led.setColor(2);
//Lighting the LED when finished
}
led.setColor(0);
//Lighting the LED Again before Starting
led.setColor(1);
delay(10000);
led.setColor(0);
//Replying with the newely found values
replay();
}
///////////////////////ENDDONE///////////////////////////////////////////////
void replay(){
if ((leftQtiTest() == 'w') && (rightQtiTest() == 'w') && (frontQtiTest() == 'b') && (backQtiTest() == 'b'))
{
Serial.println("Replay: Front Only Option");
moveFront();
}
else if ((leftQtiTest() == 'b') && (rightQtiTest() == 'b') && (frontQtiTest() == 'b') && (backQtiTest() == 'b'))
{
Serial.println("Replay Intersection");
Serial.println("Checking the array to replay");
if (mapp[replayStep] == 'D')
{
endMotion();
}
if (mapp[replayStep] == 'L'){
//delay(leapTime);
moveLeft();
}
if (mapp[replayStep] == 'R'){
//delay(leapTime);
moveRight();
}
if (mapp[replayStep] == 'F'){
//delay(leapTime);
moveFront();
}
replayStep++;
}
replay();
}
void endMotion(){
leftWheel.writeMicroseconds(1500);
rightWheel.writeMicroseconds(1500);
backWheel.writeMicroseconds(1500);
while ((leftQtiTest() == 'w') && (rightQtiTest() == 'w'))
{
led.setColor(1);
}
led.setColor(0);
}
void pushFront()
{
vL = 100; // -100 to 100 indicate course correction values
vR = 100;
vB = 0;
leftWheel.writeMicroseconds(1500 + vL); // Steer robot to recenter it over the line
rightWheel.writeMicroseconds(1500 - vR);
backWheel.writeMicroseconds(1500 - vB);
}
void checkForReplay()
{
if (replaystage == false)
{
Serial.print("Read switch input: ");
Serial.println(digitalRead(buttonPin)); // Read the pin and display the value
delay(100);
if (digitalRead(buttonPin) == 0)
replaystage = true;
}
if (replaystage == true){
Serial.println("I am replaying");
replay();
}
}