-
Notifications
You must be signed in to change notification settings - Fork 2
/
android_arduino_autonomous_vehicle.ino
212 lines (192 loc) · 5.03 KB
/
android_arduino_autonomous_vehicle.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
#define IR A0
#define model 430
#define TRIG_PIN A1
#define ECHO_PIN A2
// Replace sensor numbers with Names
#include <SharpIR.h>
#include<Servo.h>
#include <NewPing.h>
NewPing sonar(TRIG_PIN, ECHO_PIN, 250);
SharpIR SharpIR(IR, model);
Servo front_servo;
Servo rear_servo;
#define RELAY1 10
#define RELAY2 11
Servo ESC;
Servo steer;
void setup() {
Serial.begin(9600);
front_servo.attach(7);
pinMode(RELAY1, OUTPUT);
pinMode(RELAY2, OUTPUT);
ESC.attach(9);
steer.attach(3);
ESC.write(0);
steer.write(110);
Serial.begin(9600);
front_servo.write(125);
pinMode(4, OUTPUT);
}
void loop() {
Serial.flush();
if (Serial.available() > 0)
{
int x = Serial.read();
Serial.print(x);
int distanceR = 0; //
int distanceL = 0;
int USdis = sonar.ping_cm(); //Distance measured from ultrasonic sensor
if (USdis == 0) //If distance is greater than maximum,
{ //ultrasonic sensor reads it as 0. Hence it is made 250
USdis = 250;
}
switch (x)
{
case 1:
goLeft();
break;
case 0:
goStraight();
break;
case 2:
goRight();
break;
case -1:
goStop();
break;
}
if (USdis < 50) //We have kept distance here as 50cm. But as the car is in motion(high speed), the car will stop when the distance is close to 20cm. 50-30 is the threshold.
{
reverse:
digitalWrite(6, LOW); //switches off buzzer
steer.write(110); //steering is made straight
ESC.write(0); //Motor is stopped
digitalWrite(RELAY1, HIGH); // Turns ON Relays - Motor in reverse direction
digitalWrite(RELAY2, HIGH);
// delay(15);
ESC.write(70); //Motor is running in reverse direction
delay(500);
ESC.write(0);
digitalWrite(4, HIGH); //Lights are switched on as the car has stopped
front_servo.write(50); //Ultrasonic sensor mounted Servo is moved right to scan right side
delay(500);
distanceR = sonar.ping_cm(); //Right side distance is measured
if (distanceR == 0)
{
distanceR = 250;
}
Serial.println(distanceR);
delay(1000);
front_servo.write(125);
front_servo.write(170); //Ultrasonic sensor mounted Servo is moved left to scan left side
delay(500);
distanceL = sonar.ping_cm(); //Left side distance is measured
if (distanceL == 0)
{
distanceL = 250;
}
Serial.println(distanceL);
delay(1000);
front_servo.write(125);
digitalWrite(4, LOW);
int dis;
if (distanceR > distanceL) //Condition to check if right side distance is greater tha left side distance
{
dis = sonar.ping_cm();
if (dis == 0)
{
dis = 250;
}
delay(500);
if (dis > 50)
{
goRight();
delay(1000);
}
while (dis < 50)
{
dis = sonar.ping_cm();
if (dis == 0)
{
dis = 250;
}
delay(500);
goRight();
Serial.println("inLoop");
if (dis < 15) //Car cannot make a turn if obstacle distance is less than 15
{
goto reverse; //So the car moves back a little, and then completes the turn
}
}
}
else
{
dis = sonar.ping_cm();
if (dis == 0)
{
dis = 250;
}
delay(500);
if (dis > 50)
{
goLeft();
delay(1000);
}
while (dis < 50)
{
goLeft();
Serial.println("inLoop");
if (dis < 15) //Car cannot make a turn if obstacle distance is less than 15
{
goto reverse; //So the car moves back a little, and then completes the turn
}
}
}
}
/* int IRdis = SharpIR.distance();
if (IRdis < 10)
{
Serial.print("IR");
goStop();
}*/
}
Serial.flush();
}
void goLeft()
{
digitalWrite(4, LOW);
Serial.println("Left");
digitalWrite(RELAY1, LOW); // Turns OFF Relays - Motor in forward direction
digitalWrite(RELAY2, LOW);
ESC.write(68);
steer.write(85);
// delay(500);
}
void goRight()
{
digitalWrite(4, LOW);
Serial.println("Right");
digitalWrite(RELAY1, LOW); // Turns OFF Relays - Motor in forward direction
digitalWrite(RELAY2, LOW);
ESC.write(68);
steer.write(135);
// delay(500);
}
void goStraight()
{ digitalWrite(RELAY1, LOW); // Turns OFF Relays - Motor in forward direction
digitalWrite(RELAY2, LOW);
delay(15);
digitalWrite(4, LOW);
Serial.println("RUN");
ESC.write(68);
steer.write(110);
// delay(500);
}
void goStop()
{
digitalWrite(4, HIGH);
Serial.println("STOP");
ESC.write(0);
steer.write(110);
delay(1000);
}