-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdoddi.ino
More file actions
384 lines (358 loc) · 11 KB
/
doddi.ino
File metadata and controls
384 lines (358 loc) · 11 KB
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
#include "I2Cdev.h"
#include "MPU6050_6Axis_MotionApps20.h"
//#include <TeensyThreads.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_LSM303_U.h>
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h>
#endif
#include <Audio.h>
#include <SPI.h>
#include <SD.h>
/*
NOTE Finite State Machine structure
off-selection-minigame-off
*/
//#define ACCEL
//#define ACCEL_DEBUG
#define vib 16
#define INTERRUPT_PIN 5
#define PIN 23 // Pin on the Arduino connected to the NeoPixels
#define NUMPIXELS 12 //Number of NeoPixels attached to the Arduino
#define SHAKE_THRESH 1500
void setAllPixels(uint32_t color);
uint8_t splitColor ( uint32_t c, char value );
MPU6050 mpu;
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
AudioPlaySdWav playSdWav1;
AudioOutputAnalog dac1;
AudioConnection patchCord1(playSdWav1, 0, dac1, 0);
//faces coordinates
const int facesYZ[12][2] = {
{ 0, 0}, // 0
{ 50, 30}, // 1
{ 40, -30}, // 2
{-15, -60}, // 3
{-69, -5}, // 4
{-15, 60}, // 5
{ 60, -3},// 6
{ 15, -50},// 7
{-40, -30}, // 8
{-38, 30}, // 9
{ 20, 50}, // 10
{ 0 , 0}, // 11
};
typedef struct color{
int r;
int g;
int b;
} color;
typedef struct face{
boolean resPresent;
int resID;
uint32_t color;
boolean isActive;
} face;
const uint32_t yellow = pixels.Color(150, 150, 0);
const uint32_t red = pixels.Color(200, 0, 0);
const uint32_t green = pixels.Color(0, 200, 0);
const uint32_t blue = pixels.Color(0, 0, 200);
const uint32_t purple = pixels.Color(150, 0, 150);
const uint32_t off = pixels.Color(0, 0, 0);
// MPU control/status vars
bool dmpReady = false; // set true if DMP init was successful
uint8_t mpuIntStatus; // holds actual interrupt status byte from MPU
uint8_t devStatus; // return status after each device operation (0 = success, !0 = error)
uint16_t packetSize; // expected DMP packet size (default is 42 bytes)
uint16_t fifoCount; // count of all bytes currently in FIFO
uint8_t fifoBuffer[128]; // FIFO storage buffer
float final_euler[3];
float final_ypr[3];
VectorInt16 aaWorld;
volatile bool mpuInterrupt = false;
enum states {OFF=0, SELECTION=1, MINIGAME=2, STOP=3}; //States of the FSM
int state = SELECTION;
// game global vars
boolean start = false;
boolean done = false;
boolean gamerunning = false;
boolean endgame = false;
boolean faceChange = false;
uint8_t activeF = 0;
boolean shaken = false;
long lastshake = 0;
face F[12] = {0};
void arrayInit() {
// Initialize faces array
memset(&F, 0, 12 * sizeof(face));
randomSeed(analogRead(A7));
// Random assignment of resources to faces.
for(int i = 0; i < 12; i++) {
F[i].resID = random(4);
}
// Set resource color to faces based on resID
for (int i = 0; i < 12; i++) {
switch(F[i].resID)
{
case 0:
F[i].color = blue;
break;
case 1:
F[i].color = yellow;
break;
case 2:
F[i].color = green;
break;
case 3:
F[i].color = red;
break;
}
}
}
void maingame() {
int oldr, oldg, oldb;
uint32_t oldcolor;
boolean facesLeft = true;
switch(state)
{
case OFF:
if(shaken) {
state = SELECTION; //State change condition
shaken = false; //Reset shaken to avoid skipping through states
done = false;
}
setAllPixels(off);
Serial.print("Waiting for start... ");
break;
case SELECTION:
if(shaken) {
state = MINIGAME; //State change condition
shaken = false; //Reset shaken to avoid skipping through states
setAllPixels(off);
Serial.println("START TO PLAY!! SOUND FEEDBACK-->intro story");
Serial.println("Start playing");
playSdWav1.play("start1.wav");
delay(50); // wait for library to parse WAV info
}
else{
while(!done) {
setAllPixels(purple);
delay(500); //Delay to make game slower
done=true;
}
// Turn on all faces with colors
// Set faces color to corresponding resources
for (int i = 0; i < NUMPIXELS; i++) {
if (F[i].isActive == 0) {
pixels.setPixelColor(i, F[i].color);
pixels.show();
delay(50);
}
}
oldcolor = pixels.getPixelColor(activeF);
oldr = splitColor(oldcolor, 'r');
oldg = splitColor(oldcolor, 'g');
oldb = splitColor(oldcolor, 'b');
pixels.setPixelColor(activeF, pixels.Color((oldr + 55), (oldg + 55), (oldb + 55)));
pixels.show();
}
break;
case MINIGAME:
/* Highlight all the faces by placing them up*/
//Highlight the upward face
if (F[activeF].resPresent == false) {
pixels.setPixelColor(activeF, purple);
pixels.show();
F[activeF].resPresent = 1;
}
facesLeft = false;
//facesLeft: there are faces still not active
for (int i = 0; i < 12; i++) {
if(!F[i].resPresent) facesLeft = true;
}
if (facesLeft == false) state = STOP;
break;
case STOP:
//zeroing faces
for (int i = 0; i < 12; i++) {
F[i].resPresent = 0;
}
//minigame sound play
Serial.println("End playing");
playSdWav1.play("win.wav");
delay(50); // wait for library to parse WAV info
state = SELECTION;
break;
}
}
void setAllPixels(uint32_t color) {
for (int i = 0; i < NUMPIXELS; i++) {
pixels.setPixelColor(i, color);
pixels.show();
delay(50);
}
}
uint8_t splitColor ( uint32_t c, char value ) {
switch ( value ) {
case 'r': return (uint8_t)(c >> 16);
case 'g': return (uint8_t)(c >> 8);
case 'b': return (uint8_t)(c >> 0);
default: return 0;
}
}
void dmpDataReady() {
mpuInterrupt = true;
}
void setup() {
/* Hardware initialization */
pinMode(vib, OUTPUT);
pinMode(INTERRUPT_PIN, INPUT);
pixels.begin();
pixels.setBrightness(64);
pixels.show(); // turn off all the pixels
Serial.begin(9600);
SPI.begin();
while(!(SD.begin(10))) {
Serial.println("Unable to access the SD card");
delay(500);
}
Wire.begin();
Wire.setClock(400000);
AudioMemory(100);
accelinit();
/* Data structure initialization */
arrayInit();
}
void loop() {
// The main loop is dedicated to the accelerometer
VectorInt16 aa; //accel sensor measurements
VectorInt16 aaReal; // [x, y, z] gravity-free accel sensor measurements
VectorFloat gravity; //gravity vector
Quaternion q;
float euler[3];
float ypr[3];
if (!dmpReady) return;
while (!mpuInterrupt){ //&& fifoCount < packetSize) { // This condition is causing hang
// The game code is executed while the accelerometer is not working
maingame();
}
mpuInterrupt = false;
mpuIntStatus = mpu.getIntStatus();
//Serial.print("MPU status: ");
//Serial.println(mpuIntStatus);
fifoCount = mpu.getFIFOCount();
// check for overflow (this should never happen unless our code is too inefficient)
if ((mpuIntStatus & 0x10) || fifoCount == 1024) {
// reset so we can continue cleanly
mpu.resetFIFO();
Serial.println(F("FIFO overflow!"));
// otherwise, check for DMP data ready interrupt
} else if (mpuIntStatus & 0x02) {
while (fifoCount < packetSize) fifoCount = mpu.getFIFOCount();
mpu.getFIFOBytes(fifoBuffer, packetSize);
fifoCount -= packetSize;
/* Orientation reading */
mpu.dmpGetQuaternion(&q, fifoBuffer);
mpu.dmpGetEuler(euler, &q);
mpu.dmpGetGravity(&gravity, &q);
mpu.dmpGetYawPitchRoll(ypr, &q, &gravity);
/* Acceleration reading */
mpu.dmpGetAccel(&aa, fifoBuffer);
mpu.dmpGetGravity(&gravity, &q);
mpu.dmpGetLinearAccel(&aaReal, &aa, &gravity);
mpu.dmpGetLinearAccelInWorld(&aaWorld, &aaReal, &q);
for(int i=0; i<3; i++)
{
final_euler[i] = euler[i] * 180/M_PI;
final_ypr[i] = ypr[i] * 180/M_PI;
}
#ifdef ACCEL_DEBUG
Serial.print("ypr\t");
Serial.print(ypr[0] * 180/M_PI);
Serial.print("\t");
Serial.print(ypr[1] * 180/M_PI);
Serial.print("\t");
Serial.println(ypr[2] * 180/M_PI);
Serial.print("euler z ");
Serial.println(euler[2] * 180/M_PI);
Serial.print("aworld\t");
Serial.print(aaWorld.x);
Serial.print("\t");
Serial.print(aaWorld.y);
Serial.print("\t");
Serial.println(aaWorld.z);
#endif
int newF = getFace();
if(newF != activeF) {
activeF = newF;
Serial.print("Active face: ");
Serial.println(activeF);
digitalWrite(vib, HIGH);
delay(30);
digitalWrite(vib, LOW);
}
if(millis() - lastshake > 1000) {
shaken = getShake();
lastshake = millis();
if(shaken) printf("Shake: true");
}
}
}
void accelinit() {
mpu.initialize();
Serial.println(mpu.testConnection() ? F("MPU6050 connection successful") : F("MPU6050 connection failed"));
devStatus = mpu.dmpInitialize();
// supply your own gyro offsets here, scaled for min sensitivity
mpu.setXGyroOffset(220);
mpu.setYGyroOffset(76);
mpu.setZGyroOffset(-85);
mpu.setZAccelOffset(1788); // 1688 factory default for my test chip
// make sure it worked (returns 0 if so)
if (devStatus == 0) {
mpu.setDMPEnabled(true);
attachInterrupt(digitalPinToInterrupt(INTERRUPT_PIN), dmpDataReady, RISING);
mpuIntStatus = mpu.getIntStatus();
dmpReady = true;
packetSize = mpu.dmpGetFIFOPacketSize();
} else {
// ERROR!
// 1 = initial memory load failed
// 2 = DMP configuration updates failed
// (if it's going to break, usually the code will be 1)
Serial.print(F("DMP Initialization failed (code "));
Serial.print(devStatus);
Serial.println(F(")"));
}
}
int getFace() {
int d, iMin = 0, dMin = 999999;
float fY, fZ, dY, dZ;
for (int i=0; i < 12; i++) { // For each face...
fY = (float)facesYZ[i][0]; // Read y and z coordinates
fZ = (float)facesYZ[i][1];
dY = final_ypr[1] - fY; // Delta between accelerometer & face
dZ = final_ypr[2] - fZ; // Delta between accelerometer & face
d = dY * dY + dZ * dZ; // Distance^2
// Check if this face is the closest match so far. Because
// we're comparing RELATIVE distances, sqrt() can be avoided.
if (d < dMin) { // New closest match?
dMin = d; // Save closest distance^2
iMin = i; // Save index of closest match
}
}
if((iMin == 0) || (iMin == 11)) { //Top and bottom face disambiguation
int eulerZ = (final_euler[2] > 0) ? final_euler[2] : -final_euler[2];
iMin = (eulerZ > 160) ? 0 : 11;
}
return iMin; // Index of closest matching face*/
}
boolean getShake() {
int accelx = (aaWorld.x > 0) ? aaWorld.x : -aaWorld.x;
int accely = (aaWorld.y > 0) ? aaWorld.y : -aaWorld.y;
int accelz = (aaWorld.z > 0) ? aaWorld.z : -aaWorld.z;
if(accelx > SHAKE_THRESH || accely > SHAKE_THRESH || accelz > SHAKE_THRESH)
return true;
else return false;
}