-
Notifications
You must be signed in to change notification settings - Fork 0
/
BorderDetect.ino
183 lines (165 loc) · 4.64 KB
/
BorderDetect.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
/** This example uses the Zumo's line sensors to detect the white
border around a sumo ring. When the border is detected, it
backs up and turns. */
#include <Wire.h>
#include <Zumo32U4.h>
// This might need to be tuned for different lighting conditions,
// surfaces, etc.
#define QTR_THRESHOLD 1000 // microseconds
// These might need to be tuned for different motor types.
#define REVERSE_SPEED 200 // 0 is stopped, 400 is full speed
#define TURN_SPEED 200
#define FORWARD_SPEED 200
#define REVERSE_DURATION 200 // ms
#define TURN_DURATION 300 // ms
Zumo32U4LCD lcd;
Zumo32U4ButtonA buttonA;
Zumo32U4ButtonB buttonB;
Zumo32U4Buzzer buzzer;
Zumo32U4Motors motors;
Zumo32U4LineSensors lineSensors;
Zumo32U4ProximitySensors botSensors;
#define NUM_SENSORS 3
unsigned int lineSensorValues[NUM_SENSORS];
int randVar = 0;
int songLoopNum = 0;
int toneTime = 0;
void loopSong(int songLoopNum, int toneTime)
{
for(int i = 0; i < songLoopNum; i++)
{
for(int i = 0; i < 3; i++)
{
buzzer.playNote(NOTE_A(4), 2*toneTime, 15);
delay(toneTime);
buzzer.playNote(NOTE_E(4), toneTime, 15);
delay(toneTime);
buzzer.playNote(NOTE_G(4), toneTime, 15);
delay(toneTime);
buzzer.playNote(NOTE_A(4), toneTime, 15);
delay(toneTime);
buzzer.playNote(NOTE_C(5), toneTime, 15);
delay(toneTime);
buzzer.playNote(NOTE_B(4), toneTime, 15);
delay(toneTime);
buzzer.playNote(NOTE_G(4), toneTime, 15);
delay(toneTime);
buzzer.playNote(NOTE_A(4), toneTime, 15);
delay(toneTime);
}
buzzer.playNote(NOTE_D(5), 2*toneTime, 15);
delay(2*toneTime);
buzzer.playNote(NOTE_C(5), toneTime, 15);
delay(toneTime);
buzzer.playNote(NOTE_B(4), 2*toneTime, 15);
delay(2*toneTime);
buzzer.playNote(NOTE_C(5), toneTime, 15);
delay(toneTime);
buzzer.playNote(NOTE_B(4), toneTime, 15);
delay(toneTime);
buzzer.playNote(NOTE_G(4), toneTime, 15);
delay(toneTime);
}
}
void waitForButtonAndCountDown()
{
ledYellow(1);
lcd.clear();
lcd.print(F("Press A"));
buttonA.waitForButton();
ledYellow(0);
lcd.clear();
// Play audible countdown.
for (int i = 0; i < 3; i++)
{
delay(1000);
buzzer.playNote(NOTE_G(3), 200, 15);
}
delay(1000);
}
void setup()
{
// Uncomment if necessary to correct motor directions:
//motors.flipLeftMotor(true);
//motors.flipRightMotor(true);
lineSensors.initThreeSensors();
botSensors.initThreeSensors();
waitForButtonAndCountDown();
}
void loop()
{
if (buttonA.isPressed())
{
// If button is pressed, stop and wait for another press to
// go again.
motors.setSpeeds(0, 0);
buttonA.waitForRelease();
waitForButtonAndCountDown();
}
lineSensors.read(lineSensorValues);
botSensors.read();
if (lineSensorValues[0] < QTR_THRESHOLD)
{
// If leftmost sensor detects line, reverse and turn to the
// right.
motors.setSpeeds(-REVERSE_SPEED, -REVERSE_SPEED);
delay(REVERSE_DURATION);
motors.setSpeeds(TURN_SPEED, -TURN_SPEED);
delay(TURN_DURATION);
motors.setSpeeds(FORWARD_SPEED, FORWARD_SPEED);
}
else if (lineSensorValues[NUM_SENSORS - 1] < QTR_THRESHOLD)
{
// If rightmost sensor detects line, reverse and turn to the
// left.
motors.setSpeeds(-REVERSE_SPEED, -REVERSE_SPEED);
delay(REVERSE_DURATION);
motors.setSpeeds(-TURN_SPEED, TURN_SPEED);
delay(TURN_DURATION);
motors.setSpeeds(FORWARD_SPEED, FORWARD_SPEED);
}
/*else
{
// Otherwise, go straight.
motors.setSpeeds(FORWARD_SPEED, FORWARD_SPEED);
}*/
else if (botSensors.countsFrontWithLeftLeds() >= 2 || botSensors.countsFrontWithRightLeds() >= 2)
{
randVar = random(0,1);
if(randVar == 0)
{
motors.setSpeeds(-2*FORWARD_SPEED, 0.5*FORWARD_SPEED);
delay(100);
}
else if(randVar == 1)
{
motors.setSpeeds(-2*FORWARD_SPEED, 0.5*FORWARD_SPEED);
delay(100);
}
}
else if (botSensors.countsLeftWithLeftLeds() >= 2)
{
motors.setSpeeds(2*FORWARD_SPEED, FORWARD_SPEED);
delay(100);
}
else if (botSensors.countsRightWithRightLeds() >= 2)
{
motors.setSpeeds(FORWARD_SPEED, 2*FORWARD_SPEED);
delay(100);
}
else
{
randVar = random(0,1);
if (randVar == 0)
{
motors.setSpeeds(FORWARD_SPEED, -FORWARD_SPEED);
}
else if (randVar == 1)
{
motors.setSpeeds(-FORWARD_SPEED, FORWARD_SPEED);
}
songLoopNum = 1;
toneTime = 10; //insert 200 to play song properly
loopSong(songLoopNum, toneTime);
}
}