-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharduino-scoreboard.ino
319 lines (266 loc) · 7.83 KB
/
arduino-scoreboard.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
// Use the https://github.com/bhagman/Tone library to play songs
// using a passive buzzer.
#include <Tone.h>
#include <CountUpDownTimer.h>
// BUZZER
#define BUZZER_PIN 12
// Red button
#define BUTTON_PLAYER1 A4
// Yellow button
#define BUTTON_PLAYER2 A5
// Player leds size.
#define LEDS_SIZE 5
// Variables for song play.
#define OCTAVE_OFFSET 0
// Define screen type
#define SCREEN_TYPE 2
// Timer type.
CountUpDownTimer T(UP, HIGH);
// Notes array required by Tone library.
int notes[] = { 0,
NOTE_C4, NOTE_CS4, NOTE_D4, NOTE_DS4, NOTE_E4, NOTE_F4, NOTE_FS4, NOTE_G4, NOTE_GS4, NOTE_A4, NOTE_AS4, NOTE_B4,
NOTE_C5, NOTE_CS5, NOTE_D5, NOTE_DS5, NOTE_E5, NOTE_F5, NOTE_FS5, NOTE_G5, NOTE_GS5, NOTE_A5, NOTE_AS5, NOTE_B5,
NOTE_C6, NOTE_CS6, NOTE_D6, NOTE_DS6, NOTE_E6, NOTE_F6, NOTE_FS6, NOTE_G6, NOTE_GS6, NOTE_A6, NOTE_AS6, NOTE_B6,
NOTE_C7, NOTE_CS7, NOTE_D7, NOTE_DS7, NOTE_E7, NOTE_F7, NOTE_FS7, NOTE_G7, NOTE_GS7, NOTE_A7, NOTE_AS7, NOTE_B7
};
// Available songs to play when goal or win events occurs.
char *songs[] = {
(char*)"smb:d=4,o=5,b=100:16e6,16e6,32p,8e6,16c6,8e6,8g6,8p,8g,8p,8c6,16p,8g,16p,8e,16p,8a,8b,16a#,8a,16g.,16e6,16g6",
(char*)"The Simpsons:d=4,o=5,b=160:c.6,e6,f#6,8a6,g.6,e6,c6,8a,8f#,8f#,8f#,2g,8p,8p,8f#,8f#,8f#,8g,a#.,8c6,8c6,8c6,c6",
(char*)"Smurfs:d=32,o=5,b=200:4c#6,16p,4f#6,p,16c#6,p,8d#6,p,8b,p,4g#,16p,4c#6,p,16a#,p,8f#,p,8a#,p,4g#,4p,g#,p,a#,p,b,p,c6,p,4c#6,16p,4f#6,p,16c#6,p,8d#6,p,8b,p,4g#,16p,4c#6,p,16a#,p,8b,p,8f,p,4f#",
};
// Variables to control state of button1 state and player1 score.
int button1_status = 0;
int button1_last_status = 0;
int player1_counter = 0;
int player1_leds[LEDS_SIZE] = {2, 3, 4, 5, 6};
// Variables to control state of button2 state and player2 score.
int button2_status = 0;
int button2_last_status = 0;
int player2_counter = 0;
int player2_leds[LEDS_SIZE] = {7, 8, 9, 10, 11};
// Winer player record, when game is finished should be 1 or 2.
int winner_player = 0;
// Unsigned longs because the time is milliseconds.
unsigned long button1_debounce_time = 0;
unsigned long button2_debounce_time = 0;
// The debounce time to determine button activation.
unsigned int debounce_delay = 50;
int reading1;
int reading2;
// Initialize Tone object.
Tone tone1;
void setup() {
pinMode(BUTTON_PLAYER1, INPUT);
pinMode(BUTTON_PLAYER2, INPUT);
// Initialize buzzer to Tone.
tone1.begin(BUZZER_PIN);
// Initialize player1 leds pins as outputs.
for (int i = 0; i < LEDS_SIZE; i++) {
pinMode(player1_leds[i], OUTPUT);
pinMode(player2_leds[i], OUTPUT);
}
T.StartTimer();
init_screen();
}
void loop() {
T.Timer();
// Update timer display.
if (T.TimeHasChanged()) {
screen_print_timer(T.ShowTotalSeconds());
}
int reading1 = digitalRead(BUTTON_PLAYER1);
int reading2 = digitalRead(BUTTON_PLAYER2);
// Reset the debouncing timer at any state change.
if (button1_status != button1_last_status) {
button1_debounce_time = millis();
}
if (button2_status != button2_last_status) {
button2_debounce_time = millis();
}
// Current state longer than the debounce so take as the actual state.
if ((millis() - button1_debounce_time) > debounce_delay) {
button1_status = reading1;
// If the button state has changed to pressed.
if (button1_status != button1_last_status && button1_status == HIGH) {
// Ensure do not exceed player leds size.
if (player1_counter < LEDS_SIZE && winner_player == 0) {
player1_counter++;
// Mark current player score on leds.
for (int i = 0; i < player1_counter; i++) {
digitalWrite(player1_leds[i], HIGH);
}
}
evaluate_winner();
if (winner_player == 0) {
// Play goal song.
play_rtttl(songs[0]);
}
}
}
// Current state longer than the debounce so take as the actual state.
if ((millis() - button2_debounce_time) > debounce_delay) {
button2_status = reading2;
// If the button state has changed to pressed.
if (button2_status != button2_last_status && button2_status == HIGH) {
// Ensure do not exceed player leds size.
if (player2_counter < LEDS_SIZE && winner_player == 0) {
player2_counter++;
// Mark current player score on leds.
for (int i = 0; i < player2_counter; i++) {
digitalWrite(player2_leds[i], HIGH);
}
}
evaluate_winner();
if (winner_player == 0) {
// Play goal song.
play_rtttl(songs[1]);
}
}
}
// Save the reading for next time through the loop.
button1_last_status = reading1;
button2_last_status = reading2;
}
// Evaluate if there is a winner and if that case turn off loser leds
// and plays the end game over song.
void evaluate_winner() {
int *loser_pins;
if (player1_counter == LEDS_SIZE) {
loser_pins = player2_leds;
winner_player = 1;
}
if (player2_counter == LEDS_SIZE) {
loser_pins = player1_leds;
winner_player = 2;
}
// Display current result in screen.
screen_display_result();
// Play end game sound if there is a winner.
if (winner_player != 0) {
// Turn off the loser pins.
if (loser_pins) {
for (int i = 0; i < LEDS_SIZE-1; i++) {
digitalWrite(loser_pins[i], LOW);
}
}
play_rtttl(songs[2]);
}
}
#define isdigit(n) (n >= '0' && n <= '9')
// Play sound Ring Tone Transfer Language processing
// function, copied from Tone library RTTTL example.
// https://en.wikipedia.org/wiki/Ring_Tone_Transfer_Language
void play_rtttl(char *p) {
byte default_dur = 4;
byte default_oct = 6;
int bpm = 63;
int num;
long wholenote;
long duration;
byte note;
byte scale;
while(*p != ':') p++; // ignore name
p++; // skip ':'
// get default duration
if(*p == 'd') {
p++; p++; // skip "d="
num = 0;
while(isdigit(*p)) {
num = (num * 10) + (*p++ - '0');
}
if(num > 0) default_dur = num;
p++; // skip comma
}
// get default octave
if(*p == 'o') {
p++; p++; // skip "o="
num = *p++ - '0';
if(num >= 3 && num <=7) default_oct = num;
p++; // skip comma
}
// get BPM
if(*p == 'b') {
p++; p++; // skip "b="
num = 0;
while(isdigit(*p)) {
num = (num * 10) + (*p++ - '0');
}
bpm = num;
p++; // skip colon
}
// BPM usually expresses the number of quarter notes per minute
// this is the time for whole note (in milliseconds)
wholenote = (60 * 1000L / bpm) * 4;
// now begin note loop
while(*p) {
// first, get note duration, if available
num = 0;
while(isdigit(*p)) {
num = (num * 10) + (*p++ - '0');
}
// we will need to check if we are a dotted note after
if(num) duration = wholenote / num;
else duration = wholenote / default_dur;
// now get the note
note = 0;
switch(*p) {
case 'c':
note = 1;
break;
case 'd':
note = 3;
break;
case 'e':
note = 5;
break;
case 'f':
note = 6;
break;
case 'g':
note = 8;
break;
case 'a':
note = 10;
break;
case 'b':
note = 12;
break;
case 'p':
default:
note = 0;
}
p++;
// now, get optional '#' sharp
if(*p == '#') {
note++;
p++;
}
// now, get optional '.' dotted note
if(*p == '.') {
duration += duration/2;
p++;
}
// now, get scale
if(isdigit(*p)) {
scale = *p - '0';
p++;
}
else {
scale = default_oct;
}
scale += OCTAVE_OFFSET;
if(*p == ',')
// skip comma for next note (or we may be at the end)
p++;
// now play the note
if(note) {
tone1.play(notes[(scale - 4) * 12 + note]);
delay(duration);
tone1.stop();
}
else {
delay(duration);
}
}
}