-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMemoryGame.ino
286 lines (253 loc) · 6.89 KB
/
MemoryGame.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
/**
* 1. Connect to power
* 2. Green LED will turn on when game is prepared.
* 3. Push Main button (Without color assigned)
* 4. All LEDs will turn on and one LED will kick off the game
* 5. Enjoy the game!
* 6. If you push wrong Color button during the game, LEDs will blink few times and you have to reset the game using Main button.
*
* Tip: If you want to set up difficulty of the game, hold Main button in any phase for a while until all LEDs start blinking rappidly.
* LEDs will show currently selected level of difficulty in binary code. Increase difficulty using Red button and decrease it using Yellow.
* Hold Main button again to confirm your selection.
*/
const char colors[4] = {'r', 'g', 'b', 'y'};
const int redLedPin = 2;
const int greenLedPin = 3;
const int blueLedPin = 4;
const int yellowLedPin = 5;
const int redButtonPin = 6;
const int greenButtonPin = 7;
const int blueButtonPin = 8;
const int yellowButtonPin = 9;
const int newGameButtonPin = 10;
const int maxGameLength = 100;
char game[maxGameLength];
int currentLevelColorIndex = -1;
int currentLevel = -1;
int pushedButtonPin = -1;
unsigned long lastButtonChangeTime = 0;
int difficulty = 1;
const int difficultySpaceSize = 9;
const int gameDifficulties[difficultySpaceSize] = {1000, 900, 800, 700, 600, 500, 400, 300, 200};
const int prepared_mode = 0;
const int game_mode = 1;
const int difficulty_mode = 2;
int currentMode;
void setup() {
Serial.begin(9600);
pinMode(redLedPin, OUTPUT);
pinMode(greenLedPin, OUTPUT);
pinMode(blueLedPin, OUTPUT);
pinMode(yellowLedPin, OUTPUT);
pinMode(redButtonPin, INPUT);
pinMode(greenButtonPin, INPUT);
pinMode(blueButtonPin, INPUT);
pinMode(yellowButtonPin, INPUT);
pinMode(newGameButtonPin, INPUT);
currentMode = prepared_mode;
}
void loop() {
if((millis() - lastButtonChangeTime) < 100) {
return;
}
switch (currentMode) {
case prepared_mode:
digitalWrite(greenLedPin, HIGH);
break;
case difficulty_mode:
displayCurrentDifficulty();
break;
}
if (pushedButtonPin == -1) {
checkButtonPush();
}
else {
checkButtonRelease();
}
}
void checkButtonPush() {
if (digitalRead(newGameButtonPin) == HIGH) {
pushButton(newGameButtonPin);
}
else if (digitalRead(redButtonPin) == HIGH) {
pushButton(redButtonPin);
}
else if (digitalRead(greenButtonPin) == HIGH) {
pushButton(greenButtonPin);
}
else if (digitalRead(blueButtonPin) == HIGH) {
pushButton(blueButtonPin);
}
else if (digitalRead(yellowButtonPin) == HIGH) {
pushButton(yellowButtonPin);
}
}
void pushButton(int buttonPin) {
pushedButtonPin = buttonPin;
lastButtonChangeTime = millis();
Serial.println("Pushed button: " + String(pushedButtonPin));
if(currentMode == game_mode) {
if (pushedButtonPin != newGameButtonPin) {
digitalWrite(getLedPinByButtonPin(buttonPin), HIGH);
}
}
}
void checkButtonRelease() {
if (digitalRead(pushedButtonPin) == LOW) {
lastButtonChangeTime = millis();
switch (currentMode) {
case prepared_mode:
if (pushedButtonPin == newGameButtonPin) {
blinkAllColors(1000, 1);
resetGame();
playLevel();
currentMode = game_mode;
}
break;
case game_mode:
if (pushedButtonPin == newGameButtonPin) {
blinkAllColors(1000, 1);
resetGame();
playLevel();
} else {
releaseColorButton(pushedButtonPin);
}
break;
case difficulty_mode:
if (pushedButtonPin == redButtonPin) {
if (difficulty < difficultySpaceSize) {
difficulty += 1;
}
}
else if(pushedButtonPin == yellowButtonPin) {
if (difficulty > 1) {
difficulty -= 1;
}
}
break;
}
pushedButtonPin = -1;
} else {
if(pushedButtonPin == newGameButtonPin and (millis() - lastButtonChangeTime) > 1000) {
blinkAllColors(150, 5);
if (currentMode == game_mode) {
currentMode = difficulty_mode;
} else {
currentMode = game_mode;
}
}
}
}
void releaseColorButton(int buttonPin) {
digitalWrite(getLedPinByButtonPin(buttonPin), LOW);
char buttonColor = getButtonColor(buttonPin);
if (buttonColor == game[currentLevelColorIndex]) {
currentLevelColorIndex += 1;
if (currentLevelColorIndex > currentLevel) {
delay(1000);
nextLevel();
playLevel();
}
}
else {
gameLost(buttonColor);
}
}
void resetGame() {
game[0] = getRandomColor();
for (int i = 1; i < maxGameLength; i++) {
game[i] = '-';
}
currentLevel = 0;
currentLevelColorIndex = 0;
}
void nextLevel() {
currentLevel += 1;
currentLevelColorIndex = 0;
game[currentLevel] = getRandomColor();
}
void playLevel() {
Serial.println("Level #" + String(currentLevel + 1));
displayLevel();
}
void gameLost(char pushedColor) {
Serial.println("Game lost :( You pushed color " + String(pushedColor) + " but you should push " + String(game[currentLevelColorIndex]));
blinkAllColors(250, 3);
}
char getRandomColor() {
int randomNum = random(0, 4);
return colors[randomNum];
}
void displayLevel() {
int gameSpeed = gameDifficulties[difficulty - 1];
for (int i = 0; i <= currentLevel; i++) {
blinkLed(game[i], gameSpeed);
if (i != currentLevel) {
delay(gameSpeed);
}
}
}
int getLedPinByColor(char color) {
switch (color) {
case 'r':
return redLedPin;
case 'g':
return greenLedPin;
case 'b':
return blueLedPin;
case 'y':
return yellowLedPin;
}
}
int getLedPinByButtonPin(int buttonPin) {
switch (buttonPin) {
case redButtonPin:
return redLedPin;
case greenButtonPin:
return greenLedPin;
case blueButtonPin:
return blueLedPin;
case yellowButtonPin:
return yellowLedPin;
}
}
char getButtonColor(int buttonPin) {
switch (buttonPin) {
case redButtonPin:
return 'r';
case greenButtonPin:
return 'g';
case blueButtonPin:
return 'b';
case yellowButtonPin:
return 'y';
}
}
void blinkAllColors(int blink_delay, int repeat) {
for (int i = 0; i < repeat; i++) {
digitalWrite(redLedPin, HIGH);
digitalWrite(greenLedPin, HIGH);
digitalWrite(blueLedPin, HIGH);
digitalWrite(yellowLedPin, HIGH);
delay(blink_delay);
digitalWrite(redLedPin, LOW);
digitalWrite(greenLedPin, LOW);
digitalWrite(blueLedPin, LOW);
digitalWrite(yellowLedPin, LOW);
if (i + 1 != repeat) {
delay(blink_delay);
}
}
}
void blinkLed(char color, int blink_delay) {
int ledPin = getLedPinByColor(color);
digitalWrite(ledPin, HIGH);
delay(blink_delay);
digitalWrite(ledPin, LOW);
}
void displayCurrentDifficulty() {
digitalWrite(yellowLedPin, bitRead(difficulty, 0));
digitalWrite(blueLedPin, bitRead(difficulty, 1));
digitalWrite(greenLedPin, bitRead(difficulty, 2));
digitalWrite(redLedPin, bitRead(difficulty, 3));
}