-
Notifications
You must be signed in to change notification settings - Fork 0
/
CS117Car.ino
347 lines (304 loc) · 6.93 KB
/
CS117Car.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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
//www.elegoo.com
//2016.09.19
#include <Servo.h> //servo library
#define LT1 digitalRead(10)
#define LT2 digitalRead(4)
#define LT3 digitalRead(2)
//for line begin
#define ENA_line 5
#define ENB_line 11
#define IN1 6
#define IN2 7
#define IN3 8
#define IN4 9
#define ABS_line 150
//line end
#define MIDDLE_ANGLE 130
#define STOP_DIST 40
Servo myservo; // create servo object to control servo
int Echo = A4;
int Trig = A5;
int LED=13;
volatile int state = LOW;
char getstr;
int in1=6;
int in2=7;
int in3=8;
int in4=9;
int ENA=5;
int ENB=11;
int ABS=200;
// 0 - autonomous, 1 - manual, 2 - line following
int operation_mode;
//line begin
void back(){
analogWrite(ENA_line, ABS_line);
analogWrite(ENB_line, ABS_line);
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
Serial.println("go back!");
}
void forward(){
analogWrite(ENA_line, ABS_line);
analogWrite(ENB_line, ABS_line);
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
Serial.println("go forward!");
}
void left(){
analogWrite(ENA_line, ABS_line);
analogWrite(ENB_line, ABS_line);
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
Serial.println("go left!");
}
void right(){
analogWrite(ENA_line, ABS_line);
analogWrite(ENB_line, ABS_line);
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
Serial.println("go right!");
}
void stop(){
digitalWrite(ENA_line, LOW);
digitalWrite(ENB_line, LOW);
Serial.println("Stop!");
}
//line end
void _mBack()
{
analogWrite(ENA,ABS);
analogWrite(ENB,ABS);
digitalWrite(in1,HIGH);//digital output
digitalWrite(in2,LOW);
digitalWrite(in3,LOW);
digitalWrite(in4,HIGH);
Serial.println("Forward");
}
void _mForward()
{
analogWrite(ENA,ABS);
analogWrite(ENB,ABS);
digitalWrite(in1,LOW);
digitalWrite(in2,HIGH);
digitalWrite(in3,HIGH);
digitalWrite(in4,LOW);
Serial.println("Back");
}
void _mleft()
{
analogWrite(ENA,ABS);
analogWrite(ENB,ABS);
digitalWrite(in1,HIGH);
digitalWrite(in2,LOW);
digitalWrite(in3,HIGH);
digitalWrite(in4,LOW);
Serial.println("go left!");
}
void _mright()
{
analogWrite(ENA,ABS);
analogWrite(ENB,ABS);
digitalWrite(in1,LOW);
digitalWrite(in2,HIGH);
digitalWrite(in3,LOW);
digitalWrite(in4,HIGH);
Serial.println("go right!");
}
void _mStop()
{
digitalWrite(ENA,LOW);
digitalWrite(ENB,LOW);
Serial.println("Stop!");
}
/*Ultrasonic distance measurement Sub function*/
int Distance_test()
{
digitalWrite(Trig, LOW);
delayMicroseconds(2);
digitalWrite(Trig, HIGH);
delayMicroseconds(20);
digitalWrite(Trig, LOW);
float Fdistance = pulseIn(Echo, HIGH);
Fdistance= Fdistance/58;
return (int)Fdistance;
}
void stateChange()
{
state = !state;
digitalWrite(LED, state);
}
void setup()
{
pinMode(LED, OUTPUT);
myservo.attach(3);// attach servo on pin 3 to servo object
Serial.begin(9600);
pinMode(Echo, INPUT);
pinMode(Trig, OUTPUT);
pinMode(in1,OUTPUT);
pinMode(in2,OUTPUT);
pinMode(in3,OUTPUT);
pinMode(in4,OUTPUT);
pinMode(ENA,OUTPUT);
pinMode(ENB,OUTPUT);
_mStop();
operation_mode = 1;
myservo.write(MIDDLE_ANGLE);
}
char in;
void loop()
{
// Check bluetooth input values
if (Serial.available())
{
in = Serial.read();
if (in == '0' || in == '1' || in == '2')
{
_mStop();
operation_mode = in - '0';
Serial.println(operation_mode);
if (operation_mode == 1)
{
myservo.write(MIDDLE_ANGLE);
delay(500);
}
else if (operation_mode == 0)
{
myservo.write(MIDDLE_ANGLE);
delay(500);
}
}
}
int middleDistance, rightDistance, leftDistance;
switch (operation_mode)
{
// AUTONOMOUS MODE
case 0:
middleDistance = Distance_test();
#ifdef send
Serial.print("middleDistance=");
Serial.println(middleDistance);
#endif
if(middleDistance<= STOP_DIST)
{
Serial.println("c");
_mStop();
delay(500);
myservo.write(MIDDLE_ANGLE - 90);//10°-180°
delay(1000);
rightDistance = Distance_test();
#ifdef send
Serial.print("rightDistance=");
Serial.println(rightDistance);
#endif
delay(500);
// myservo.write(135);
//delay(1000);
myservo.write(MIDDLE_ANGLE + 90);
delay(1000);
leftDistance = Distance_test();
#ifdef send
Serial.print("leftDistance=");
Serial.println(leftDistance);
#endif
delay(500);
myservo.write(MIDDLE_ANGLE);
delay(1000);
if(rightDistance>leftDistance)
{
_mright();
delay(360);
}
else if(rightDistance<leftDistance)
{
_mleft();
delay(360);
}
else if((rightDistance <= STOP_DIST) || (leftDistance<= STOP_DIST))
{
_mBack();
delay(180);
}
else
{
_mForward();
}
}
else
_mForward();
break;
// MANUAL MODE
case 1:
// check for collision
middleDistance = Distance_test();
#ifdef send
Serial.print("middleDistance=");
Serial.println(middleDistance);
#endif
if (middleDistance <= STOP_DIST)
{
Serial.println("c");
_mStop();
delay(100);
_mright();
delay(850);
_mStop();
}
// read input from bluetooth
if(in =='f')
_mForward();
else if(in =='b')
{
_mBack();
//delay(20);
}
else if(in =='l')
{
_mleft();
//delay(20);
}
else if(in =='r')
{
_mright();
//delay(20);
}
else if(in =='s')
{
_mStop();
}
else if(in =='A')
{
stateChange();
}
break;
// LINE FOLLOWING MODE
case 2:
// check for collision
// read IR sensors
// move based on sensor values
if (LT2 == HIGH) {
forward();
}
//else if(LT1 == HIGH) {
// forward();
// }
else if (LT3 == HIGH) {
forward();
//while (LT3 == HIGH);
//stop();
} else {
right();
}
break;
default:
break;
}
}