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
| /*
* Author: Denis Cosmin
* Date: 19.09.2016
* Name: Simon Says
*
* To add a reset button connect: reset -> button <- pulldown resistor ground.
* The buttons have 1k pulldown resitors.
* The leds have a 220 ohm resistor.
*
* Will use the following numbers for colors, pins and notes
* 0 - Yellow
* 1 - Green
* 2 - Red
* 3 - Blue
*
*/
/*
* The game class, handles everything.
*/
class Game {
private:
int debounce(int last, int buttonPin);
void playNote(int note, int noteSpeed) const;
void flashLed(int led, int flashSpeed) const;
public:
static const int RED_PIN;
static const int BLUE_PIN;
static const int GREEN_PIN;
static const int YELLOW_PIN;
static const int MICROPHONE_PIN;
static const int RED_BUTTON_PIN;
static const int BLUE_BUTTON_PIN;
static const int GREEN_BUTTON_PIN;
static const int YELLOW_BUTTON_PIN;
static const int RED_TONE;
static const int BLUE_TONE;
static const int GREEN_TONE;
static const int YELLOW_TONE;
static const int GAMEOVER_TONE;
int gameLevel[200];
int gameSpeed;
int lastButtonValue;
int currentLevel;
int gameIsOver;
double gameDifficulty;
enum color { YELLOW, GREEN, RED, BLUE };
public:
Game();
Game(int);
void playLevel();
int userInput();
int gameOver();
int getNote(int note) const;
int pinToColorCode(int);
int colorCodeToPin(int);
int readButton(int buttonPin);
};
/* Pin settings */
static const int Game::MICROPHONE_PIN = 12;
static const int Game::BLUE_PIN = 11;
static const int Game::RED_PIN = 10;
static const int Game::GREEN_PIN = 9;
static const int Game::YELLOW_PIN = 8;
static const int Game::BLUE_BUTTON_PIN = 7;
static const int Game::RED_BUTTON_PIN = 6;
static const int Game::GREEN_BUTTON_PIN = 5;
static const int Game::YELLOW_BUTTON_PIN = 4;
/* Tone frequencies */
static const int Game::RED_TONE = 200;
static const int Game::BLUE_TONE = 400;
static const int Game::YELLOW_TONE = 600;
static const int Game::GREEN_TONE = 800;
static const int Game::GAMEOVER_TONE = 1000;
// Construct and initialize the Game object.
Game::Game(int difficulty) : gameSpeed(1000), lastButtonValue(-1), currentLevel(0), gameDifficulty(difficulty), gameIsOver(0) {
Serial.print("Constructing game object with difficulty: ");
Serial.println(difficulty);
pinMode(Game::MICROPHONE_PIN, OUTPUT);
pinMode(Game::BLUE_PIN, OUTPUT);
pinMode(Game::RED_PIN, OUTPUT);
pinMode(Game::GREEN_PIN, OUTPUT);
pinMode(Game::YELLOW_PIN, OUTPUT);
}
Game::Game() : gameSpeed(1000), lastButtonValue(-1), currentLevel(0), gameDifficulty(10), gameIsOver(0) {
Serial.println("Constructing game object");
pinMode(Game::MICROPHONE_PIN, OUTPUT);
pinMode(Game::BLUE_PIN, OUTPUT);
pinMode(Game::RED_PIN, OUTPUT);
pinMode(Game::GREEN_PIN, OUTPUT);
pinMode(Game::YELLOW_PIN, OUTPUT);
}
/*
* Makes sure the button is pressed only once.
*/
int Game::debounce(int last, int buttonPin) {
int current = digitalRead(buttonPin);
if (last != current)
{
delay(5);
current = digitalRead(buttonPin);
}
return current;
}
/*
* Plays a note.
* Receives the button number and plays the corresponding note.
*/
void Game::playNote(int note, int noteSpeed) const {
Serial.print("playNote: Playing note: ");
Serial.print(note);
Serial.print(" with speed: ");
Serial.println(noteSpeed);
note = Game::getNote(note);
tone(Game::MICROPHONE_PIN, note, noteSpeed);
}
/*
* Returns the corresponding color code based on pin.
*/
int Game::colorCodeToPin(int value) {
int ret_val = -1;
switch(value) {
case RED:
ret_val = Game::RED_PIN;
break;
case GREEN:
ret_val = Game::GREEN_PIN;
break;
case BLUE:
ret_val = Game::BLUE_PIN;
break;
case YELLOW:
ret_val = Game::YELLOW_PIN;
break;
default:
Serial.println("colorCodeToPin: Invalid value!");
delay(1000);
exit(0);
}
return ret_val;
}
/*
* Converts the button pin to a color code.
*/
int Game::pinToColorCode(int value) {
int ret_val = -1;
switch(value) {
case Game::RED_BUTTON_PIN:
ret_val = RED;
break;
case Game::GREEN_BUTTON_PIN:
ret_val = GREEN;
break;
case Game::BLUE_BUTTON_PIN:
ret_val = BLUE;
break;
case Game::YELLOW_BUTTON_PIN:
ret_val = YELLOW;
break;
default:
Serial.println("pinToColorCode: Invalid value!");
delay(1000);
exit(0);
}
return ret_val;
}
/*
* The the corresponding note based on the color code it receives.
*/
int Game::getNote(int note) const {
int return_value = -1;
switch(note) {
case YELLOW:
return_value = Game::YELLOW_TONE;
break;
case GREEN:
return_value = Game::GREEN_TONE;
break;
case RED:
return_value = Game::RED_TONE;
break;
case BLUE:
return_value = Game::BLUE_TONE;
break;
case 4:
return_value = Game::GAMEOVER_TONE;
break;
default:
Serial.println("playNote: Error! Invalid note!");
delay(1000);
exit(0);
}
return return_value;
}
/*
* Flashes a led. Receives the led code and sets it to the corresponding pin.
*/
void Game::flashLed(int led, int flashSpeed) const {
Serial.print("flashLed: Flashing LED: ");
Serial.print(led);
Serial.print(" with speed: ");
Serial.println(flashSpeed);
led = Game::colorCodeToPin(led);
digitalWrite(led, HIGH);
delay(flashSpeed);
digitalWrite(led, LOW);
}
/*
* Plays the next level.
*/
void Game::playLevel() {
Serial.print("playLevel: Playing on level: ");
Serial.println(Game::currentLevel);
Game::gameLevel[Game::currentLevel] = random(0, 4); // Create a random move every time. 0 to 4 exclusive.
++Game::currentLevel;
int nextDificulty = Game::gameDifficulty * Game::currentLevel;
if (Game::gameSpeed - nextDificulty >= 10) {
Game::gameSpeed -= nextDificulty; // decrease the speed;
}
// Play all the moves
for (int i = 0; i < Game::currentLevel; ++i) {
Game::playNote(Game::gameLevel[i], Game::gameSpeed);
Game::flashLed(Game::gameLevel[i], Game::gameSpeed);
}
}
/*
* Reads the button value and returns the following codes:
* 0 - Yellow 1 - Green 2 - Red 3 - Blue
*/
int Game::readButton(int buttonPin) {
int currentButtonValue = Game::debounce(Game::lastButtonValue, buttonPin);
int return_value = -1;
if (lastButtonValue == LOW && currentButtonValue > LOW) {
return_value = Game::pinToColorCode(buttonPin);
}
Game::lastButtonValue = currentButtonValue;
if (return_value >= 0) {
Serial.print("readButton: Received signal from button number: ");
Serial.println(return_value);
}
return return_value;
}
int Game::gameOver() {
Serial.println("game_is_over: Checking if game is over!");
if (Game::gameIsOver) {
Serial.println("game_is_over: Game is over!");
}
return Game::gameIsOver;
}
/*
* Gets the user button presses and checks them to see if they're good.
*/
int Game::userInput() {
for (int i = 0; i < Game::currentLevel; ++i) {
Serial.println("userInput: User is pressing.");
int buttonPressed = -1;
while(true) {
buttonPressed = readButton(Game::RED_BUTTON_PIN);
if (buttonPressed != -1) { break; }
buttonPressed = readButton(Game::GREEN_BUTTON_PIN);
if (buttonPressed != -1) { break; }
buttonPressed = readButton(Game::YELLOW_BUTTON_PIN);
if (buttonPressed != -1) { break; }
buttonPressed = readButton(Game::BLUE_BUTTON_PIN);
if (buttonPressed != -1) { break; }
}
if (buttonPressed != gameLevel[i]) {
Game::playNote(4, 100); // game over note, and game over note speed.
Game::flashLed(buttonPressed, 1000);
return 0;
}
Game::playNote(buttonPressed, Game::gameSpeed);
Game::flashLed(buttonPressed, Game::gameSpeed);
}
delay(500);
return 1;
}
Game g(50); // Constructs the game object.
void setup() {
Serial.begin(9600);
randomSeed(0);
}
void loop() {
if (g.gameOver()) {
delay(1000); // Wait for serial to finish printing.
/*
On Arduino exit(0) disables the interrupts
and goes in an infinite loop.
On your PC exit(0) closes the program and
tries to clean up resources.
*/
exit(0);
}
g.playLevel();
if (g.userInput() == 0) {
g.gameIsOver = 1;
}
}
|