forked from aesia/gertrudepoubellevivante
-
Notifications
You must be signed in to change notification settings - Fork 0
/
makeymakeytest.ino
298 lines (257 loc) · 7.52 KB
/
makeymakeytest.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
/**
*
* The MIT License (MIT)
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
* and associated documentation files (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
#include "Timer.h"
#include <math.h>
#include <Adafruit_NeoPixel.h>
#define MAKEYMAKEY_DIGITAL_PIN 5
#define LOUDNESS_THRESHOLD 18
#define SECONDS_BEFORE_START 3
#define ONE_SECOND 1000
#define MAXUSEDPIXELS 30
#define SAMPLE_WINDOW 50
// Our LEDS
Adafruit_NeoPixel strip = Adafruit_NeoPixel(30, 6, NEO_GRB + NEO_KHZ800);
Adafruit_NeoPixel rightEye = Adafruit_NeoPixel(12, 5, NEO_GRB + NEO_KHZ800);
Adafruit_NeoPixel leftEye = Adafruit_NeoPixel(12, 4, NEO_GRB + NEO_KHZ800);
unsigned int sample; // loudness to volts
double maxSignalRecorded = 0.0; // loudness to volts
double volts; // loudness to volts
int score = 0; // game score
int currentLedIndex = 0; // led index
Timer t;
int eyesCounter = SECONDS_BEFORE_START;
boolean isStart = true;
int gameStartTimer;
int scoreCheckTimer;
// Colors
int purple[] = { 107,35,148 };
int blue[] = { 35,35,220 };
int green[] = { 87,157,28 };
int yellow[] = { 255,255,0 };
int orange[] = { 255,149,14 };
int red[] = { 255,0,0 };
int currentColor[3] = { 255,255,255 };
int eyeColor[] = { 87,157,28 };
// thresholds
#define PURPLE_LEVEL 55
#define BLUE_LEVEL 34
#define GREEN_LEVEL 21
#define YELLOW_LEVEL 13
#define ORANGE_LEVEL 8
#define RED_LEVEL 5
/*-----------------------------------------
ACTUAL PROGRAM
-----------------------------------------*/
void setup()
{
leftEye.begin();
rightEye.begin();
strip.begin();
clearStrip();
Serial.begin(9600);
}
/**
* Our main loop which handles of the logic:
* 1 - we check that we get a contact from the makey makey (players holding hands + holding copper areas)
* 2 - if that's the case we get the loudness level and change color of the LED strip according to the score
*/
void loop() {
// update our timer to check score
t.update();
// check makey makey
if (!contactDone()) {
// no signal incoming from makey makey = we shutdown the leds and reset the game
resetScore();
clearStrip();
isStart = true;
eyesCounter = SECONDS_BEFORE_START * 2;
t.stop(scoreCheckTimer);
closeEyes();
return;
}
// Lets make our yes blink before starting
if (isStart) {
isStart = false;
gameStartTimer = t.every(ONE_SECOND / 2, blinkEyes, SECONDS_BEFORE_START * 2);
return;
}
// check our start game timer
if (eyesCounter > 0) return;
// collect data for 50 ms on our Loudness sensor
// convert it to volts
// determine LED index and illuminate LED strip
unsigned long startMillis = millis(); // Start of sample window
unsigned int peakToPeak = 0; // peak-to-peak level
unsigned int signalMax = 0;
unsigned int signalMin = 1024;
while (millis() - startMillis < SAMPLE_WINDOW) {
sample = analogRead(0);
if (sample < 1024) {
if (sample > signalMax) {
signalMax = sample; // save just the max levels
}
else if (sample < signalMin) {
signalMin = sample; // save just the min levels
}
}
}
peakToPeak = signalMax - signalMin; // max - min = peak-peak amplitude
volts = (peakToPeak * 3.3) / 1024; // convert to volts
if (volts > maxSignalRecorded) maxSignalRecorded = volts;
double max = 2.7;
double oneLed = max / MAXUSEDPIXELS;
currentLedIndex = ceil(volts / oneLed);
clearStrip();
if (currentLedIndex > 2) {
colorWipe(strip.Color(currentColor[0], currentColor[1], currentColor[2]), currentLedIndex);
}
/*
String output = "";
output.concat(volts);
output.concat(" / ");
output.concat(currentLedIndex);
Serial.println(output);
*/
}
/*-----------------------------------------
HELPERS
-----------------------------------------*/
/**
* Clear our LED Strip
*/
void clearStrip() {
colorWipe(strip.Color(0, 0, 0), strip.numPixels());
}
/**
* Change our LED Strip's colors
*/
void colorWipe(uint32_t c, int index) {
for (uint16_t i = 0; i< MAXUSEDPIXELS; i++) {
if (i <index)
strip.setPixelColor(i, c);
else
strip.setPixelColor(i, strip.Color(0, 0, 0));
strip.show();
}
}
/**
* Check for signal incoming from Makey Makey
*/
boolean contactDone() {
boolean makeyMakeySentSignal = analogRead(MAKEYMAKEY_DIGITAL_PIN) > 500;
return makeyMakeySentSignal;
}
/**
* RIGHT EYE
*/
void rightEyeDisplay(int r, int g, int b ) {
for (uint16_t i = 0; i<rightEye.numPixels() + 2; i++) {
rightEye.setPixelColor(i, rightEye.Color(r, g, b)); // Draw new pixel
rightEye.show();
}
}
/**
* LEFT EYE
*/
void leftEyeDisplay(int r, int g, int b) {
for (uint16_t i = 0; i < leftEye.numPixels() + 2; i++) {
leftEye.setPixelColor(i, leftEye.Color(r, g, b)); // Draw new pixel
leftEye.show();
}
}
void closeEyes() {
rightEyeDisplay(0, 0, 0);
leftEyeDisplay(0, 0, 0);
}
void startGame() {
scoreCheckTimer = t.every(ONE_SECOND, checkLevel);
}
/**
* let's open or close our eyes before actual game start
*/
void blinkEyes() {
eyesCounter--;
if (eyesCounter <= 0) {
t.stop(gameStartTimer);
closeEyes();
startGame();
}
else {
if (eyesCounter % 2 == 0) {
closeEyes();
}
else {
// show eyes
rightEyeDisplay(eyeColor[0], eyeColor[1], eyeColor[2]);
leftEyeDisplay(eyeColor[0], eyeColor[1], eyeColor[2]);
}
}
}
/**
* Every second, the loudness is checked.
* change colors accordinly
*/
void checkLevel() {
Serial.println(currentLedIndex);
if (currentLedIndex > LOUDNESS_THRESHOLD) {
score++;
if (score >= PURPLE_LEVEL) {
Serial.println("PURPLE!");
currentColor[0] = purple[0];
currentColor[1] = purple[1];
currentColor[2] = purple[2];
}
else if (score >= BLUE_LEVEL) {
Serial.println("BLUE!");
currentColor[0] = blue[0];
currentColor[1] = blue[1];
currentColor[2] = blue[2];
}
else if (score >= GREEN_LEVEL) {
Serial.println("GREEN!");
currentColor[0] = green[0];
currentColor[1] = green[1];
currentColor[2] = green[2];
}
else if (score >= YELLOW_LEVEL) {
Serial.println("YELLOW!");
currentColor[0] = yellow[0];
currentColor[1] = yellow[1];
currentColor[2] = yellow[2];
}
else if (score >= ORANGE_LEVEL) {
Serial.println("ORANGE!");
currentColor[0] = orange[0];
currentColor[1] = orange[1];
currentColor[2] = orange[2];
}
else if (score >= RED_LEVEL) {
Serial.println("RED!");
currentColor[0] = red[0];
currentColor[1] = red[1];
currentColor[2] = red[2];
}
}
else {
resetScore();
}
}
void resetScore() {
currentColor[0] = 255;
currentColor[1] = 255;
currentColor[2] = 255;
score = 0;
}