-
Notifications
You must be signed in to change notification settings - Fork 0
/
nBackGame.pde
428 lines (391 loc) · 10.5 KB
/
nBackGame.pde
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
/*
N-Back Game Arduino Sketch
Copyright (C) 2012 Brent Smith
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
// pin mappings
const int blueLEDPin = 2;
const int greenLEDPin = 4;
const int redLEDPin = 3;
const int SCORE_PIN_1 = 9;
const int SCORE_PIN_2 = 8;
const int SCORE_PIN_3 = 10;
const int SCORE_PIN_4 = 7;
const int SCORE_PIN_5 = 6;
const int SCORE_PIN_6 = 5;
const int Player1ButtonPin = A3;
const int Player2ButtonPin = A2;
// game settings
const int nbackDelay = 1500;
// play states
const int GAME_STATE_PREP = -1;
const int GAME_STATE_INIT = 0;
const int GAME_STATE_SELECT_LEVEL = 1;
const int GAME_STATE_START = 2;
const int GAME_STATE_ACTIVE= 3;
const int GAME_STATE_OVER = 4;
const int GAME_STATE_RESET = 5;
int gameState = -1;
const int PLAY_STATE_CLEAR = 0;
const int PLAY_STATE_ACTIVE= 1;
const int PLAY_STATE_NEXT = 2;
int playState = 0;
int player1ButtonState = 0;
int player2ButtonState = 0;
int player1PreviousButtonState = 0;
int player2PreviousButtonState = 0;
int level = 0;
boolean player1Clicked = false;
boolean player2Clicked = false;
boolean player1HadClicked = false;
boolean player2HadClicked = false;
boolean player1Accepted = false;
boolean player2Accepted = false;
boolean initalized = false;
int history[7];
int historyIterator = 0;
int historyFull = false;
int score = 0;
boolean hit = false;
long startTime = 0;
int colour = 0;
boolean turnScored = false;
int gameOverCounter = 0;
void setup() {
Serial.begin(9600);
pinMode(redLEDPin, OUTPUT);
pinMode(greenLEDPin, OUTPUT);
pinMode(blueLEDPin, OUTPUT);
pinMode(SCORE_PIN_1, OUTPUT);
pinMode(SCORE_PIN_2, OUTPUT);
pinMode(SCORE_PIN_3, OUTPUT);
pinMode(SCORE_PIN_4, OUTPUT);
pinMode(SCORE_PIN_5, OUTPUT);
pinMode(SCORE_PIN_6, OUTPUT);
randomSeed(analogRead(0));
}
void loop(){
switch (gameState) {
case GAME_STATE_PREP:
gameOverCounter = 0;
level = 0;
score = 0;
player1Accepted = false;
player2Accepted = false;
playState = PLAY_STATE_CLEAR;
setClear();
scorePinDemo();
gameState = GAME_STATE_INIT;
break;
// Wait for player one to initialize the game
case GAME_STATE_INIT:
if (checkPlayer1Click()){
setGreen();
delay(1000);
setClear();
gameState = GAME_STATE_SELECT_LEVEL;
}
break;
case GAME_STATE_SELECT_LEVEL:
if (checkPlayer1Click()){
level ++;
lightLevel(level);
setBlue();
delay(500);
setClear();
}
if (checkPlayer2Click()){
setGreen();
delay(500);
setClear();
gameState = GAME_STATE_START;
}
break;
case GAME_STATE_START:
if (checkPlayer1Click()){
Serial.println("Player 1 Accepted");
fillPlayer1();
player1Accepted = true;
}
if (checkPlayer2Click()){
Serial.println("Player 2 Accepted");
fillPlayer2();
player2Accepted = true;
}
if (player1Accepted && player2Accepted){
delay(500);
clearPlayer1();
clearPlayer2();
startupPattern();
gameState = GAME_STATE_ACTIVE;
}
break;
case GAME_STATE_ACTIVE:
switch(playState){
case PLAY_STATE_CLEAR:
Serial.println("Game Clear");
setClear();
delay(1000);
playState = PLAY_STATE_NEXT;
break;
case PLAY_STATE_NEXT:
Serial.println("Next");
player1HadClicked = false;
player2HadClicked = false;
// update the colour
colour = random(0,3);
if (colour == 1){
setRed();
}else if (colour == 2){
setGreen();
}else{
setBlue();
}
startTime = millis();
// don't bother checking for hits until we've had a sufficient
// number of colours appear
if (historyFull){
// check for player1 nback hit
hit = (colour == history[historyIterator]);
}
history[historyIterator] = colour;
if (historyIterator == level - 1){
historyIterator = 0;
historyFull = true;
}else{
historyIterator++;
}
Serial.print(colour);
Serial.print(" : ");
if (hit){
Serial.println("hit");
}else{
Serial.println("miss");
}
turnScored = false;
playState = PLAY_STATE_ACTIVE;
break;
case PLAY_STATE_ACTIVE:
if (checkPlayer1Click() && !turnScored){
if (hit){
score --;
}else{
score ++;
}
checkScore();
turnScored = true;
//playState = PLAY_STATE_CLEAR;
}
if (checkPlayer2Click() && !turnScored){
if (hit){
score ++;
}else{
score --;
}
checkScore();
turnScored = true;
//playState = PLAY_STATE_CLEAR;
}
if (millis() - startTime >= nbackDelay){
playState = PLAY_STATE_CLEAR;
}
break;
}
break;
case GAME_STATE_OVER:
Serial.println("Game Over");
gameOverCounter ++;
if (gameOverCounter > 5){
gameState = GAME_STATE_PREP;
}
setRed();
delay(200);
setGreen();
delay(200);
setBlue();
delay(200);
break;
default:
Serial.println("ERROR IN STATE");
}
}
//TODO: combine these using and argument
boolean checkPlayer1Click(){
// read the state of the pushbutton value:
player1ButtonState = analogRead(Player1ButtonPin);
//Serial.print("player1ButtonState: ");
//Serial.println(player1ButtonState);
player1Clicked = (player1ButtonState > 500 && player1PreviousButtonState < 500);
player1PreviousButtonState = player1ButtonState;
return player1Clicked;
}
boolean checkPlayer2Click(){
// read the state of the pushbutton value:
player2ButtonState = analogRead(Player2ButtonPin);
//Serial.print("player2ButtonState: ");
//Serial.println(player2ButtonState);
player2Clicked = (player2ButtonState > 500 && player2PreviousButtonState < 500);
player2PreviousButtonState = player2ButtonState;
return player2Clicked;
}
void checkScore(){
Serial.print("Score:");
Serial.println(score);
clearPlayer1();
clearPlayer2();
if(score == 0){
//No Lights for even score
}else if (score == 1){
digitalWrite(SCORE_PIN_4,HIGH);
}else if (score == 2){
digitalWrite(SCORE_PIN_5,HIGH);
}else if (score == 3){
digitalWrite(SCORE_PIN_6,HIGH);
}else if (score == 4){
digitalWrite(SCORE_PIN_4,HIGH);
digitalWrite(SCORE_PIN_5,HIGH);
digitalWrite(SCORE_PIN_6,HIGH);
gameOver();
}else if (score == -1){
digitalWrite(SCORE_PIN_1,HIGH);
}else if (score == -2){
digitalWrite(SCORE_PIN_2,HIGH);
}else if (score == -3){
digitalWrite(SCORE_PIN_3,HIGH);
}else if (score == -4){
digitalWrite(SCORE_PIN_1,HIGH);
digitalWrite(SCORE_PIN_2,HIGH);
digitalWrite(SCORE_PIN_3,HIGH);
gameOver();
}else{
Serial.println("SCORE ERROR");
}
}
void gameOver(){
Serial.println("Game Over");
gameState = GAME_STATE_OVER;
}
// This sequence is the countdown to the start of the game.
void startupPattern(){
setGreen();
delay(500);
setClear();
delay(500);
setGreen();
delay(500);
setClear();
delay(500);
setGreen();
delay(500);
setClear();
delay(1000);
}
void lightLevel(int num){
clearPlayer1();
if (num == 1){
digitalWrite(SCORE_PIN_1, HIGH);
}
if (num == 2){
digitalWrite(SCORE_PIN_2, HIGH);
}
if (num == 3){
digitalWrite(SCORE_PIN_1, HIGH);
digitalWrite(SCORE_PIN_2, HIGH);
}
if (num == 4){
digitalWrite(SCORE_PIN_3, HIGH);
}
if (num == 5){
digitalWrite(SCORE_PIN_1, HIGH);
digitalWrite(SCORE_PIN_3, HIGH);
}
if (num == 6){
digitalWrite(SCORE_PIN_2, HIGH);
digitalWrite(SCORE_PIN_3, HIGH);
}
if (num == 7){
digitalWrite(SCORE_PIN_1, HIGH);
digitalWrite(SCORE_PIN_2, HIGH);
digitalWrite(SCORE_PIN_3, HIGH);
}
}
void clearPlayer1(){
digitalWrite(SCORE_PIN_1, LOW);
digitalWrite(SCORE_PIN_2, LOW);
digitalWrite(SCORE_PIN_3, LOW);
}
void clearPlayer2(){
digitalWrite(SCORE_PIN_4, LOW);
digitalWrite(SCORE_PIN_5, LOW);
digitalWrite(SCORE_PIN_6, LOW);
}
void fillPlayer1(){
digitalWrite(SCORE_PIN_1, HIGH);
digitalWrite(SCORE_PIN_2, HIGH);
digitalWrite(SCORE_PIN_3, HIGH);
}
void fillPlayer2(){
digitalWrite(SCORE_PIN_4, HIGH);
digitalWrite(SCORE_PIN_5, HIGH);
digitalWrite(SCORE_PIN_6, HIGH);
}
// set the LED to Off
void setClear(){
digitalWrite(greenLEDPin, LOW);
digitalWrite(blueLEDPin, LOW);
digitalWrite(redLEDPin, LOW);
}
// set the LED to Red
void setRed(){
digitalWrite(greenLEDPin, LOW);
digitalWrite(blueLEDPin, LOW);
digitalWrite(redLEDPin, HIGH);
}
// set the LED to Blue
void setBlue(){
digitalWrite(redLEDPin, LOW);
digitalWrite(greenLEDPin, LOW);
digitalWrite(blueLEDPin, HIGH);
}
// set the LED to Green
void setGreen(){
digitalWrite(redLEDPin, LOW);
digitalWrite(blueLEDPin, LOW);
digitalWrite(greenLEDPin, HIGH);
}
void scorePinDemo(){
int demoDelay = 250;
digitalWrite(SCORE_PIN_1, LOW);
digitalWrite(SCORE_PIN_2, LOW);
digitalWrite(SCORE_PIN_3, LOW);
digitalWrite(SCORE_PIN_4, LOW);
digitalWrite(SCORE_PIN_5, LOW);
digitalWrite(SCORE_PIN_6, LOW);
digitalWrite(SCORE_PIN_1, HIGH);
delay(demoDelay);
digitalWrite(SCORE_PIN_1, LOW);
digitalWrite(SCORE_PIN_2, HIGH);
delay(demoDelay);
digitalWrite(SCORE_PIN_2, LOW);
digitalWrite(SCORE_PIN_3, HIGH);
delay(demoDelay);
digitalWrite(SCORE_PIN_3, LOW);
digitalWrite(SCORE_PIN_4, HIGH);
delay(demoDelay);
digitalWrite(SCORE_PIN_4, LOW);
digitalWrite(SCORE_PIN_5, HIGH);
delay(demoDelay);
digitalWrite(SCORE_PIN_5, LOW);
digitalWrite(SCORE_PIN_6, HIGH);
delay(demoDelay);
digitalWrite(SCORE_PIN_6, LOW);
}