-
Notifications
You must be signed in to change notification settings - Fork 0
/
FlappyLED.ino
570 lines (460 loc) · 11.3 KB
/
FlappyLED.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
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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
#include "ArduinoGraphics.h"
#include "Arduino_LED_Matrix.h"
#include "Modulino.h"
#include <Scheduler.h>
#include "flappy_animation.h"
#include "crash_animation.h"
#include "modes_animation.h"
#include "intro_animation.h"
//#define DEBUG 1 //Enable debug prints in Serial Monitor : 115200
/* Defines */
#define HEIGHT 8
#define WIDTH 12
#define PLAYER_X 1
#define PLAYER_Y 4
#define RESET_TIME_MS 1800000
/* TOF Configuration */
#define MAX_TOF_H 500
#define MIN_TOF_H 30
#define TOF_TRESHOLD 35
#define TOF_PRESENCE_TIME 2000
/* Enable Features */
/* Disable the define to disable the related feature*/
#define BUTTONS //Modulino button needed: select the game mode and toggle mute.
#define ENCODER_MODE //Modulino Knob needed: rotate to move the led while playing
#define TOF_MODE //Modulino Distance needed: move the hand in front of the sensor to move the led while playing
#define BUZZER //Modulino Buzzer needed: play sounds while playing
#define ANIMATIONS //Enable Animations
#define RESET_TIME //Automatically reset after some time
/* Game Modes */
/* It is the state of the game*/
#define MENU 0 //No Game mode selected
#define ENCODER 1 //Playing with Encoder
#define TOF 2 //Playing with TOF
/* Matrix & Modulinos */
ArduinoLEDMatrix matrix;
ModulinoKnob encoder;
ModulinoDistance distanceSensor;
ModulinoButtons buttons;
ModulinoBuzzer buzzer;
/* Variables */
int game_ongoing = 0;
unsigned long tofOldDistanceTime = 0;
static byte wall_move = false;
static int8_t player_move = 0;
static uint8_t player_x = PLAYER_X, player_y = PLAYER_Y;
static int8_t holeStartX = 0, wall_pos_x = WIDTH-1, holeSize = 3;
static uint16_t score = 0, time_to_wait = 70;
static uint8_t gameMode = MENU;
static float tofOldDistance = 0;
static bool mute = false;
byte frame[8][12] = {
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
};
void setup() {
// put your setup code here, to run once:
#ifdef DEBUG
Serial.begin(115200);
#endif
matrix.begin();
Modulino.begin();
welcomeMessage();
#ifdef ENCODER_MODE
encoder.begin();
encoder.set(PLAYER_Y);
Scheduler.startLoop(playerLoop);
#endif
#ifdef TOF_MODE
distanceSensor.begin();
Scheduler.startLoop(distanceLoop);
#endif
#ifdef BUZZER
buzzer.begin();
pinMode(12, INPUT_PULLUP);
digitalWrite(12, HIGH);
/*Start with MUTE status instead of Unmuted*/
/*In order to start with mute = true, it is wnough to add a Jumper between GND and PIN 12*/
if(!digitalRead(12)){
mute = true;
}
#endif
#ifdef BUTTONS
buttons.begin();
buttons.setLeds(false, mute, false);
Scheduler.startLoop(menuLoop);
#endif
#if !defined(TOF_MODE) && !defined(ENCODER_MODE)
game_ongoing = true;
gameMode = 127; //dummy value
digitalWrite(LED_BUILTIN, HIGH);
#endif
}
void loop() {
while(gameMode == MENU){
#ifdef DEBUG
Serial.println("Waiting for game mode");
#endif
#ifdef ANIMATIONS
showIdleAnimation(true);
#endif
#ifdef RESET_TIME
if(millis() > RESET_TIME_MS){
NVIC_SystemReset();
}
#endif
delay(200);
}
if (game_ongoing) {
if(score == 0){
delay(800);
}
/* Remove the wall from the matrix */
for (uint8_t y = 0; y < HEIGHT; y++) {
frame[y][0] = 0;
frame[y][1] = 0;
frame[y][2] = 0;
}
/* Set the Hole position */
holeStartX = random(0, HEIGHT-holeSize-1);
/* Move the wall from right to left */
do {
if (wall_move) {
for (uint8_t y = 0; y < HEIGHT; y++) {
frame[y][wall_pos_x] = (y >= holeStartX && y < holeStartX + holeSize) ? 0 : 1;
if (wall_pos_x < WIDTH - 3) {
frame[y][wall_pos_x + 3] = 0;
}
}
wall_pos_x--;
}
wall_move = !wall_move;
/* Update Matrix */
matrix.renderBitmap(frame, HEIGHT, WIDTH);
/* Adapt level */
adaptLevel();
#if defined(TOF_MODE) || defined(ENCODER_MODE)
/* Check if player touch the wall */
if (frame[player_y][player_x] == frame[player_y][player_x - 1]) {
resetGameState();
return;
}
#endif
} while (wall_pos_x >= 0);
/* Increment score counter */
score++;
#ifdef BUZZER
playWallSound();
#endif
/* Reset wall position */
wall_pos_x = WIDTH-1;
}
delay(1);
}
void menuLoop () {
buttons.update();
if(buttons.isPressed(1)){
mute = !mute;
switch(gameMode){
case MENU:
buttons.setLeds(false, mute, false);
break;
case ENCODER:
buttons.setLeds(true, mute, false);
break;
case TOF:
buttons.setLeds(false, mute, true);
break;
}
delay(300);
}
if(gameMode != MENU){
#ifdef DEBUG
Serial.println("mode selected, buttons disabled");
#endif
delay(1);
return;
}
if(buttons.isPressed(0)){
gameMode = ENCODER;
buttons.setLeds(true, mute, false);
}
if (buttons.isPressed(2)){
gameMode = TOF;
buttons.setLeds(false, mute, true);
}
delay(1);
}
void playerLoop() {
while(gameMode != ENCODER){
#ifdef DEBUG
Serial.println("Waiting for ENCODER");
#endif
if(game_ongoing == 1){
delay(500);
continue;
}
#if !defined(BUTTONS)
if(encoder.get() != PLAYER_Y){
gameMode = ENCODER;
}
#endif
delay(1);
}
game_ongoing = 1;
frame[player_y][player_x] = 0;
int value = encoder.get();
if(value < 0) {encoder.set(0);}
if(value >= HEIGHT-1) {encoder.set((HEIGHT-1));}
value = encoder.get();
player_y = value;
frame[player_y][player_x] = 1;
matrix.renderBitmap(frame, 8, 12);
delay(1);
}
void distanceLoop() {
while(gameMode != TOF){
#ifdef DEBUG
Serial.println("Waiting for TOF");
#endif
if(game_ongoing == 1){
delay(500);
continue;
}
#if !defined(BUTTONS)
if(!distanceSensor.available()){
delay(1);
continue;
}
float d = distanceSensor.get();
unsigned long m = millis();
if(!isnan(d) && d < MAX_TOF_H && d > MIN_TOF_H){
#ifdef DEBUG
Serial.println("MenuMode - something in front of the sensor");
#endif
if(tofOldDistance != 0 && ((m - tofOldDistanceTime) > TOF_PRESENCE_TIME)){
#ifdef DEBUG
Serial.println("MenuMode - something in front of the sensor for 5 seconds");
#endif
gameMode = TOF;
} else if(tofOldDistance == 0) {
#ifdef DEBUG
Serial.println("MenuMode - starting to count seconds");
#endif
tofOldDistance = d;
tofOldDistanceTime = m;
}
} else {
#ifdef DEBUG
Serial.println("MenuMode - Nothing in front of the sensor");
#endif
tofOldDistance = 0;
tofOldDistanceTime = m;
}
#endif
delay(1);
}
if(!distanceSensor.available()){
delay(1);
return;
}
float distance = distanceSensor.get();
if(isnan(distance)){
#ifdef DEBUG
Serial.println("Distance is NAN");
#endif
delay(1);
return;
}
game_ongoing = 1;
#ifdef DEBUG
Serial.print("Distance: ");
Serial.println(distance);
#endif
if(distance > MAX_TOF_H || distance < MIN_TOF_H){
#ifdef DEBUG
Serial.println("Distance discarded, OUT OF RANGE!");
#endif
frame[player_y][player_x] = 1;
matrix.renderBitmap(frame, 8, 12);
delay(1);
return;
}
if(tofOldDistance == 0){
tofOldDistance = distance;
game_ongoing = 1;
delay(1);
return;
} else {
frame[player_y][player_x] = 0;
if(distance < tofOldDistance-TOF_TRESHOLD ){
tofOldDistance = distance;
if(player_y < HEIGHT-1) {
player_y++;
}
} else if(distance > tofOldDistance+TOF_TRESHOLD ){
tofOldDistance = distance;
if(player_y > 0) {
player_y--;
}
}
#ifdef DEBUG
Serial.print("Player Position Y: ");
Serial.println(player_y);
#endif
frame[player_y][player_x] = 1;
matrix.renderBitmap(frame, 8, 12);
}
delay(1);
}
/* Functions declaration ----------------------------------------------------------*/
void welcomeMessage(){
#if !defined(ANIMATIONS)
matrix.beginDraw();
matrix.stroke(0xFFFFFFFF);
matrix.textScrollSpeed(50);
// add the text
const char text[] = " Flappy LED! ";
matrix.textFont(Font_5x7);
matrix.beginText(0, 1, 0xFFFFFF);
matrix.println(text);
matrix.endText(SCROLL_LEFT);
matrix.endDraw();
#endif
#ifdef ANIMATIONS
showIdleAnimation(false);
#endif
}
#ifdef ANIMATIONS
void showIdleAnimation(bool interrupt){
for(auto i:introAnimation){
if(interrupt && game_ongoing == true){break;}
matrix.loadFrame(i);
delay(90);
}
for(auto i:flappyAnimation){
if(interrupt && game_ongoing == true){break;}
matrix.loadFrame(i);
delay(66);
}
for(auto i:crashAnimation){
if(interrupt && game_ongoing == true){break;}
matrix.loadFrame(i);
delay(66);
}
for(auto i:modesAnimation){
if(interrupt && game_ongoing == true){break;}
matrix.loadFrame(i);
delay(80);
}
}
#endif
#ifdef BUZZER
void playWallSound(){
if(!mute){
buzzer.tone(200+score*5, time_to_wait*5);
}
}
void playEndSound(uint8_t frame){
if(!mute && frame < 4){
buzzer.tone(200+100*(4-frame), 200);
delay(200);
} else {
delay(66);
}
}
#endif
/* The idea is to increese the speed or reduce the hole size every 5 points */
void adaptLevel()
{
if (score < 5) {
time_to_wait = 70;
}
else if (score >= 5 && score < 10) {
time_to_wait = 50;
}
else if (score >= 10 && score < 15) {
time_to_wait = 40;
}
else if (score >= 15 && score < 20) {
holeSize = 2;
}
else if (score >= 20 && score < 25) {
time_to_wait = 30;
}
else {
holeSize = 1;
}
delay(time_to_wait);
}
void print_score(uint16_t game_score)
{
uint8_t text_pos_x = (game_score < 10) ? 5 : 3;
matrix.clear();
matrix.beginDraw();
matrix.stroke(0xFFFFFFFF);
char text[3];
itoa(game_score, text, 10);
matrix.textFont(Font_4x6);
matrix.beginText(text_pos_x, 1, 0xFFFFFF);
matrix.println(text);
matrix.endText();
matrix.endDraw();
}
void clear_text()
{
matrix.clear();
matrix.beginDraw();
matrix.stroke(0xFFFFFFFF);
matrix.textFont(Font_4x6);
matrix.beginText(0, 1, 0xFFFFFF);
matrix.println(" ");
matrix.endText();
matrix.endDraw();
}
void resetGameState()
{
/* Reset game mode*/
gameMode = MENU;
/*Reset Matrix*/
memset(frame, 0, WIDTH * HEIGHT * sizeof(byte));
/*Reset Player position*/
player_x = PLAYER_X;
player_y = PLAYER_Y;
/*Print crash animation and sound*/
uint8_t countF = 0;
for(auto i:crashAnimation){
matrix.loadFrame(i);
#ifdef BUZZER
playEndSound(countF);
#endif
#if !defined(BUZZER)
delay(66);
#endif
countF++;
}
#ifdef BUTTONS
/*Switch buttons leds off*/
buttons.setLeds(false, mute, false);
#endif
print_score(score);
score = 0;
wall_pos_x = WIDTH-1;
holeSize = 3;
time_to_wait = 70;
/*Reset encoder value*/
encoder.set(PLAYER_Y);
/*Reset old distance for TOF sensor */
tofOldDistanceTime = 0;
tofOldDistance = 0;
/*Wait some time after finish*/
delay(3000);
clear_text();
game_ongoing = 0;
}