-
Notifications
You must be signed in to change notification settings - Fork 0
/
voiceActivatedLaserToy.ino
266 lines (219 loc) · 6.9 KB
/
voiceActivatedLaserToy.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
#include <ServoEasing.h>
#include "config.h"
#include "dataStructures.h"
// create the two servos; pitch is the top servo, yaw is the bottom
ServoEasing pitchServo;
ServoEasing yawServo;
// track the state of the yaw and pitch
int currentPitch = 90;
int currentYaw = 90;
// the minimum/maxiumum pitch and yaw; calibrate these to the size of the room
int minPitch = 115;
int maxPitch = 140;
int minYaw = 67;
int maxYaw = 100;
// track the time for heart beat
int currentTime = millis();
// create the feeds from the Adafruit IO
AdafruitIO_Feed *catLaserYawFeed = io.feed("catLaserYaw");
AdafruitIO_Feed *catLaserPitchFeed = io.feed("catLaserPitch");
AdafruitIO_Feed *catLaserInitPlayFeed = io.feed("catLaserInitPlay");
AdafruitIO_Feed *catLaserPollFeed = io.feed("catLaserPoll");
AdafruitIO_Feed *catLaserToggleFeed = io.feed("catLaserToggle");
void setup()
{
// home servos and setup laser
homeLaser();
pinMode(14, OUTPUT); // laser pin = 14
// setup serial connection
Serial.begin(115200);
// wait for serial monitor
//while( ! Serial);
// connect to Adafruit IO Service
Serial.print("Connecting to Adafruit IO");
io.connect();
// handle messages from the Adafruit IO feeds
catLaserPitchFeed->onMessage(handlePitchMessage);
catLaserYawFeed->onMessage(handleYawMessage);
catLaserToggleFeed->onMessage(handleLaserMessage);
catLaserInitPlayFeed->onMessage(handlePlayMessage);
// wait for Adafruit IO connection
while(io.status() < AIO_CONNECTED)
{
// print dots to terminal and blink laser
Serial.print(".");
delay(250);
toggleLaser(HIGH);
delay(250);
toggleLaser(LOW);
}
// once connected to Adafruit IO
Serial.println();
Serial.println(io.statusText());
catLaserPitchFeed->get();
catLaserYawFeed->get();
catLaserToggleFeed->get();
// reset all status indicators on Adafruit IO
catLaserToggleFeed->save("OFF");
catLaserPitchFeed->save(currentPitch);
catLaserYawFeed->save(currentYaw);
}
void loop()
{
// stay connected to Adafruit IO and process incoming commands
io.run();
// let Adafruit know we are alive
sendHeartBeatToAdafruit();
}
// turn the servos on or off; send a LOW for off or HIGH for on
void toggleServos(int servosState)
{
if (servosState == HIGH)
{
pitchServo.attach(13); // top servo pin = 13
yawServo.attach(12); // bottom servo pin = 12
} else if (servosState == LOW)
{
pitchServo.detach();
yawServo.detach();
}
}
// turn the laser on and off; send a LOW for off or HIGH for on
void toggleLaser(int laserState)
{
digitalWrite(14, laserState); // laser pin = 14
// update Adafruit IO
String stateName;
if (laserState == HIGH)
{
//catLaserToggleFeed->save("ON");
}
else
{
//catLaserToggleFeed->save("OFF");
}
}
// move to laser to home position
void homeLaser(){
currentPitch = 20;
currentYaw = 110;
setSpeedForAllServos(30);
toggleServos(HIGH);
pitchServo.setEaseTo(currentPitch);
yawServo.setEaseTo(currentYaw);
synchronizeAllServosStartAndWaitForAllServosToStop();
// make it "pop" back into place
yawServo.write(90);
// update the Adafruit IO values
//catLaserPitchFeed->save(currentPitch);
//catLaserYawFeed->save(currentYaw);
delay(2000); // give enough time to get there
toggleServos(LOW);
}
// handle the Adafruit IO messages for the pitch servo
void handlePitchMessage(AdafruitIO_Data *data)
{
int angle = data->toInt();
toggleServos(HIGH);
pitchServo.write(angle);
delay(2000);
toggleServos(LOW);
}
// handle the Adafruit IO messages for the pitch servo
void handleYawMessage(AdafruitIO_Data *data)
{
int angle = data->toInt();
toggleServos(HIGH);
yawServo.write(angle);
delay(2000);
toggleServos(LOW);
}
// handle the Adafruit IO messages for turning the laser on and off
void handleLaserMessage(AdafruitIO_Data *data)
{
String state = data->toString();
if (state == "ON")
{
toggleLaser(HIGH);
} else if (state == "OFF")
{
toggleLaser(LOW);
}
}
// handle the Adafruit IO messages for initiation of play session
void handlePlayMessage(AdafruitIO_Data *data)
{
String toggleValue = data->toString();
if(toggleValue == "PLAYING")
{
// start play session
makeRandomCommandSet();
executePlayCommandSet(randomCommandSet, 50);
//executePlayCommandSet(laserWallJump, countElements(laserWallJump));
makeRandomCommandSet();
executePlayCommandSet(randomCommandSet, 50);
catLaserInitPlayFeed->save("OFF");
homeLaser();
}
}
// sends a '1' to Adafruit IO every minute to let it know we are alive
void sendHeartBeatToAdafruit()
{
int timeDifferential = millis() - currentTime;
if (timeDifferential > 60000)
{
catLaserPollFeed->save(1);
currentTime = millis();
}
}
// execute a play command setup
void executePlayCommandSet(PlayCommandSet commandSet[], int elementsInCommandSet)
{
// attach servos
toggleServos(HIGH);
setSpeedForAllServos(140);
for (int i = 0; i < elementsInCommandSet; i++)
{
toggleLaser(commandSet[i].laserToggle);
// smoothly move laser to next location
int diffBetweenPitch = commandSet[i].pitch - commandSet[i-1].pitch;
pitchServo.setEaseTo(commandSet[i].pitch);
yawServo.setEaseTo(commandSet[i].yaw);
synchronizeAllServosStartAndWaitForAllServosToStop();
delay(commandSet[i].delayTime);
}
// detach servos and shutoff laser
toggleServos(LOW);
toggleLaser(LOW);
}
// make a laser floor trace game
void makeRandomCommandSet()
{
//int numberOfElements = countElements(randomCommandSet);
int numberOfElements = 50;
for (int i = 0; i < numberOfElements; i++)
{
// consitent delay between movements
randomCommandSet[i].delayTime = random(300,500);
// rarely turn off the laser
int randomNumber = random(50);
if (randomNumber == random(50))
{
randomCommandSet[i].laserToggle = LOW;
}
else
{
randomCommandSet[i].laserToggle = HIGH;
}
// keep pitch between 120 and 180
randomCommandSet[i].pitch = random(minPitch, maxPitch);
// keep yaw between 30 and 100
randomCommandSet[i].yaw = random(minYaw, maxYaw);
}
}
// count the number of elements in the command set
int countElements(PlayCommandSet commandSet[])
{
int elementsInCommandSet = sizeof(commandSet) / sizeof(commandSet[0]);
return elementsInCommandSet;
}