-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSumo
88 lines (67 loc) · 1.88 KB
/
Sumo
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
#include <ZumoReflectanceSensorArray.h>
#include <Pushbutton.h>
#include <QTRSensors.h>
#include <ZumoBuzzer.h>
#include <ZumoMotors.h>
#include <NewPing.h>
#define TRIGGER_PIN 12 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN 13 // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
#define QTR_THRESHOLD 250
Pushbutton button(ZUMO_BUTTON); // pushbutton on pin 12
ZumoBuzzer buzzer;
ZumoMotors motors;
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
#define NUM_SENSORS 6
unsigned int sensor_values[NUM_SENSORS];
ZumoReflectanceSensorArray sensors(QTR_NO_EMITTER_PIN);
void waitForButtonAndCountDown()
{
button.waitForButton();
// play audible countdown
for (int i = 0; i < 3; i++)
{
delay(1000);
buzzer.playNote(NOTE_G(3), 200, 15);
}
delay(1000);
buzzer.playNote(NOTE_G(4), 500, 15);
delay(1000);
}
void setup()
{
sensors.calibrate();
waitForButtonAndCountDown();
}
void loop()
{
unsigned int uS = sonar.ping();
if (uS / US_ROUNDTRIP_CM < 1){
}
else{
if (uS / US_ROUNDTRIP_CM < 50) {
sensors.read(sensor_values);
if (sensor_values[0] < QTR_THRESHOLD)
{
// if leftmost sensor detects line, reverse and turn to the right
motors.setSpeeds(-200, -200);
delay(200);
motors.setSpeeds(200, -200);
delay(200);
motors.setSpeeds(200, 200);
}
else if (sensor_values[5] < QTR_THRESHOLD)
{
// if rightmost sensor detects line, reverse and turn to the left
motors.setSpeeds(-200, -200);
delay(200);
motors.setSpeeds(-200, 200);
delay(200);
motors.setSpeeds(200, 200);
}
motors.setSpeeds(250,250);
}
else {
motors.setSpeeds(200,-200);
}
}}