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
| /*************************************************
* Pin / Library Setup
*************************************************/
#include <SoftwareSerial.h>
#include "Adafruit_Soundboard.h"
#include<Wire.h>
// Accelerometer / Gyro
const int MPU_addr=0x68;
int16_t AcX,AcY,AcZ,Tmp,GyX,GyY,GyZ;
// WAV TRIGGER
#define SFX_TX 11
#define SFX_RX 12
#define SFX_RST 10
SoftwareSerial ss = SoftwareSerial(SFX_TX, SFX_RX);
Adafruit_Soundboard sfx = Adafruit_Soundboard(&ss, NULL, SFX_RST);
// RGB LED
const int blueLED = 6;
const int greenLED = 5;
const int redLED = 3;
// MODE BUTTONS
const int bgModeButton = 8;
const int fxModeButton = 9;
// BUTTONS
const int b1 = A0;
const int b2 = A1;
const int b3 = A2;
const int b4 = A3;
// LOGIC VARIABLES
int stateOfBoard = 0;
int loopingTrack = 0;
int fairySongs[] = {6,4,2,9,3};
boolean fairyIsAwake = false;
boolean isLooping = false;
char oldMembraneKey;
/*************************************************
* Initial Startup
*************************************************/
void setup() {
// COMMUNICATION
// Serial, sfx, and accelerometer, respectively
Serial.begin(9600);
ss.begin(9600);
Wire.begin();
Wire.beginTransmission(MPU_addr);
Wire.write(0x6B);
Wire.write(0);
Wire.endTransmission(true);
// SET PIN MODES
// leds
pinMode(redLED, OUTPUT);
pinMode(blueLED, OUTPUT);
pinMode(greenLED, OUTPUT);
// buttons
pinMode(bgModeButton, INPUT);
pinMode(fxModeButton, INPUT);
pinMode(b1, INPUT);
pinMode(b2, INPUT);
pinMode(b3, INPUT);
pinMode(b4, INPUT);
}
/*************************************************
* Loop
*************************************************/
void loop() {
// Checks if harp is picked up
moveThatFey();
// Control Mode & Settings
setMode();
// Change LEDs and Sounds based on setMode() feedback
modeHandling();
ledHandling();
// If in loop mode, enable looping
loopThatBg();
delay(5);
}
/*************************************************
* My Functions
*************************************************
+++ TABLE OF CONTENTS +++
1.0 // State Logic
2.0 // LEDs
3.0 // Looping
4.0 // Other
*/
// 1.0 STATE LOGIC
// change board mode or "state"
// Uses larger buttons located near base
void setMode() {
int bgModeState = digitalRead(bgModeButton);
int fxModeState = digitalRead(fxModeButton);
// mode selection
// EXPERIMENTAL MODE
if (fxModeState == HIGH && bgModeState == HIGH) {
stateOfBoard = 3;
isLooping = false;
// FX MODE
} else if (fxModeState == HIGH) {
stateOfBoard = 2;
isLooping = false;
// LOOP MODE
} else if (bgModeState == HIGH) {
stateOfBoard = 1;
isLooping = true;
// DEFAULT, NON LOOP MUSIC
} else {
stateOfBoard = 0;
isLooping = false;
}
}
// depending on mode, play certain sound list
// case 3 is special, activates looping
void modeHandling() {
int b1State = digitalRead(b1);
int b2State = digitalRead(b2);
int b3State = digitalRead(b3);
int b4State = digitalRead(b4);
switch (stateOfBoard) {
// NON LOOP: WHITE
case 0:
if (b1State == HIGH) {
sfx.playTrack(4); // project k ish
} else if (b2State == HIGH) {
sfx.playTrack(2); // medieval entry
} else if (b3State == HIGH) {
sfx.playTrack(6); // happy tune
} else if (b4State == HIGH) {
sfx.playTrack(3); // march
}
break;
// LOOP MODE: BLUE
case 1:
if (b1State == HIGH) {
sfx.playTrack(4); // project k ish
loopingTrack = 4;
} else if (b2State == HIGH) {
sfx.playTrack(2); // medieval entry
loopingTrack = 2;
} else if (b3State == HIGH) {
sfx.playTrack(6); // happy tune
loopingTrack = 6;
} else if (b4State == HIGH) {
sfx.playTrack(9); // medieval entry
loopingTrack = 9;
}
break;
// FX MODE: RED
case 2:
if (b1State == HIGH) {
sfx.playTrack(1); // magic fx
} else if (b2State == HIGH) {
sfx.playTrack(9); // harpsichord
} else if (b3State == HIGH) {
sfx.playTrack(5); // monster
} else if (b4State == HIGH) {
sfx.playTrack(3); // march
}
break;
// EXPERIMENTAL MODE, PURPLE (RANDOM)
case 3:
int fairyChoice = random(0,4);
sfx.playTrack(fairySongs[fairyChoice]);
break;
default:
break;
}
}
// 2.0 LEDS
// depending on state of board, change LED
// Fade in over short time
void ledHandling() {
turnOffLEDs();
switch (stateOfBoard) {
case 0:
digitalWrite(greenLED, HIGH);
digitalWrite(redLED, HIGH);
digitalWrite(blueLED, HIGH);
break;
case 1:
digitalWrite(blueLED, HIGH);
break;
case 2:
digitalWrite(redLED, HIGH);
break;
case 3:
digitalWrite(redLED, HIGH);
digitalWrite(blueLED, HIGH);
break;
default:
break;
}
}
// turn off LEDs on state change
void turnOffLEDs() {
digitalWrite(redLED, LOW);
digitalWrite(greenLED, LOW);
digitalWrite(blueLED, LOW);
}
// 3.0 LOOPING
// stop loop if mode is changed
// disables loop for 1st and 3rd mode
void pauseAndStopLoop() {
isLooping = false;
loopingTrack = 0;
sfx.stop();
}
// loop sound if mode 2
void loopThatBg() {
if (isLooping == true && loopingTrack != 0) {
sfx.playTrack(loopingTrack);
}
}
// 4.0 OTHER
void moveThatFey() {
// Get Data from Accelerometer
Wire.beginTransmission(MPU_addr);
Wire.write(0x3B);
Wire.endTransmission(false);
Wire.requestFrom(MPU_addr,14,true);
AcX=Wire.read()<<8|Wire.read();
AcY=Wire.read()<<8|Wire.read();
AcZ=Wire.read()<<8|Wire.read();
Tmp=Wire.read()<<8|Wire.read();
GyX=Wire.read()<<8|Wire.read();
GyY=Wire.read()<<8|Wire.read();
GyZ=Wire.read()<<8|Wire.read();
int mapMovement = (map(AcZ, -10000, 10000, 0, 100));
mapMovement = -mapMovement;
Serial.println(mapMovement);
if (-1 * mapMovement > 20) {
if (fairyIsAwake == false) {
fairyIsAwake = true;
sfx.playTrack(1);
}
}
}
/* For testing only
void variableTest() {
Serial.println("state of board is:"+stateOfBoard);
Serial.println("fx button is:"+fxModeState);
Serial.println("bg button is:"+bgModeState);
Serial.println("b1 is:"+b1State);
Serial.println("b2 is:"+b2State);
Serial.println("b3 is:"+b3State);
Serial.println("b4 is:"+b4State);
Serial.println("b5 is:"+b5State);
}
|