-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFinal.cpp
More file actions
1554 lines (1341 loc) · 40.5 KB
/
Copy pathFinal.cpp
File metadata and controls
1554 lines (1341 loc) · 40.5 KB
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
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <Arduino.h>
#include <EEPROM.h>
#include <Servo.h>
#include <U8g2lib.h>
#ifdef U8X8_HAVE_HW_SPI
#include <SPI.h>
#endif
#ifdef U8X8_HAVE_HW_I2C
#include <Wire.h>
#endif
U8G2_SH1106_128X32_VISIONOX_F_HW_I2C u8g2(U8G2_R0, /* reset=*/U8X8_PIN_NONE);
/////////////////////////////////Definitions///////////////////////////////////////////////////////////////////////////////////////////////
// ========== LINE_COLOR DEFINITION ==========
const String LINE_COLOR = "WHITE";
int array_lit_amount = 0;
// Define the size of the array and the EEPROM address to start writing data
const int arraySize = 8; // Adjust this based on your array size
const int eepromAddress = 0; // EEPROM address where you want to start writing
#define Debug_led 13
bool wallVar = true;
// PID parameters for Line Following
double Kp = 8;
double Ki = 0;
double Kd = 7;
// PID parameters for Ultrasonic Wall Following
double UKp = 35;
double UKi = 0;
double Ukd = 20;
int Drive_constant = 200;
// Initialize PID variables
double prevError = 0;
double integral = 0;
double derivative = 0;
// recorrect
int Ir_thresholds[] = {300, 300, 100, 100, 100, 105, 400, 400};
int prev_error_history[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
int stack_pointer = 0;
int IR_history_count = 0;
int IR_history = 0;
int IR_first_grabbed = 0;
int guard_left = 0;
int guard_right = 0;
int guard_left_prev = 2;
int guard_right_prev = 2;
int count = 0;
bool pull_box_arm_retract = false;
bool pull_bend = false;
// define trig pin 6 and echo pin 7
#define trigPin 6
#define echoPin 7
// pushbutton input for calibration
#define calibration 7
// pin for buzzer
#define buzzer_pin 8
bool bend_done = false;
// pin for right bend sensor
#define right_bend_sensor 9
bool Switch = 1;
bool bend_start = false;
bool bend_condition = false;
int now_time = 0;
String mode = "normal";
String wall_side = "";
Servo myservo; // create servo object to control a servo
enum Stage // This is for the final competition.
{
LINE = 1,
WALL,
RAMP,
PULL,
COLOR,
SOUND,
GUARD,
HALT
};
// If initial square is also counted add something first and assign it to TASK.
Stage TASK = LINE; // 1 - Line Following, 2 - Wall Following, etc.
int Stage_no = 256; // 1 - Line Following, 2 - Wall Following, etc.
String STAGES[] = {"LINE", "WALL", "RAMP", "PULL", "COLOR", "SOUND", "GUARD", "HALT"};
int pointer = 0;
// int TASK = 1; // 1 - Line Following, 2 - Wall Following, etc.
// IR_array 8 space empty array
#define IR_ARRAY_LENGTH 12
int IR_array[IR_ARRAY_LENGTH];
int Threshold = 100;
int prev_position = 0;
int prev_history_sum = 0;
// define right motor enable pin is 3 and left 2
#define ENR 3
#define ENL 2
#define Motor_Right_Forward 28
#define Motor_Right_Backward 24
#define Motor_Left_Forward 22
#define Motor_Left_Backward 26
// Motor and Direction Definitions
#define RIGHT_MOTOR 1
#define LEFT_MOTOR 2
#define FORWARD 1
#define BACKWARD 2
// Sound sensor
#define SOUND_SENSOR A10
#define SOUND_THRESHOLD 300
#define SOUND_WAIT_TIME 500 // milliseconds
#define SOUND_LED 13
unsigned long int sound_now_time;
// bool Switch = 0; //0 for left ; 1 for right;
int x = 0;
// Ultrasonic sensors
#define SENSOR_LEFT 1
#define SENSOR_RIGHT 2
#define SENSOR_LEFT_TRIG_PIN 32
#define SENSOR_LEFT_ECHO_PIN 34
#define SENSOR_RIGHT_TRIG_PIN 36
#define SENSOR_RIGHT_ECHO_PIN 38
float ultrasonic_prev_error = 0;
#define MAX_ULTRASONIC_DISTANCE 30 // in centimeters
// Task selection switch
#define TASK_SWITCH 44
#define SWITCH_MSB 45
#define SWITCH_MID_SB 46
#define SWITCH_LSB 47
int sound_threshold = 500;
/////////////////////////////////Function Declarations/////////////////////////////////////////////////////////////////////////////////////
void readIRArray();
void motor(int motor, int direction);
void calibrate();
void buzzer_beep();
int ultrasonicDistance(int sensor);
Stage getStageFromSwitches();
bool squareDetected();
bool TDetected();
void lineFollow();
void wallFollow();
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////Setup//////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void setup()
{
Serial.begin(115200);
u8g2.begin();
int now_time = millis();
int max_sound = 0;
int min_sound = 1024;
u8g2.clearBuffer(); // clear the internal memory
u8g2.setFont(u8g2_font_ncenB08_tr); // choose a suitable font
u8g2.drawStr(0, 10, "SOUND CALIBERATING"); // write something to the internal memory
u8g2.sendBuffer();
while (millis()-now_time <= 5000){
int analog_sound_read = analogRead(A13);
Serial.println(analog_sound_read);
max_sound = max(analog_sound_read, max_sound);
min_sound = min(analog_sound_read,min_sound);
}
sound_threshold = (max_sound + min_sound)/2;
Serial.println(sound_threshold);
pinMode(buzzer_pin, OUTPUT);
// pin 8 is buzzer, tur it on and off for 2 quick beeps
buzzer_beep();
delay(100);
buzzer_beep();
// pinmode trig and echo
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
// attaches the servo on pin 11 to the servo object
myservo.attach(11);
// myservo.write(0);
// delay(1000);
// myservo.write(120);
// delay(1000);
// myservo.write(0);
// Serial begin
for (int i = 0; i < IR_ARRAY_LENGTH; i++)
{
byte temp = 0;
// Ir_thresholds[i] = EEPROM.read(i)*4;
EEPROM.get(i, temp);
Ir_thresholds[i] = temp;
}
Serial.print("int IR_thresholds[] = {");
for (int i = 0; i < IR_ARRAY_LENGTH; i++)
{
Serial.print(Ir_thresholds[i]);
Serial.print(", ");
}
Serial.println("}; ");
pinMode(Motor_Right_Forward, OUTPUT);
pinMode(Motor_Right_Backward, OUTPUT);
pinMode(Motor_Left_Forward, OUTPUT);
pinMode(Motor_Left_Backward, OUTPUT);
pinMode(right_bend_sensor, INPUT);
motor(RIGHT_MOTOR, FORWARD);
motor(LEFT_MOTOR, FORWARD);
pinMode(calibration, INPUT);
sound_now_time = millis();
pinMode(SENSOR_LEFT_ECHO_PIN, INPUT);
pinMode(SENSOR_LEFT_TRIG_PIN, OUTPUT);
pinMode(SENSOR_RIGHT_ECHO_PIN, INPUT);
pinMode(SENSOR_RIGHT_TRIG_PIN, OUTPUT);
pinMode(TASK_SWITCH, INPUT);
pinMode(SWITCH_MSB, INPUT);
pinMode(SWITCH_MID_SB, INPUT);
pinMode(SWITCH_LSB, INPUT);
pinMode(12, INPUT);
delay(1000); // Wait for the sensors to be ready.
// while(true){
// Serial.println(analogRead(A13));
// }
// motor(LEFT_MOTOR, FORWARD);
// motor(RIGHT_MOTOR, BACKWARD);
// analogWrite(ENL, 255);
// analogWrite(ENR, 255);
// delay(750);
// analogWrite(ENL, 0);
// analogWrite(ENR, 0);
int POT_value = 0;
while (digitalRead(calibration) == 0)
{
// Serial.println(digitalRead(12));
if (digitalRead(12) == 1)
{
delay(600);
POT_value++;
while (true)
{
if (digitalRead(12) == 0)
{
break;
}
}
}
if (POT_value == 8)
{
POT_value = 0;
}
// Serial.println(POT_value);
if (POT_value == 0)
{
u8g2.clearBuffer(); // clear the internal memory
u8g2.setFont(u8g2_font_ncenB08_tr); // choose a suitable font
u8g2.drawStr(0, 10, "LINE"); // write something to the internal memory
u8g2.sendBuffer(); // transfer internal memory to the display
}
else if (POT_value == 1)
{
u8g2.clearBuffer(); // clear the internal memory
u8g2.setFont(u8g2_font_ncenB08_tr); // choose a suitable font
u8g2.drawStr(0, 10, "WALL"); // write something to the internal memory
u8g2.sendBuffer(); // transfer internal memory to the display
}
else if (POT_value == 2)
{
u8g2.clearBuffer(); // clear the internal memory
u8g2.setFont(u8g2_font_ncenB08_tr); // choose a suitable font
u8g2.drawStr(0, 10, "RAMP"); // write something to the internal memory
u8g2.sendBuffer(); // transfer internal memory to the display
}
else if (POT_value == 3)
{
u8g2.clearBuffer(); // clear the internal memory
u8g2.setFont(u8g2_font_ncenB08_tr); // choose a suitable font
u8g2.drawStr(0, 10, "PULL"); // write something to the internal memory
u8g2.sendBuffer(); // transfer internal memory to the display
}
else if (POT_value == 4)
{
u8g2.clearBuffer(); // clear the internal memory
u8g2.setFont(u8g2_font_ncenB08_tr); // choose a suitable font
u8g2.drawStr(0, 10, "COLOR"); // write something to the internal memory
u8g2.sendBuffer(); // transfer internal memory to the display
}
else if (POT_value == 5)
{
u8g2.clearBuffer(); // clear the internal memory
u8g2.setFont(u8g2_font_ncenB08_tr); // choose a suitable font
u8g2.drawStr(0, 10, "SOUND"); // write something to the internal memory
u8g2.sendBuffer(); // transfer internal memory to the display
}
else if (POT_value == 6)
{
u8g2.clearBuffer(); // clear the internal memory
u8g2.setFont(u8g2_font_ncenB08_tr); // choose a suitable font
u8g2.drawStr(0, 10, "GUARD"); // write something to the internal memory
u8g2.sendBuffer(); // transfer internal memory to the display
}
else if (POT_value == 7)
{
u8g2.clearBuffer(); // clear the internal memory
u8g2.setFont(u8g2_font_ncenB08_tr); // choose a suitable font
u8g2.drawStr(0, 10, "HALT"); // write something to the internal memory
u8g2.sendBuffer(); // transfer internal memory to the display
}
else
{
u8g2.clearBuffer(); // clear the internal memory
u8g2.setFont(u8g2_font_ncenB08_tr); // choose a suitable font
u8g2.drawStr(0, 10, "NOT DEF"); // write something to the internal memory
u8g2.sendBuffer(); // transfer internal memory to the display
}
}
pointer = POT_value;
// pointer = 5;
u8g2.clearBuffer(); // clear the internal memory
u8g2.setFont(u8g2_font_ncenB08_tr); // choose a suitable font
u8g2.drawStr(0, 10, "ENTERED THE MODE"); // write something to the internal memory
u8g2.sendBuffer(); // transfer internal memory to the display
delay(1000);
}
int temp = 0;
int Left_drive = 0;
int Right_drive = 0;
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////Loop//////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void loop()
{
// while(1) {
// analogWrite(ENL, 230);
// analogWrite(ENR, 170);
// motor(LEFT_MOTOR, FORWARD);
// motor(RIGHT_MOTOR, FORWARD);
// }
if (digitalRead(calibration) == 1)
{
Serial.println("Calibrating...");
calibrate();
}
// Gets the task to be started, using switches.
// Maybe we can use an INTERRUPT.
// if (digitalRead(TASK_SWITCH) == HIGH) {
// TASK = getStageFromSwitches();
// // Buzzer beep count can be used to get the task.
// for (int i = 0; i < TASK; i++) {
// buzzer_beep();
// }
// }
// Serial.print("Stage_no: ");
// Serial.println(STAGES[pointer]);
// Automatically increment task after detecting a white square.
if (squareDetected())
{
if (pointer == 3){
motor(LEFT_MOTOR, FORWARD);
motor(RIGHT_MOTOR, FORWARD);
analogWrite(ENL, 230);
analogWrite(ENR, 170);
delay(500);
myservo.write(140);
analogWrite(ENL, 0);
analogWrite(ENR, 0);
delay(1000);
analogWrite(ENL, 240);
analogWrite(ENR, 160);
motor(LEFT_MOTOR, BACKWARD);
motor(RIGHT_MOTOR, BACKWARD);
delay(6000);
analogWrite(ENL, 0);
analogWrite(ENR, 0);
}
if(pointer != 3){
Serial.println("Square detected");
analogWrite(ENL, 0);
analogWrite(ENR, 0);
// if (Stage_no == 257){
// motor(LEFT_MOTOR, FORWARD);
// motor(RIGHT_MOTOR, BACKWARD);
// analogWrite(ENL, 255);
// analogWrite(ENR, 255);
// delay(1000);
// }
if (pointer == 1){
motor(LEFT_MOTOR, FORWARD);
motor(RIGHT_MOTOR, FORWARD);
analogWrite(ENL, 230);
analogWrite(ENR, 170);
delay(900);
motor(LEFT_MOTOR, FORWARD);
motor(RIGHT_MOTOR, BACKWARD);
analogWrite(ENL, 255);
analogWrite(ENR, 255);
delay(750);
analogWrite(ENL,0);
analogWrite(ENR, 0);
}
if (pointer == 4){
motor(LEFT_MOTOR, FORWARD);
motor(RIGHT_MOTOR, FORWARD);
analogWrite(ENL, 230);
analogWrite(ENR, 170);
delay(900);
motor(LEFT_MOTOR, BACKWARD);
motor(RIGHT_MOTOR, FORWARD);
analogWrite(ENL, 255);
analogWrite(ENR, 255);
delay(750);
analogWrite(ENL,0);
analogWrite(ENR, 0);
}
pointer++;
// Stage_no = Stage_no +1 ;
myservo.write(120);
delay(1000);
myservo.write(0);
delay(1000);
myservo.write(120);
motor(LEFT_MOTOR, FORWARD);
motor(RIGHT_MOTOR, FORWARD);
analogWrite(ENL, 230);
analogWrite(ENR, 170);
delay(1400);
// //TASK = Stage(TASK + 1);
// // square is correct for the final competition.`````````````````````````````````````
// // TASK = HALT;
}
}
///////////////////////// 1] LINE FOLLOWING///////////////////////////////////////////
if (pointer == 0)
{
lineFollow();
}
///////////////////////// 2] WALL FOLLOWING///////////////////////////////////////////
else if (pointer == 1)
{
// int ultrasonicDistanceLeft = ultrasonicDistance(SENSOR_LEFT);
// int ultrasonicDistanceRight = ultrasonicDistance(SENSOR_RIGHT);
lineFollow();
if (Switch == 0)
{
// // 2nd wall
// if (ultrasonicDistanceLeft < 40 && ultrasonicDistanceRight > 40)
// {
// motor(LEFT_MOTOR, FORWARD);
// motor(RIGHT_MOTOR, FORWARD);
// analogWrite(ENL, 255);
// analogWrite(ENR, 10);
// delay(800);
// while (true)
// {
// motor(RIGHT_MOTOR, FORWARD);
// analogWrite(ENL, 230);
// analogWrite(ENR, 170);
// array_lit_amount = 0;
// readIRArray();
// for (int i = 0; i < IR_ARRAY_LENGTH; i++)
// {
// array_lit_amount += IR_array[i];
// }
// if (array_lit_amount >= 3)
// {
// analogWrite(ENL, 0);
// analogWrite(ENR, 0);
// // delay(500);
// motor(LEFT_MOTOR, FORWARD);
// analogWrite(ENL, 255);
// analogWrite(ENR, 0);
// delay(700);
// break;
// // Switch = 0;
// // analogWrite(ENL, 0);
// // analogWrite(ENR, 0);
// // delay(1000) ;
// }
// }
// }
// // 1st wall
// else if (ultrasonicDistanceLeft > 30 && ultrasonicDistanceRight < 30)
// {
// motor(RIGHT_MOTOR, FORWARD);
// analogWrite(ENR, 255);
// analogWrite(ENL, 0);
// delay(500);
// while (true)
// {
// motor(LEFT_MOTOR, FORWARD);
// analogWrite(ENL, 230);
// analogWrite(ENR, 170);
// array_lit_amount = 0;
// readIRArray();
// for (int i = 0; i < IR_ARRAY_LENGTH; i++)
// {
// array_lit_amount += IR_array[i];
// }
// if (array_lit_amount >= 3)
// {
// analogWrite(ENL, 0);
// analogWrite(ENR, 0);
// // delay(500);
// motor(RIGHT_MOTOR, FORWARD);
// analogWrite(ENL, 0);
// analogWrite(ENR, 255);
// delay(500);
// break;
// // Switch = 0;
// // analogWrite(ENL, 0);
// // analogWrite(ENR, 0);
// // delay(1000) ;
// }
// }
// }
// 1st wall
int count = 0;
if (count == 0)
{
count = count + 1;
motor(RIGHT_MOTOR, FORWARD);
motor(LEFT_MOTOR, FORWARD);
analogWrite(ENL, 255);
analogWrite(ENR, 140);
delay(5000);
while (true)
{
motor(LEFT_MOTOR, FORWARD);
analogWrite(ENL, 255);
analogWrite(ENR, 170);
array_lit_amount = 0;
readIRArray();
for (int i = 0; i < IR_ARRAY_LENGTH; i++)
{
array_lit_amount += IR_array[i];
}
if (array_lit_amount >= 3)
{
analogWrite(ENL, 0);
analogWrite(ENR, 0);
delay(500);
motor(LEFT_MOTOR, FORWARD);
analogWrite(ENL, 255);
analogWrite(ENR, 0);
delay(500);
analogWrite(ENL, 0);
analogWrite(ENR, 0);
myservo.write(120);
delay(1000);
myservo.write(0);
delay(1000);
myservo.write(120);
break;
// Switch = 0;
// analogWrite(ENL, 0);
// analogWrite(ENR, 0);
// delay(1000) ;
}
}
}
}
}
///////////////////////// 3] RAMP ///////////////////////////////////////////
else if (pointer == 2)
{
lineFollow();
}
///////////////////////// 4] PULL the Box///////////////////////////////////////////
else if (pointer == 3)
{
if(pull_box_arm_retract == false){
myservo.write(0);
analogWrite(ENL, 0);
analogWrite(ENR, 0);
delay(100);
pull_box_arm_retract = true;
}
lineFollow();
if (pull_bend == false){
if(TDetected()){
motor(LEFT_MOTOR, FORWARD);
motor(RIGHT_MOTOR, FORWARD);
analogWrite(ENL, 230);
analogWrite(ENR, 170);
delay(900);
motor(LEFT_MOTOR, FORWARD);
motor(RIGHT_MOTOR, BACKWARD);
analogWrite(ENL, 255);
analogWrite(ENR, 255);
delay(750);
analogWrite(ENL,0);
analogWrite(ENR, 0);
delay(1000);
pull_bend = true;
}
}
}
///////////////////////// 5] Color FOLLOWING///////////////////////////////////////////
else if (pointer == 4)
{
if(TDetected()){
motor(LEFT_MOTOR, FORWARD);
motor(RIGHT_MOTOR, FORWARD);
analogWrite(ENL, 230);
analogWrite(ENR, 170);
delay(900);
motor(LEFT_MOTOR, FORWARD);
motor(RIGHT_MOTOR, BACKWARD);
analogWrite(ENL, 255);
analogWrite(ENR, 255);
delay(750);
analogWrite(ENL,0);
analogWrite(ENR, 0);
delay(1000);
pull_bend = true;
}else{
lineFollow();
}
}
///////////////////////// 6] Sound Game ///////////////////////////////////////////
else if (pointer == 5) {
Drive_constant = 50;
Serial.println(analogRead(13));
if (analogRead(A13)<sound_threshold){
delay(50);
if (analogRead(A13)<sound_threshold){
delay(50);
if (analogRead(A13)<sound_threshold){
int now_time = millis();
while(millis()-now_time <= 500){
lineFollow();
}
// analogWrite(ENL,0);
// analogWrite(ENR,0);
// delay(1000);
Serial.println("triggered the sound sensor");
}
}
}
analogWrite(ENL,0);
analogWrite(ENR,0);
while (true){
delay(300);
if (analogRead(A13) < sound_threshold){break;}
}
{
/* code */
}
// if (analogRead(SOUND_SENSOR) >= SOUND_THRESHOLD) {
// sound_now_time = millis();
// }
// if (millis() - sound_now_time < SOUND_WAIT_TIME) {
// // digitalWrite(SOUND_LED, HIGH);
// Switch = 0;
// }
// else {
// // digitalWrite(SOUND_LED, LOW);
// Switch = 1;
// lineFollow();
// }
// // {
// // Serial.println(analogRead(A13));
// // // Serial.println("SOUND");
// // if (analogRead(A13) >= 800)
// // {
// // lineFollow();
// // }
// // else
// // {
// // analogWrite(ENL, 0);
// // analogWrite(ENR, 0);
// // Serial.println("Sound Detected");
// // delay(300);
// // }
// // }
}
///////////////////////// 7] GUARD AVOIDING///////////////////////////////////////////
else if (pointer == 6)
{
Serial.print("GUARD : ");
if (count < 3)
{
analogWrite(ENL, 0);
analogWrite(ENR, 0);
int ultrasonicDistanceLeft = ultrasonicDistance(SENSOR_LEFT);
int ultrasonicDistanceRight = ultrasonicDistance(SENSOR_RIGHT);
Serial.print(ultrasonicDistanceLeft);
Serial.print(" ");
Serial.print(ultrasonicDistanceRight);
Serial.print(" ");
Serial.print(guard_left);
// Serial.print(" ");
// Serial.print(guard_left_prev);
Serial.print(" ");
Serial.print(guard_right);
// Serial.print(" ");
// Serial.print(guard_right_prev);
Serial.print(" ");
Serial.println(count);
if (ultrasonicDistanceLeft < 20)
{
guard_left = 1;
}
else
{
guard_left = 0;
}
if (ultrasonicDistanceRight < 20)
{
guard_right = 1;
}
else
{
guard_right = 0;
}
delay(300);
if (count == 0 and guard_left == 1 and guard_right == 1)
{
count = 1;
u8g2.clearBuffer(); // clear the internal memory
u8g2.setFont(u8g2_font_ncenB08_tr); // choose a suitable font
u8g2.drawStr(0, 10, "STEP 1"); // write something to the internal memory
u8g2.sendBuffer(); // transfer internal memory to the display
}
if (count == 1 and guard_left == 1 and guard_right == 0)
{
count = 2;
u8g2.clearBuffer(); // clear the internal memory
u8g2.setFont(u8g2_font_ncenB08_tr); // choose a suitable font
u8g2.drawStr(0, 10, "STEP 2"); // write something to the internal memory
u8g2.sendBuffer(); // transfer internal memory to the display
}
if (count == 2 and guard_left == 0 and guard_right == 0)
{
count = 3;
u8g2.clearBuffer(); // clear the internal memory
u8g2.setFont(u8g2_font_ncenB08_tr); // choose a suitable font
u8g2.drawStr(0, 10, "STEP 3"); // write something to the internal memory
u8g2.sendBuffer(); // transfer internal memory to the display
}
}
else
{
u8g2.clearBuffer(); // clear the internal memory
u8g2.setFont(u8g2_font_ncenB08_tr); // choose a suitable font
u8g2.drawStr(0, 10, "Satisfied"); // write something to the internal memory
u8g2.sendBuffer(); // transfer internal memory to the display
Serial.print("Satisfied");
// motor(LEFT_MOTOR,FORWARD);
// motor(RIGHT_MOTOR, FORWARD);
// analogWrite(ENL,230);
// analogWrite(ENR, 170);
// delay(1000);
lineFollow();
if(TDetected()){
motor(LEFT_MOTOR, FORWARD);
motor(RIGHT_MOTOR, FORWARD);
analogWrite(ENL, 230);
analogWrite(ENR, 170);
delay(900);
motor(LEFT_MOTOR, FORWARD);
motor(RIGHT_MOTOR, BACKWARD);
analogWrite(ENL, 255);
analogWrite(ENR, 255);
delay(750);
analogWrite(ENL,0);
analogWrite(ENR, 0);
delay(1000);
pull_bend = true;
}
}
}
else
{
// For safery, the robot will stop at any invalid TASK.
Switch = 0;
}
if (Switch == 1)
{
analogWrite(ENL, Left_drive);
analogWrite(ENR, Right_drive);
}
///////////////////// 8]Tasks completed////////////////////////////////////////
else
{
analogWrite(ENL, 0);
analogWrite(ENR, 0);
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////FUNCTIONS//////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////Updating the IR array elements///////////////////////////////////////////
void readIRArray()
{
for (int i = 0; i < IR_ARRAY_LENGTH - 4; i++)
{
// Serial.print(analogRead(i));
// Serial.print(" ");
if (LINE_COLOR == "WHITE")
{
temp = analogRead(i) <= Ir_thresholds[i];
}
else if (LINE_COLOR == "BLACK")
{
temp = analogRead(i) > Ir_thresholds[i];
}
// IR_array[IR_ARRAY_LENGTH - 1 - i] = temp;
// IR_array[i] = temp; // This is correct if pins are connected in order.
IR_array[i + 2] = temp;
// This part is needed because the pins are connected not in order.
// if (i < 8) {
// IR_array[i + 2] = temp;
// }
// else {
// if (i == 8) IR_array[0] = temp;
// else if (i == 9) IR_array[1] = temp;
// else if (i == 10) IR_array[10] = temp;
// else if (i == 11) IR_array[11] = temp;
// }
}
IR_array[0] = 1 - digitalRead(A11);
IR_array[1] = 1 - digitalRead(A10);
IR_array[10] = 1 - digitalRead(A9);
IR_array[11] = 1 - digitalRead(A8);
// for (int i = 0; i < IR_ARRAY_LENGTH; i++)
// {
// Serial.print(IR_array[i]);
// Serial.print(" ");
// }
// Serial.println();
}
///////////////////////////Ultrasonic sensor reading///////////////////////////////////////////
int ultrasonicDistance(int sensor)
{
int distance = 0;
if (sensor == SENSOR_LEFT)
{
digitalWrite(SENSOR_LEFT_TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(SENSOR_LEFT_TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(SENSOR_LEFT_TRIG_PIN, LOW);
distance = pulseIn(SENSOR_LEFT_ECHO_PIN, HIGH) / 2 * 0.034;
}
else if (sensor == SENSOR_RIGHT)
{
digitalWrite(SENSOR_RIGHT_TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(SENSOR_RIGHT_TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(SENSOR_RIGHT_TRIG_PIN, LOW);
distance = pulseIn(SENSOR_RIGHT_ECHO_PIN, HIGH) / 2 * 0.034;
}
return distance;
}
///////////////////////////Calibarating IR Thresholds///////////////////////////////////////////
void calibrate()
{
motor(RIGHT_MOTOR, BACKWARD);
motor(LEFT_MOTOR, FORWARD);
// Stop motors when calibrating
analogWrite(ENL, 255);
analogWrite(ENR, 255);
// make sensor_max_values array of length 8 equal to sensor calibration array
int sensor_max_values[8] = {0, 0, 0, 0, 0, 0, 0, 0};
// sensor min values array
int sensor_min_values[8] = {0, 0, 0, 0, 0, 0, 0, 0};
int now_time_for_calibration = millis();
Serial.print("now time for calibration: ");
Serial.print(now_time_for_calibration);
Serial.print(" millis: ");
Serial.println(millis());
while (millis() - now_time_for_calibration < 8000)
{
// Serial.println("Caliberating IR");
digitalWrite(buzzer_pin, 1);
bool allZeros = true;
for (int i = 0; i < 8; i++)
{
if (sensor_max_values[i] != 0)
{
allZeros = false;
break; // No need to continue checking if we find a non-zero value
}