-
Notifications
You must be signed in to change notification settings - Fork 0
/
Project.ino
548 lines (455 loc) · 11.9 KB
/
Project.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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
#include "MeMCore.h"
#define RGBWait 200
#define LDRWait 10
#define TOLERANCE 0.8 // cm
MeLightSensor lightSensor(PORT_6);
MeRGBLed led(PORT_7);
MeDCMotor motor1(M1);
MeDCMotor motor2(M2);
MeBuzzer buzzer;
MeLineFollower lineFinder(2);
MeUltrasonicSensor ultraSensor(PORT_3);
int red = 0;
int green = 0;
int blue = 0;
float colourArray[] = {0, 0, 0};
float whiteArray[] = {800.00, 800.00, 800.00};
float blackArray[] = {200.00, 200.00, 200.00};
float greyDiff[] = {600.00, 600.00, 600.00};
double distance = 0.00;
double distanceThreshold = 4.0; // for ultrasonic sensor
uint8_t slow_speed = 200; // when adjusting with proximity sensor
uint8_t normal_speed = 250; // when moving and turning
#define NOTE_B0 31
#define NOTE_C1 33
#define NOTE_CS1 35
#define NOTE_D1 37
#define NOTE_DS1 39
#define NOTE_E1 41
#define NOTE_F1 44
#define NOTE_FS1 46
#define NOTE_G1 49
#define NOTE_GS1 52
#define NOTE_A1 55
#define NOTE_AS1 58
#define NOTE_B1 62
#define NOTE_C2 65
#define NOTE_CS2 69
#define NOTE_D2 73
#define NOTE_DS2 78
#define NOTE_E2 82
#define NOTE_F2 87
#define NOTE_FS2 93
#define NOTE_G2 98
#define NOTE_GS2 104
#define NOTE_A2 110
#define NOTE_AS2 117
#define NOTE_B2 123
#define NOTE_C3 131
#define NOTE_CS3 139
#define NOTE_D3 147
#define NOTE_DS3 156
#define NOTE_E3 165
#define NOTE_F3 175
#define NOTE_FS3 185
#define NOTE_G3 196
#define NOTE_GS3 208
#define NOTE_A3 220
#define NOTE_AS3 233
#define NOTE_B3 247
#define NOTE_C4 262
#define NOTE_CS4 277
#define NOTE_D4 294
#define NOTE_DS4 311
#define NOTE_E4 330
#define NOTE_F4 349
#define NOTE_FS4 370
#define NOTE_G4 392
#define NOTE_GS4 415
#define NOTE_A4 440
#define NOTE_AS4 466
#define NOTE_B4 494
#define NOTE_C5 523
#define NOTE_CS5 554
#define NOTE_D5 587
#define NOTE_DS5 622
#define NOTE_E5 659
#define NOTE_F5 698
#define NOTE_FS5 740
#define NOTE_G5 784
#define NOTE_GS5 831
#define NOTE_A5 880
#define NOTE_AS5 932
#define NOTE_B5 988
#define NOTE_C6 1047
#define NOTE_CS6 1109
#define NOTE_D6 1175
#define NOTE_DS6 1245
#define NOTE_E6 1319
#define NOTE_F6 1397
#define NOTE_FS6 1480
#define NOTE_G6 1568
#define NOTE_GS6 1661
#define NOTE_A6 1760
#define NOTE_AS6 1865
#define NOTE_B6 1976
#define NOTE_C7 2093
#define NOTE_CS7 2217
#define NOTE_D7 2349
#define NOTE_DS7 2489
#define NOTE_E7 2637
#define NOTE_F7 2794
#define NOTE_FS7 2960
#define NOTE_G7 3136
#define NOTE_GS7 3322
#define NOTE_A7 3520
#define NOTE_AS7 3729
#define NOTE_B7 3951
#define NOTE_C8 4186
#define NOTE_CS8 4435
#define NOTE_D8 4699
#define NOTE_DS8 4978
double Vout_to_dist(double Vout)
{
//NEED TO RECALIBRATE RELATION BET Vout & DISTANCE
//DEPENDING ON ACTUAL SENSOR DATA
return (Vout * 5 / 1024) + 0.63;
//return (Vout*5/1024 * 0.5) + 0.9; //distance in cm (get relation for characterization graph)
}
double left_IR_V = analogRead(A1); // left IR Vout
double right_IR_V = analogRead(A0); // right IR Vout
double left_IR_dist = Vout_to_dist(left_IR_V);
double right_IR_dist = Vout_to_dist(right_IR_V);
void calibrate() {
// for initiale colour calibration
Serial.println("Put White Sample For Calibration ...");
delay(5000); //delay for five seconds for getting sample ready
for (int i = 0; i <= 2; i++) {
colour(i);
delay(RGBWait);
whiteArray[i] = getAvgReading(5); //scan 5 times and return the average,
colour(3);
delay(RGBWait);
}
Serial.println("Put Black Sample For Calibration ...");
delay(5000); //delay for five seconds for getting sample ready
for (int i = 0; i <= 2; i++) {
colour(i);
delay(RGBWait);
blackArray[i] = getAvgReading(5);
colour(3);
delay(RGBWait);
greyDiff[i] = whiteArray[i] - blackArray[i];
}
Serial.println("White: ");
for (int i = 0; i <= 2; i++) {
Serial.println(whiteArray[i]);
}
Serial.println("Black: ");
for (int i = 0; i <= 2; i++) {
Serial.println(blackArray[i]);
}
}
int getAvgReading(int times) {
int reading;
int total = 0;
for (int i = 0; i < times; i++) {
reading = lightSensor.read();
total = reading + total;
delay(LDRWait);
}
return total / times;
}
char whatColour() {
for (int c = 0; c <= 2; c++) {
colour(c);
delay(RGBWait);
colourArray[c] = getAvgReading(25);
colourArray[c] = (colourArray[c] - blackArray[c]) / (greyDiff[c]) * 255;
colour(3);
delay(RGBWait);
}
if (colourArray[2] < 50.00 && colourArray[1] < 50.00 && colourArray[0] < 50.00) {
//all RGB readings are lesser than 50.0. Colour is Black
return 'E';
}
else if (colourArray[1] > colourArray[0] && colourArray[1] > colourArray[2] ) {
// Green > Red && Green > Blue. Colour is Green
return 'G';
}
else if (colourArray[0] > colourArray[2] && colourArray[1] > colourArray[2] && colourArray[1] >= 120.00) {
// Red > Blue && Green > Blue && Blue >= 120.00. Colour is Yellow
return 'Y';
}
else if (colourArray[0] > colourArray[1] && colourArray[0] > colourArray[2] ) {
//Red > Green && Red > Blue. Colour is Red
return 'R';
}
else if (colourArray[2] > colourArray[0] && colourArray[1] >= colourArray[0] ) {
// Blue > Red && Green >= Red . Colour is Blue
return 'B';
}
else if (colourArray[2] > colourArray[0] && colourArray[0] > colourArray[1] && colourArray[1] < 175.00 ) {
// Blue > Red && Red > Green && Green < 175.00 . Colour is Purple
return 'P';
}
}
void colour(int x) {
led.setColorAt(0, 0, 0, 0);
led.show();
if (x == 0) {
uint8_t red = 255;
uint8_t green = 0;
uint8_t blue = 0;
led.setColorAt(0, red, green, blue);
led.show();
}
else if (x == 1) {
uint8_t red = 0;
uint8_t green = 255;
uint8_t blue = 0;
led.setColorAt(0, red, green, blue);
led.show();
}
else if (x == 2) {
uint8_t red = 0;
uint8_t green = 0;
uint8_t blue = 255;
led.setColorAt(0, red, green, blue);
led.show();
}
else if (x == 3) {
uint8_t red = 0;
uint8_t green = 0;
uint8_t blue = 0;
led.setColorAt(0, red, green, blue);
led.show();
}
}
void move_forward() {
// for bot to move forward
motor1.run(-normal_speed);
motor2.run(normal_speed);
}
void adjust()
{
//bot was moving initially
move_forward();
double left_IR_V = analogRead(A1); // left IR Vout
double right_IR_V = analogRead(A0); // right IR Vout
double left_IR_dist = Vout_to_dist(left_IR_V);
double right_IR_dist = Vout_to_dist(right_IR_V);
//readjust
if (left_IR_dist < 4 - TOLERANCE) //bot is too close to the left
{
motor1.stop();
motor2.stop();
//bot would have moved while stopping, need to recompute distance
left_IR_V = analogRead(A1); // left IR Vout
right_IR_V = analogRead(A0); // right IR Vout
left_IR_dist = Vout_to_dist(left_IR_V);
right_IR_dist = Vout_to_dist(right_IR_V);
while (left_IR_dist < 4 - TOLERANCE)
{
//turn towards right slowly
motor1.run(-slow_speed);
motor2.run(-slow_speed);
//recompute distance for loop exit condition
left_IR_V = analogRead(A1); // left IR Vout
right_IR_V = analogRead(A0); // right IR Vout
left_IR_dist = Vout_to_dist(left_IR_V);
right_IR_dist = Vout_to_dist(right_IR_V);
}
motor1.stop();
motor2.stop();
motor1.run(-normal_speed);
motor2.run(normal_speed);
}
else if ((right_IR_dist < 4 - TOLERANCE) )//bot is too close to the left
{
motor1.stop();
motor2.stop();
//bot would have moved while stopping, need to recompute distance
left_IR_V = analogRead(A1); // left IR Vout
right_IR_V = analogRead(A0); // right IR Vout
left_IR_dist = Vout_to_dist(left_IR_V);
right_IR_dist = Vout_to_dist(right_IR_V);
while (right_IR_dist < 4 - TOLERANCE)
{
//turn toward left slowly
motor1.run(slow_speed);
motor2.run(slow_speed);
//recompute distance for loop exit condition
left_IR_V = analogRead(A1); // left IR Vout
right_IR_V = analogRead(A0); // right IR Vout
left_IR_dist = Vout_to_dist(left_IR_V);
right_IR_dist = Vout_to_dist(right_IR_V);
}
motor1.stop();
motor2.stop();
motor1.run(-normal_speed);
motor2.run(normal_speed);
}
//bot can continue moving
}
void TurnLeft() {
// wheels of Bot move different direction to move left
motor1.run(normal_speed);
motor2.run(normal_speed);
delay(270);
// Bot stops moving
motor1.stop();
motor2.stop();
// move forward while adjusting using proximity sensor
adjust();
}
void TurnRight() {
// wheels of Bot move different direction to move right
motor1.run(-normal_speed);
motor2.run(-normal_speed);
delay(280);
// Bot stops moving
motor1.stop();
motor2.stop();
// move forward while adjusting using proximity sensor
adjust();
}
// u turn clockwise
void reverse1() {
motor1.run(-normal_speed);
motor2.run(-normal_speed);
delay(540);
motor1.stop();
motor2.stop();
adjust();
}
// u turn anti-clockwise
void reverse2() {
motor1.run(normal_speed);
motor2.run(normal_speed);
delay(450);
motor1.stop();
motor2.stop();
adjust();
}
void TurnLeftLeft() {
// Turn left, move forward 1 grid, turn left again
TurnLeft();
delay(550);
TurnLeft();
}
void TurnRightRight() {
// Turn right, move forward 1 grid, turn right again
TurnRight();
delay(600);
TurnRight();
}
void TurnEnd() {
// Stop moving and play victory tune. ENd of maze
motor1.stop();
motor2.stop();
tune();
delay(10000);
}
void turn() {
// Check for colour
char x = whatColour();
Serial.println(x);
if ( x == 'R') {
// colour detected is Red
TurnLeft();
}
else if ( x == 'G') {
// colour detected is Green
TurnRight();
}
else if ( x == 'Y') {
// colour detected is Yellow
left_IR_V = analogRead(A1); // left IR Vout
right_IR_V = analogRead(A0); // right IR Vout
left_IR_dist = Vout_to_dist(left_IR_V);
right_IR_dist = Vout_to_dist(right_IR_V);
if (right_IR_dist < left_IR_dist) {
// bot closer to right side of path
reverse2();
}
else {
// bot closer to left side of path
reverse1();
}
}
else if ( x == 'P') {
// colour detected is Purple
TurnLeftLeft();
}
else if ( x == 'B') {
// colour detected is Blue
TurnRightRight();
}
else if ( x == 'E') {
// colour detected is Black
TurnEnd();
}
}
int is_black_line() {
// Check for black strip
int sensorState = lineFinder.readSensors();
if (sensorState == S1_IN_S2_IN || sensorState == S1_IN_S2_OUT || sensorState == S1_OUT_S2_IN) {
return 1;
}
else {
return 0;
}
}
void Stop_or_Move() {
distance = ultraSensor.distanceCm();
if (distance <= distanceThreshold) {
// Wall infront. Stop moving and do colour challenge
motor1.stop();
motor2.stop();
turn();
}
if (is_black_line() == 1) {
// Black strip detected. Stop moving and do colour challenge
motor1.stop();
motor2.stop();
turn();
}
else {
// Move forward while adjusting using proximity sensor
adjust();
}
}
// notes in the melody:
int melody[] = {
NOTE_C4, NOTE_G3, NOTE_G3, NOTE_A3, NOTE_G3, 0, NOTE_B3, NOTE_C4
};
// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] = {
4, 8, 8, 4, 4, 4, 4, 4
};
void tune() {
buzzer.tone(800, 1000);
// iterate over the notes of the melody:
for (int thisNote = 0; thisNote < 8; thisNote++) {
// to calculate the note duration, take one second divided by the note type.
//e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
int noteDuration = 1000 / noteDurations[thisNote];
buzzer.tone(8, melody[thisNote], noteDuration);
// to distinguish the notes, set a minimum time between them.
// the note's duration + 30% seems to work well:
int pauseBetweenNotes = noteDuration * 1.30;
delay(pauseBetweenNotes);
// stop the tone playing:
buzzer.noTone(8);
}
}
void setup() {
Serial.begin(9600);
move_forward();
}
void loop() {
// constantly check for distance of an obstacle in front of mBot
distance = ultraSensor.distanceCm();
Stop_or_Move();
}