-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNavigation.ino
150 lines (126 loc) · 3.37 KB
/
Navigation.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
#include <Servo.h>
Servo servoLeft;
Servo servoRight;
const int rightSensor = 47;
const int middleSensor = 51;
const int leftSensor = 52;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(rightSensor, OUTPUT);
pinMode(middleSensor, OUTPUT);
pinMode(leftSensor, OUTPUT);
servoLeft.attach(11);
servoRight.attach(12);
//servoLeft.writeMicroseconds(1300);
//servoRight.writeMicroseconds(1700);
//delay(100);
//pinMode(2, OUTPUT);
//digitalWrite(2, LOW);
}
void loop() {
bool colorRight = RCtime(rightSensor);
bool colorMid = RCtime(middleSensor);
bool colorLeft = RCtime(leftSensor);
while (colorMid==1 && colorRight==0 && colorLeft==0){
forward();
colorRight = RCtime(rightSensor);
colorMid = RCtime(middleSensor);
colorLeft = RCtime(leftSensor);
}
if(colorMid==1 && colorRight==1 && colorLeft==1){
servoLeft.writeMicroseconds(1500);
servoRight.writeMicroseconds(1500);
delay(1000);
forward();
}
if (!colorMid && !colorLeft && !colorRight) {
servoLeft.writeMicroseconds(1500);
servoRight.writeMicroseconds(1500);
}
while(colorRight==0 && colorMid==0 && colorLeft==1){
// backLeft();
// left();
// forward();
circleLeft();
colorRight = RCtime(rightSensor);
colorMid = RCtime(middleSensor);
colorLeft = RCtime(leftSensor);
}
while(colorRight==1 && colorLeft==0 && colorMid==0){
// backRight();
// right();
// forward();
circleRight();
colorRight = RCtime(rightSensor);
colorMid = RCtime(middleSensor);
colorLeft = RCtime(leftSensor);
}
delay(200);
}
//White <100; Black >= 100
bool RCtime(int sensPin){
bool ans = false;
long result = 0;
pinMode(sensPin, OUTPUT); // make pin OUTPUT
digitalWrite(sensPin, HIGH); // make pin HIGH to discharge capacitor - study the schematic
delay(1); // wait a ms to make sure cap is discharged
pinMode(sensPin, INPUT); // turn pin into an input and time till pin goes low
digitalWrite(sensPin, LOW); // turn pullups off - or it won't work
while(digitalRead(sensPin)){ // wait for pin to go low
result++;
}
Serial.print(result);
Serial.print(" ");
Serial.print(sensPin);
Serial.println();
if(result >= 120){
return true;
}
return false;
}
void forward(){
servoLeft.writeMicroseconds(1000);
servoRight.writeMicroseconds(2000);
delay(10);
}
void forwardLeft(){
servoLeft.writeMicroseconds(1300);
servoRight.writeMicroseconds(2000);
delay(10);
}
void forwardRight(){
servoLeft.writeMicroseconds(1000);
servoRight.writeMicroseconds(1700);
delay(10);
}
void backLeft(){
servoLeft.writeMicroseconds(1800);
servoRight.writeMicroseconds(1400);
delay(50);
}
void backRight(){
servoLeft.writeMicroseconds(1600);
servoRight.writeMicroseconds(1200);
delay(50);
}
void left(){
servoLeft.writeMicroseconds(1500);
servoRight.writeMicroseconds(1700);
delay(1000);
}
void right(){
servoLeft.writeMicroseconds(1300);
servoRight.writeMicroseconds(1500);
delay(1000);
}
void circleLeft(){
servoLeft.writeMicroseconds(1400);
servoRight.writeMicroseconds(2000);
delay(1000);
}
void circleRight(){
servoLeft.writeMicroseconds(1000);
servoRight.writeMicroseconds(1600);
delay(1000);
}