-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLightgame4kids.ino
executable file
·349 lines (301 loc) · 10.9 KB
/
Lightgame4kids.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
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
#include "stdlib.h"
#include "pitches.h"
#include "StopWatch.h"
#define MAX_PATTERN_SIZE 20
// initialize a stopwatch
StopWatch MySW;
// initialize the timers for each color
// the timers will keep the sum of the time it took
// to press correctly the given color
int yellow_timer=0;
int red_timer=0;
int green_timer=0;
// the rgb led pins
int buzzer = 7;
int divisionResult;
// the rgb led pins
int pin_rgb_red = 11;
int pin_rgb_green = 10;
int pin_rgb_blue = 9;
// the rgb led pins
int button_red = 5;
int button_green = 6;
int button_yellow = 4;
// stage_game values :
// 0 - wait for serial command to begin the game
// 1 - start the game
// 2 - ending the game
// at the ending stage, it returns to 0 value, waiting again for serial command
int stage_game = 0;
// initialize the string we will use
// for the pattern
String pattern;
// reaction_times[] array will keep the reaction time
// for each color.
// if the wrong color is pressed it will have a 0 value
int reaction_times[MAX_PATTERN_SIZE];
// initialize the current turn to 0
// the turn will be used as index in the pattern() array
// to display the correct color each time
int turn=0;
// the pace of music
int noteDuration = 115;
//5 notes
int melody_correct[] = {NOTE_AS6,NOTE_F7,NOTE_AS7,NOTE_DS8,NOTE_D8};
//5 notes
int melody_fault[] = {NOTE_E6,NOTE_A5,NOTE_AS5,NOTE_E5,NOTE_F5};
//8 notes
int melody_tension[] = {NOTE_G6,NOTE_DS7,NOTE_B6,NOTE_G6,NOTE_DS6,NOTE_C6,NOTE_A5,NOTE_G5};
//64 notes
int melody_outro[] = {
NOTE_G5,NO_SOUND,NO_SOUND,NO_SOUND,NOTE_G4,NO_SOUND,NOTE_G5,NO_SOUND,
NOTE_D5,NO_SOUND,NO_SOUND,NO_SOUND,NOTE_D4,NO_SOUND,NOTE_D5,NO_SOUND,
NOTE_E5,NO_SOUND,NO_SOUND,NO_SOUND,NOTE_E4,NO_SOUND,NOTE_E5,NO_SOUND,
NOTE_B4,NO_SOUND,NO_SOUND,NO_SOUND,NOTE_B3,NO_SOUND,NOTE_B4,NO_SOUND,
NOTE_G5,NO_SOUND,NO_SOUND,NO_SOUND,NOTE_C4,NO_SOUND,NOTE_G5,NO_SOUND,
NOTE_G4,NO_SOUND,NO_SOUND,NO_SOUND,NOTE_G3,NO_SOUND,NOTE_G4,NO_SOUND,
NOTE_C5,NO_SOUND,NO_SOUND,NO_SOUND,NOTE_C4,NO_SOUND,NOTE_C5,NO_SOUND,
NOTE_D5,NO_SOUND,NO_SOUND,NO_SOUND,NOTE_D4,NO_SOUND,NOTE_D5,NO_SOUND
};
// 320 notes
int melody_intro[] = {
NOTE_C5,NOTE_C5,NOTE_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S6,NO_SOUND,NOTE_GS4,NO_SOUND,NOTE_GS6,NO_SOUND,
NOTE_GS4,NO_SOUND,NOTE_GS6,NO_SOUND,NOTE_GS4,NO_SOUND,NOTE_GS6,NO_SOUND,
NOTE_C5,NO_SOUND,NO_SOUND,NO_SOUND,NOTE_C5,NO_SOUND,NO_SOUND,NO_SOUND,
NOTE_C5,NO_SOUND,NO_SOUND,NO_SOUND,NOTE_C5,NO_SOUND,NOTE_C7,NO_SOUND,
NOTE_CS5,NO_SOUND,NO_SOUND,NO_SOUND,NOTE_CS5,NO_SOUND,NO_SOUND,NO_SOUND,
NOTE_CS5,NO_SOUND,NO_SOUND,NO_SOUND,NOTE_CS5,NO_SOUND,NO_SOUND,NO_SOUND,
NOTE_F4,NO_SOUND,NO_SOUND,NO_SOUND,NOTE_F4,NO_SOUND,NO_SOUND,NO_SOUND,
NOTE_F4,NO_SOUND,NO_SOUND,NO_SOUND,NOTE_F4,NO_SOUND,NO_SOUND,NO_SOUND,
NOTE_GS4,NO_SOUND,NO_SOUND,NO_SOUND,NOTE_GS4,NO_SOUND,NO_SOUND,NO_SOUND,
NOTE_GS4,NO_SOUND,NO_SOUND,NO_SOUND,NOTE_GS4,NO_SOUND,NO_SOUND,NO_SOUND,
};
void setup()
{
// declare buzzer pin as output
pinMode(buzzer, OUTPUT);
// declare the rgb pins as outputs
pinMode(pin_rgb_red, OUTPUT);
pinMode(pin_rgb_blue, OUTPUT);
pinMode(pin_rgb_green, OUTPUT);
// declare the buttons pins as inputs
pinMode(button_red, INPUT);
pinMode(button_green, INPUT);
pinMode(button_yellow, INPUT);
//initialize reaction_times array
for(int i; i<20; i++){
reaction_times[i]=0;
}
//turn on serial communication at 9600
Serial.begin(9600);
}
void loop()
{
// stage_game has initial value 0
// at this stage it will get commands and data
// through the serial
//
// when the command to start the game is given
// it will get the value of 1 and ignore the serial
// communication untill the game ends or reset button is pressed
//
// when the game ends it will take the value of 2
if(stage_game == 0){
// When there is something sent by serial
// reach each character and save it in c
// add c to the string readString
//
// at the end of the command, readString will contain the
// given command as a string
String readString;
while (Serial.available()) {
delay(3);
if (Serial.available() >0) {
char c = Serial.read();
readString += c;
}
}
// compare the command give through serial
// to the available commands
//
// if it doesn't equal any of them
// treat it as raw data
if ( readString.length() > 0) {
if(readString == "start"){
if(pattern.length() > 0){
// start game, play the intro music
stage_game=1;
play_music(5);
}
}else{
// do something with the raw data
pattern=readString;
}
}
}else if(stage_game == 1){
// small delay between rounds ( 2 sec)
delay(2000);
// button_pressed values :
// 0 - no button is pressed
// 1 - button is pressed
int button_pressed = 0;
// play a little sound before the led lights
play_music(3);
// display the correct color on the rgb led
// based on the pattern given by the doctor
// right after that, the timer begins
if(pattern[turn]=='y'){ // yellow
digitalWrite(pin_rgb_blue, LOW);
analogWrite(pin_rgb_red, 180);
digitalWrite(pin_rgb_green, HIGH);
}else if(pattern[turn]=='r'){ // red
digitalWrite(pin_rgb_blue, LOW);
digitalWrite(pin_rgb_red, HIGH);
digitalWrite(pin_rgb_green, LOW);
}else if(pattern[turn]=='g'){ // green
digitalWrite(pin_rgb_blue, LOW);
digitalWrite(pin_rgb_red, LOW);
digitalWrite(pin_rgb_green, HIGH);
}
MySW.reset();
MySW.start();
// wait for a button to be pressed
//
// when a button is pressed exit the loop
// play the correct music
// increase the correct counter if necessary
// and add the reaction time for that color to it's sum
// then, stop the timer
while( button_pressed == 0){
if( digitalRead(button_red)== HIGH){
button_pressed=1;
if(pattern[turn]=='r'){
play_music(1);
reaction_times[turn]=MySW.elapsed();
}else{
play_music(2);
}
}
if( digitalRead(button_green)== HIGH){
button_pressed=1;
if(pattern[turn]=='g'){
play_music(1);
reaction_times[turn]=MySW.elapsed();
}else{
play_music(2);
}
}
if( digitalRead(button_yellow)== HIGH){
button_pressed=1;
if(pattern[turn]=='y'){
play_music(1);
reaction_times[turn]=MySW.elapsed();
}else{
play_music(2);
}
}
}
MySW.stop();
// turn will increase by 1 to point the next index of the pattern
// if it's equal to the pattern's length, we have display all the colors
turn++;
if(turn == pattern.length()){
// game over stage
stage_game = 2;
}
}else{
delay(1000);
// send back the statistics
for(int i =0; i<pattern.length(); i++){
Serial.write(reaction_times[i]);
divisionResult = reaction_times[i] / 255;
Serial.write(divisionResult);
reaction_times[i] = 0;
}
// game is over, play the ending music
// and wait again for serial commands
play_music(4);
stage_game = 0;
turn=0;
pattern="";
}
}
// function play_music will play the music based on the value
// of menu_music
// menu_music values :
// 1 - correct button pressed
// 2 - wrong button pressed
// 3 - into for each turn just before the rgb led lights
// 4 - game over
// 5 - game begins
void play_music(int menu_music){
// turn rgb led off after a button is pressed
digitalWrite(pin_rgb_blue, LOW);
digitalWrite(pin_rgb_red, LOW);
digitalWrite(pin_rgb_green, LOW);
// play the music
// we play each note from the corresponding array with the melody
// for <noteDuration> seconds ( note duration )
// and wait for <noteDuration*1.2> seconds ( music pace )
if(menu_music==1){
for (int Note = 0; Note <5; Note++) {
tone(buzzer, melody_correct[Note],noteDuration); //Play note
// to distinguish the notes, set a minimum time between them.
delay(noteDuration*1.2);
}
}else if(menu_music==2){
for (int Note = 0; Note <5; Note++) {
tone(buzzer, melody_fault[Note],noteDuration); //Play note
// to distinguish the notes, set a minimum time between them.
delay(noteDuration*1.2);
}
}else if(menu_music==3){
for (int Note = 0; Note <8; Note++) {
tone(buzzer, melody_tension[Note],noteDuration); //Play note
// to distinguish the notes, set a minimum time between them.
delay(noteDuration*1.2);
}
}else if(menu_music==4){
for (int Note = 0; Note <64; Note++) {
tone(buzzer, melody_outro[Note],noteDuration); //Play note
// to distinguish the notes, set a minimum time between them.
delay(noteDuration*1.2);
}
}else if(menu_music==5){
for (int Note = 0; Note <64; Note++) {
tone(buzzer, melody_intro[Note],noteDuration); //Play note
// to distinguish the notes, set a minimum time between them.
delay(noteDuration*1.2);
}
}
}