-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathobstacle avoidance.ino
195 lines (173 loc) · 4.01 KB
/
obstacle avoidance.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
#include <Servo.h>;
Servo myservo;
long duration, inches, cm, dist1 , dist2 , a; // function from datasheet of ultrasonic sensor
int set_length=4;
int servopin=9;
int LeftMotorForward = 7;
int LeftMotorReverse = 6;
int RightMotorForward = 5;
int RightMotorReverse = 4;
int trigPin =3;
int echoPin =2;
void off()
{
digitalWrite(RightMotorForward, LOW);
digitalWrite(RightMotorReverse, LOW);
digitalWrite(LeftMotorForward, LOW);
digitalWrite(LeftMotorReverse, LOW);
delay(500);
//return;
}
void setup()
{
Serial.begin(9600);
myservo.attach(servopin);
pinMode(LeftMotorForward, OUTPUT);
pinMode(RightMotorForward, OUTPUT);
pinMode(LeftMotorReverse, OUTPUT);
pinMode(RightMotorReverse, OUTPUT);
pinMode(echoPin,INPUT);
pinMode(7, OUTPUT);
pinMode(5, OUTPUT);
analogWrite(7, 255); // Faster motor at 100% of its full speed
analogWrite(5, 255); // Slower motor at 100% of its full speed
delay(1000);
}
int ultra()
{
//long duration, inches, cm;
pinMode(trigPin, OUTPUT);
digitalWrite(trigPin, LOW);
delayMicroseconds(10);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
//pinMode(echoPin,INPUT);
duration = pulseIn(echoPin, HIGH);
inches = microsecondsToInches(duration);
cm=microsecondsToCentimeters(duration);
//delay
Serial.print(inches);
Serial.print("in, ");
Serial.print(cm);
Serial.print("cm");
Serial.println();
delay(100);
return round(inches);
}
void loop(){
Driveforward(); // if nothing is wrong the go forward.
int distance = ultra(); // us the ultra() function to see if anything is ahead.
if (distance<= set_length){
Reverse(); // If something is ahead, move back
char turndirection = scan(); //Decide which direction to turn.
switch (turndirection){
case 'l':
Leftturn();
break;
case 'r':
Rightturn();
break;
}
}
}
/* if(inches<=set_length) // if there is obstacle at 15 inches or below
{
Reverse();
Rightturn();
dist1=microsecondsToInches(a);
Leftcheck();
dist2=microsecondsToInches(a);
Rightturn();
if(dist1>dist2)
{
Rightturn();
off();
}
if(dist1<dist2)
{
Leftturn();
off();
}
}
else
{
Driveforward();
}
}*/
long microsecondsToInches(long microseconds)
{
return a=microseconds / 74 / 2;
}
long microsecondsToCentimeters(long microseconds)
{
return microseconds / 29 / 2;
}
void Reverse(){
digitalWrite(RightMotorForward, LOW);
digitalWrite(RightMotorReverse, HIGH);
digitalWrite(LeftMotorForward, LOW);
digitalWrite(LeftMotorReverse, HIGH);
delay(2000);
off();
//return;
}
void Rightturn(){
digitalWrite(RightMotorForward, LOW );
digitalWrite(LeftMotorForward, HIGH);
digitalWrite(LeftMotorReverse, LOW );
digitalWrite(RightMotorReverse, HIGH );
delay(1500);
off();
//return;
}
void Leftturn(){
digitalWrite(RightMotorForward, HIGH );
digitalWrite(LeftMotorForward, LOW);
digitalWrite(LeftMotorReverse, HIGH );
digitalWrite(RightMotorReverse, LOW );
delay(1250);
off();
//return;
}
void Leftcheck(){
digitalWrite(RightMotorForward, HIGH );
digitalWrite(LeftMotorForward, LOW);
digitalWrite(LeftMotorReverse, HIGH );
digitalWrite(RightMotorReverse, LOW );
delay(8500);
off();
return;
}
void Driveforward(){
digitalWrite(RightMotorForward, HIGH);
digitalWrite(RightMotorReverse, LOW);
digitalWrite(LeftMotorForward, HIGH);
digitalWrite(LeftMotorReverse, LOW);
delay(500);
//off();
//return;
}
char scan(){
int leftscanval, rightscanval;
char choice;
myservo.writeMicroseconds(600);
delay(1000); // //Look left
leftscanval = ultra();
myservo.write(2500);
delay(1000); // //Look right
rightscanval = ultra();
myservo.writeMicroseconds(1500);
delay(1000);
myservo.detach();
myservo.attach(9);
if (leftscanval>rightscanval){
choice = 'l';
}
if (rightscanval>leftscanval){
choice = 'r';
}
Serial.print("Choice: ");
Serial.println(choice);
return choice;
}