-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathall.ino
454 lines (390 loc) · 8.96 KB
/
all.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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
int i=1;
const int trigPin = 7;
const int echoPin = 8;
//line follower
#define r_sensor A0
#define m_sensor A1
#define l_sensor A2
#define MAX 75
#define kp 15
#define kd 5
int speedA =MAX ;int speedB =MAX;
int lasterror = 0;
int error = 0;
// defines variables
long duration;
int distance;
int number=0;String fullno="";String str;
// Setup Motor A (front and rear) pins
#define PIN_R1 5
#define PIN_R2 6
#define PIN_R_enable 3
// Setup Motor B (front and rear) pins
#define PIN_L1 9
#define PIN_L2 10
#define PIN_L_enable 11
// Setup Bluetooth
#define TX 1
#define RX 0
unsigned long timer0 = 2000; //Stores the time (in millis since execution started)
unsigned long timer1 = 0; //Stores the time when the last command was received from the phone
// Define Attributes
#define NO_COMMAND 'X'
#define RESET 'C'
#define FORWARD 'F'
#define FORWARD_RIGHT 'I'
#define RIGHT 'R'
#define BACKWORD_RIGHT 'J'
#define BACKWORD 'B'
#define BACKWORD_LEFT 'H'
#define LEFT 'L'
#define FORWARD_LEFT 'G'
#define BREAK ' '
#define FOLLOWLINE 'W'
#define STOPFOLLOWING 'w'
#define STOP 'S'
#define GEAR_1 '0'
#define GEAR_2 '3'
#define GEAR_3 '5'
#define GEAR_4 '8'
#define GEAR_5 'q'
char prevCommand = NO_COMMAND;
char command = RESET;
int speed = 0;
char state = STOP;
void setup() {
// The setup code goes here and runs once only
Serial.begin(9600);
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
// Configure the pin modes for each drive motor
pinMode (PIN_R1, OUTPUT);
pinMode (PIN_R2, OUTPUT);
pinMode (PIN_L1, OUTPUT);
pinMode (PIN_L2, OUTPUT);
pinMode (PIN_R_enable, OUTPUT);
pinMode (PIN_L_enable, OUTPUT);
digitalWrite (PIN_L_enable, HIGH);
digitalWrite (PIN_R_enable, HIGH);
pinMode (r_sensor, INPUT);
pinMode (m_sensor, INPUT);
pinMode (l_sensor, INPUT);
}
void loop() {
// Main code goes here and will run repeatedly:
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance= duration*0.034/2;
// Prints the distance on the Serial Monitor
if (Serial.available()) {
delay(10); //Delay added to make thing stable
timer1 = millis();
command = Serial.read();
Serial.println(command);
//Change pin mode only if new command is different from previous.
if (command != prevCommand) {
setState(command);
}
}
else {
timer0 = millis(); //Get the current time (millis since execution started).
//Check if it has been 60000ms since we received last command.
if ((timer0 - timer1) > 60000) {
//More tan 60000ms have passed since last command received, car is out of range.
//Therefore stop the car.
breakRobot(1);
coast(0);
}
}
if(distance<25)
{backward(1000,100);
breakRobot(100);}
}
void setSpeed(char gear) {
if (gear == GEAR_1) {
speed = 100;
} else if (gear == GEAR_2) {
speed = 140;
} else if (gear == GEAR_3) {
speed = 180;
} else if (gear == GEAR_4) {
speed = 220;
} else if (gear == GEAR_5) {
speed = 255;
}
}
void setState(char command) {
switch (command) {
case'-':
breakRobot(4000);
forward(1700,70);
breakRobot(500);
while(1)
{left(13,255,255);
breakRobot(250);
i++;Serial.println(i);
if(i>45){ i=1;
break;}
}
forward(1700,70);
breakRobot(500);
while(1)
{left(13,255,255);
breakRobot(250);
i++;Serial.println(i);
if(i>45) { i=1;
break;}
}
forward(1700,70);
breakRobot(500);
while(1)
{left(13,255,255);
breakRobot(250);
i++;Serial.println(i);
if(i>45) { i=1;
break;}
}
forward(1700,70);
breakRobot(500);
while(1)
{left(13,255,255);
breakRobot(250);
i++;Serial.println(i);
if(i>45) { i=1;
break;}
}
breakRobot(2000);
motorBforward(255);
motorAforward(120);
delay(3780);
breakRobot(2000);
motorAforward(255);
motorBforward(100);
delay(3000);
motorBforward(255);
motorAforward(100);
delay(2700);
breakRobot(10);
break;
case 't'://angle
while(1){if(Serial.available()){
command=Serial.read();
str=String(command);
if (isDigit(command)==0)break;
Serial.println(str);
fullno=fullno+str;
Serial.println(fullno);
}
}number=fullno.toInt();
Serial.println(number);fullno="";
while(1){int i=1;
while(1)
{left(13,255,255);
breakRobot(250);
i++;Serial.println(i);
if(i>number/2) break;
}number=0;break;}
breakRobot(1);
coast(0);
break;
case 's'://distance
while(1){if(Serial.available()){
command=Serial.read();
str=String(command);
if (isDigit(command)==0)break;
Serial.println(str);
fullno=fullno+str;
Serial.println(fullno);
}
}number=fullno.toInt();
Serial.println(number);fullno="";
while(1){forward(22*number,100);number=0;break;}
breakRobot(10);
break;
//linefollower
case FOLLOWLINE:
while(1){
followLine();
if (Serial.available())command=Serial.read();
if(command==STOPFOLLOWING) break;
}breakRobot(10);
break;
case RESET:
breakRobot(1);
coast(0);
speed = 0;
state = STOP;
break;
case BREAK:
state = BREAK;
breakRobot(1);
break;
case STOP:
state = STOP;
coast(0);
break;
case GEAR_1:
setSpeed(GEAR_1);
setState(state);
break;
case GEAR_2:
setSpeed(GEAR_2);
setState(state);
break;
case GEAR_3:
setSpeed(GEAR_3);
setState(state);
break;
case GEAR_4:
setSpeed(GEAR_4);
setState(state);
break;
case GEAR_5:
setSpeed(GEAR_5);
setState(state);
break;
case FORWARD:
state = FORWARD;
forward(1, speed);
break;
case FORWARD_RIGHT:
state = FORWARD_RIGHT;
right(1, 0.5 * speed, speed);
break;
case RIGHT:
state = RIGHT;
right(1, speed, speed);
break;
case BACKWORD_RIGHT:
state = BACKWORD_RIGHT;
left(1, 0.5 * speed, speed);
break;
case BACKWORD:
state = BACKWORD;
backward(1, speed);
break;
case BACKWORD_LEFT:
state = BACKWORD_LEFT;
right(1, speed, 0.5 * speed);
break;
case LEFT:
state = LEFT;
left(1, speed, speed);
break;
case FORWARD_LEFT:
state = FORWARD_LEFT;
left(1, speed, 0.5 * speed);
}
prevCommand = command;
}
// Create motor functions
void motorAforward(int speed) {
analogWrite (PIN_R1, speed);
digitalWrite (PIN_R2, LOW);
}
void motorBforward(int speed) {
digitalWrite (PIN_L1, LOW);
analogWrite (PIN_L2, speed);
}
void motorAbackward(int speed) {
digitalWrite (PIN_R1, LOW);
analogWrite (PIN_R2, speed);
}
void motorBbackward(int speed) {
analogWrite (PIN_L1, speed);
digitalWrite (PIN_L2, LOW);
}
void motorAstop() {
digitalWrite (PIN_R1, HIGH);
digitalWrite (PIN_R2, HIGH);
}
void motorBstop() {
digitalWrite (PIN_L1, HIGH);
digitalWrite (PIN_L2, HIGH);
}
void motorAcoast() {
digitalWrite (PIN_R1, LOW);
digitalWrite (PIN_R2, LOW);
}
void motorBcoast() {
digitalWrite (PIN_L1, LOW);
digitalWrite (PIN_L2, LOW);
}
// Setup movement functions
void forward (int duration, int speed) {
motorAforward(speed);
motorBforward(speed);
delay (duration);
}
void backward (int duration, int speed) {
motorAbackward(speed);
motorBbackward(speed);
delay (duration);
}
void left (int duration, int speedA, int speedB) {
motorAbackward(speedA);
motorBforward(speedB);
delay (duration);
}
void right (int duration, int speedA, int speedB) {
motorAforward(speedA);
motorBbackward(speedB);
delay (duration);
}
void coast (int duration) {
motorAcoast();
motorBcoast();
delay (duration);
}
void breakRobot (int duration) {
motorAstop();
motorBstop();
delay (duration);
}
void followLine(){
int sensor_R = digitalRead(r_sensor);
int sensor_M = digitalRead(m_sensor);
int sensor_L = digitalRead(l_sensor);
lasterror = error;
if(error == -1){
right(1,speedA,speedB);
}
else if (error == 1 ){
left(1,speedA,speedB);
}
else if(error == 0){
forward(1,MAX);
}
if(sensor_R == 1 && sensor_M == 1 && sensor_L == 0){
error = 2;
}else if(sensor_R == 1 && sensor_M == 0 && sensor_L == 0){
error = 1;
}else if(sensor_R == 1 && sensor_M == 0 && sensor_L == 1){
error = 0;
}else if(sensor_R == 0 && sensor_M == 0 && sensor_L == 1){
error = -1;
}else if(sensor_R == 0 && sensor_M == 1 && sensor_L == 1){
error = -2;
}else if(sensor_R == 1 && sensor_M == 1 && sensor_L == 1){
error = 0;}
if(error == -1 ){
right(1,speedA,speedB);
}
else if (error == 1 ){
left(1,speedA,speedB);
}
else if(error == 0 ){
forward(1,MAX);
}
//////////////////////////////// INVERTED LOGIC MOTORA --- > right
speedA = MAX - error*kp - kd*lasterror;
speedB = MAX + error*kp + kd*lasterror;
}