-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBuzzWire.ino
More file actions
353 lines (304 loc) · 7.94 KB
/
BuzzWire.ino
File metadata and controls
353 lines (304 loc) · 7.94 KB
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
/*
* BuzzWire program -- control LCD display, buttons, game, nails.
* Free Mode: timer on LCD counts up until success or failure
* Challenge Mode: Set a time, timer decrements, failure at zero or contact
* Tournament Mode: Timer is set to elapsed time of last successful run.
*
* Copyright (c) Charles Shapiro, November 2017
This file is part of BuzzWire.
BuzzWire 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.
BuzzWire 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 BuzzWire. If not, see <http://www.gnu.org/licenses/>.
*/
#include <EnableInterrupt.h>
#include <LiquidCrystal.h>
/*
Define SILENT to make this game silent. Otherwise, use the MusicBox library
( http://github.com/lemgandi/MusicBox ) for the tunes.
*/
// #define SILENT
#ifdef SILENT
#pragma message ("SILENT defined. This game will not sing.")
#endif
/*
* Define CHS_DEBUG to log various things to Serial
*/
// #define CHS_DEBUG
#include "BuzzWireTypes.h"
#include "Prototypes.h"
#ifndef SILENT
#include "BuzzWireTunes.h"
#include <MusicBox.h>
#endif
#define STARTPIN 16
#define FAILPIN 17
#define SUCCESSPIN 18
// Type of game, set by ChooseBuzzWireGame.ino . Never "NullGame" after setup.
GameType CurrentGameType=NullGame;
// Our State Machine s State
State CurrentState=Notified;
LiquidCrystal Lcd(8,9,4,5,6,7);
#ifndef SILENT
#define MUSIC_PIN 15
MusicBox MusicPlayer(MUSIC_PIN);
#endif
// Millis at start of game
unsigned long StartTime = 0;
// Millis since start of game
int ElapseTime = 0;
// Seconds next game should last (always the same for Challenge,
// time of last success for Tournament, -1 for Free)
int TimerInitSec = -1;
// Seconds remaining in this game
int ChallengeTime = -1;
// Has the time expired yet?
boolean TimerExpired=false;
// Has start song been played on start?
boolean Restarted = false;
#ifdef CHS_DEBUG
void printCurrentState(int nlflag)
{
char *CurrentStateNames[] = {"Started","Failed","Succeeded","Notified"};
Serial.print("CurrentState: ");
Serial.print(CurrentStateNames[CurrentState]);
Serial.print("(");
Serial.print(CurrentState);
if(nlflag)
Serial.println(")");
else
Serial.print(") ");
}
#endif
/*
* ISR for STARTPIN low
*/
void Start()
{
StartTime=millis();
ChallengeTime = TimerInitSec;
CurrentState=Started;
Restarted = true;
#ifdef CHS_DEBUG
printCurrentState(0);
Serial.print(" Started at ");
Serial.print(StartTime);
Serial.print(" ChallengeTime: ");
Serial.println(ChallengeTime);
#endif
}
/*
* ISR for FAILPIN low
*/
void Fail()
{
if(Started == CurrentState)
CurrentState=Failed;
#ifdef CHS_DEBUG
printCurrentState(0);
Serial.print(" Failed at ");
Serial.println(ElapseTime);
#endif
}
/*
* ISR for SUCCESSPIN low
*/
void Success()
{
if(Started == CurrentState)
CurrentState=Succeeded;
#ifdef CHS_DEBUG
printCurrentState(0);
Serial.print(" Succeeded at ");
Serial.print(ElapseTime);
Serial.print(" ChallengeTime:");
Serial.println(ChallengeTime);
#endif
}
/*
* Well, did I Win or Lose? Play a tune here eventually.
*/
void notifyUser(State thestate)
{
char firstLine[16];
String twostLine;
#ifdef CHS_DEBUG
Serial.print("notifyUser: ");
printCurrentState(0);
#endif
memset(firstLine,0,sizeof(firstLine));
if(Succeeded == thestate) {
strncpy(firstLine,"Hurray for You!",sizeof(firstLine)-1);
twostLine="Success ";
twostLine.concat(String(ElapseTime));
twostLine.concat(" sec");
}
else { //Failed. Should never be any other state
strncpy(firstLine,"Oops. Too Bad.",sizeof(firstLine)-1);
#ifdef CHS_DEBUG
printCurrentState(0);
Serial.print(" ChallengeTime: ");
Serial.print(ChallengeTime);
Serial.print(" ElapseTime:");
Serial.print(ElapseTime);
#endif
if(TimerExpired)
twostLine = "Too Slow!";
else
twostLine = "Touched Wire!";
}
Lcd.clear();
Lcd.write(firstLine);
Lcd.setCursor(0,1);
Lcd.write(twostLine.c_str());
Restarted = false;
#ifndef SILENT
if(Succeeded == thestate)
MusicPlayer.playATune(SUCCESSSONG);
else
MusicPlayer.playATune(FAILSONG);
#endif
}
/*
* Display time or time left on LCD
*/
void displayNewTime(int theTime,char *firstLine)
{
Lcd.clear();
Lcd.setCursor(0,0);
Lcd.write(firstLine);
Lcd.setCursor(0,1);
Lcd.write(" ");
Lcd.setCursor(0,1);
Lcd.write(String(theTime).c_str());
}
/*
* Count time down in challenge or tournament mode
*/
boolean doTimeCountDown(int elapsed_time)
{
boolean retVal=false;
if(ElapseTime < elapsed_time) {
++ElapseTime;
--ChallengeTime;
if(ChallengeTime <= 0)
retVal=true;
else
displayNewTime(ChallengeTime,"Time Left.Hurry!");
}
return retVal;
}
/*
* Do timer. Decrement for Tournament or Challenge mode, display for timed mode.
*/
boolean handleTimer(void)
{
boolean retVal = false;
int elapsed_time= (int)((millis() - StartTime)/1000);
switch(CurrentGameType)
{
case Challenge:
retVal = doTimeCountDown(elapsed_time);
break;
case Tournament:
if(-1 == TimerInitSec) {
if(ElapseTime < elapsed_time) {
++ElapseTime;
displayNewTime(ElapseTime,"Time to Beat!");
}
}
else
retVal=doTimeCountDown(elapsed_time);
default: // Free
if(ElapseTime < elapsed_time) {
++ElapseTime;
displayNewTime(ElapseTime,"Time So Far");
}
break;
}
return retVal;
}
/***********************************************************************************************/
/*
* Main Line Setup
*/
void setup()
{
#ifdef CHS_DEBUG
Serial.begin(9600);
#endif
Lcd.begin(16,2);
pinMode(STARTPIN,INPUT_PULLUP);
pinMode(FAILPIN,INPUT_PULLUP);
pinMode(SUCCESSPIN,INPUT_PULLUP);
enableInterrupt(STARTPIN,Start,FALLING);
enableInterrupt(FAILPIN,Fail,FALLING);
enableInterrupt(SUCCESSPIN,Success,FALLING);
CurrentGameType=chooseGameType();
#ifdef CHS_DEBUG
Serial.print("CurrentGameType: ");
Serial.print(CurrentGameType);
Serial.print(" TimerInitSec: ");
Serial.println(TimerInitSec);
#endif
#ifndef SILENT
MusicPlayer.loadATune(StartSong,STARTSONG);
MusicPlayer.loadATune(FailSong,FAILSONG);
MusicPlayer.loadATune(SuccessSong,SUCCESSSONG);
#endif
}
/*
* Main Line Loop
*/
void loop()
{
/* BuzzWire state machine:
* Started -- game in progress
* Succeeded -- user has won
* Failed -- user has lost
* Notified -- music has played, results are displayed, and we are ready for a new game.
*/
if((Started == CurrentState) && (TimerExpired))
CurrentState = Failed;
switch(CurrentState)
{
case Started:
if(Restarted) {
ElapseTime = 0;
#ifndef SILENT
MusicPlayer.playATune(STARTSONG);
#endif
Restarted=false;
}
// Have we timed out for Challenge or Tournament yet? Else just display the time.
TimerExpired = handleTimer();
break;
case Succeeded:
#ifdef CHS_DEBUG
Serial.println("Succeeded");
#endif
if(Tournament == CurrentGameType)
TimerInitSec = ElapseTime;
ChallengeTime = TimerInitSec;
notifyUser(CurrentState);
TimerExpired=false;
ElapseTime = 0;
CurrentState = Notified;
break;
case Failed:
ChallengeTime = TimerInitSec;
notifyUser(CurrentState);
TimerExpired=false;
ElapseTime = 0;
CurrentState = Notified;
break;
default: //Notified -- wait for start of new game.
break;
}
}