-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3in1-arduino-car.ino
330 lines (282 loc) · 7.12 KB
/
3in1-arduino-car.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
#include <NewPing.h>
#include <Servo.h>
//define pins
#define motor1Control 9 //right wheel
#define motor2Control 7 // left wheel
#define motor1ControlR 8 //right wheel reverse
#define motor2ControlR 6 // left wheel reverse
#define motor1Speed 10 //right wheel speed
#define motor2Speed 5 // left wheel speed
#define trigPin A0
#define echoPin A1
#define servoPin 11 //pwm
#define ir1Pin 12 //right IR
#define ir2Pin 13 //left IR
#define autocon '0'
#define bluetoothCon '1'
#define lineFollow '2'
NewPing sonar (trigPin, echoPin, 200);
//define global variables
int maxSpeed = 255;
long duration;
int i, line, distance;
int safeDistance = 45;
int angle = 90;
String voice = "";
char state = bluetoothCon;
int ultraSonicDistance() {
delay(70);
distance = sonar.ping_cm();
if (distance == 0) {
distance = 250;
}
//Serial.print("distance:");
Serial.println(distance);
return distance;
}
int irScan()
{
int ir1, ir2;
ir1 = digitalRead(ir1Pin);
ir2 = digitalRead(ir2Pin);
// returns ab: a left motor, b right motor
return (ir2Pin * 10 + ir1Pin);
}
char blueTooth() {
char message;
if (Serial.available() > 0) message = Serial.read();
return message;
}
//class that controls car movement (left,right,forward,backward,stop)
class CarMove {
private:
int motorDir; //1 for forward, 0 for backwards
public:
void right() {
analogWrite(motor1Speed, 0);
analogWrite(motor2Speed, maxSpeed);
}
void left() {
analogWrite(motor1Speed, maxSpeed);
analogWrite(motor2Speed, 0);
}
void forward() {
motorDir = 1;
analogWrite(motor1Speed, maxSpeed);
analogWrite(motor2Speed, maxSpeed);
digitalWrite(motor1ControlR, LOW);
digitalWrite(motor2ControlR, LOW);
digitalWrite(motor1Control, HIGH);
digitalWrite(motor2Control, HIGH);
}
void backward() {
motorDir = 0;
analogWrite(motor1Speed, maxSpeed);
analogWrite(motor2Speed, maxSpeed);
digitalWrite(motor1ControlR, HIGH);
digitalWrite(motor2ControlR, HIGH);
digitalWrite(motor1Control, LOW);
digitalWrite(motor2Control, LOW);
//Serial.println("backward");
}
void stop() {
//check for previous stage then go opposite direction then stops
switch(motorDir){
case 1:
backward();
delay(500);
analogWrite(motor2Speed, 0);
analogWrite(motor1Speed, 0);
digitalWrite(motor1ControlR, LOW);
digitalWrite(motor2ControlR, LOW);
digitalWrite(motor1Control, LOW);
digitalWrite(motor2Control, LOW);
break;
case 0:
forward();
delay(500);
analogWrite(motor2Speed,0);
analogWrite(motor1Speed,0);
digitalWrite(motor1ControlR,LOW);
digitalWrite(motor2ControlR,LOW);
digitalWrite(motor1Control,LOW);
digitalWrite(motor2Control,LOW);
break;
}
}
};
////////////////////////////////////////////////////
//define objects
Servo myservo;
CarMove carMove;
void voicecontrol () {
//get characters to form string
while (Serial.available()) {
delay(10);
char c = Serial.read ();
if (c == '#') {
break;
}
voice += c;
}
//do commands
if (voice.length() > 0) {
Serial.println(voice);
if (voice == "*forward")
{
carMove.forward();
}
else if (voice == "*backwards")
{
carMove.backward ();
}
else if (voice == "*left") {
carMove.left() ;
}
else if (voice == "*right" || voice == "*write"||voice == "*bright")
{
carMove.right();
}
else if (voice == "*stop")
{
carMove.stop();
}
}
voice = "";
}
void scanTwiceNTurn(int degree) {
int distance1 = 0, distance2 = 0;
//Serial.println("start scan");
carMove.stop();
//scans right side
myservo.write(degree);
delay(1000);
distance1 = ultraSonicDistance();
//delay(100);
//scans left side
myservo.write(180 - degree);
delay(1000);
distance2 = ultraSonicDistance();
//delay(100);
//resets servo
myservo.write(75);
//compares the reads of each side then turns depending on it
carMove.backward();
if (distance1 >= distance2) {
while (distance < safeDistance) {
carMove.right();
delay(150);
distance = ultraSonicDistance();
}
}
else {
while (distance < safeDistance) {
carMove.left();
delay(150);
distance = ultraSonicDistance();
}
}
}
void setup() {
//init ultra sonic pins
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
//init motor control pins
pinMode(motor1Control, OUTPUT);
pinMode(motor1ControlR, OUTPUT);
pinMode(motor2Control, OUTPUT);
pinMode(motor2ControlR, OUTPUT);
pinMode(motor1Speed, OUTPUT);
pinMode(motor2Speed, OUTPUT);
//init servo
myservo.attach(servoPin);
myservo.write(75);
//init ir sensors
pinMode(ir1Pin , INPUT);
pinMode(ir2Pin , INPUT);
//init //Serial
Serial.begin(9600);
}
void loop() {
//auto control
switch (state) {
//auto control
case autocon:
angle = 75;
//Serial.println("autoMode");
myservo.write(75);
distance = ultraSonicDistance();
carMove.forward();
//obstacle avoidance
if (distance > safeDistance) carMove.forward();
else scanTwiceNTurn(40);
//check for bluetooth if avaible
if ( blueTooth() == bluetoothCon) state = bluetoothCon;
if ( blueTooth() == lineFollow) state = lineFollow;
break;
//bluetooth control
case bluetoothCon:
voicecontrol();
//Serial.println("bluetoothMode");
switch (blueTooth()) {
case 'F':
carMove.forward();
break;
case 'S':
carMove.stop();
break;
case 'B':
carMove.backward();
break;
case 'R':
carMove.right();
break;
case 'L':
carMove.left();
break;
case '1': ///////////// make attention here ( O )
state = autocon;
break;
case '2':
state = lineFollow;
break;
//control servo for fun
case 'X':
angle += 10;
myservo.write(angle);
break;
case 'Y':
angle -= 10;
myservo.write(angle);
break;
}
break;
//follow line
case lineFollow:
carMove.forward();
line = irScan();
switch ( line ) {
case 11 : //line ok
break;
case 10 : //line is on left
while (distance > safeDistance && irScan() == line) {
carMove.left();
distance = ultraSonicDistance();
}
break;
case 01 : //line is on right
while (distance > safeDistance && irScan() == line) {
carMove.right();
distance = ultraSonicDistance();
}
break;
case 00 : //if car loses line, switch into bluetooth mode
state = bluetoothCon;
break;
}
//check for bluetooth if avaible
if ( blueTooth() == bluetoothCon) state = bluetoothCon;
if ( blueTooth() == lineFollow) state = lineFollow;
break;
}
}