-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLedPong.ino
474 lines (364 loc) · 9.1 KB
/
LedPong.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
#include <Modulino.h>
#include <Scheduler.h>
#include <Arduino_LED_Matrix.h>
#include "crash.h"
#include "pong_start.h"
#include "winner.h"
//#define DEBUG 1
/* Features */
#define BUZZER 1
#define ANIMATION 1
#define LEDS 1
//#define BACKGROUND_LEDS 1
/* Defines */
#define SECONDARY_ENCODER_ADDRESS 0x08
#define SECONDARY_PIXELS_ADDRESS 0x22
#define WIDTH 12
#define HEIGHT 8
#define START_Y 3
#define BAR_WIDTH 3
#define BALL_SPEED 120
#define LEFT_WIN 8
#define RIGHT_WIN 0
#define MATCH_TO_WIN 5
#define LED_BRI 8
#define WINNER_ANIM_LEFT 0
#define WINNER_ANIM_RIGHT 2
/* Modes */
#define DIR_TOP -1
#define DIR_BOTTOM 1
#define DIR_BALANCE 0
#define DIR_LEFT -1
#define DIR_RIGHT 1
/* Objects */
ArduinoLEDMatrix matrix;
ModulinoKnob sxEncoder, dxEncoder(SECONDARY_ENCODER_ADDRESS);
ModulinoBuzzer buzzer;
ModulinoPixels leds, ledsBackground(SECONDARY_PIXELS_ADDRESS);
/* Colors */
ModulinoColor leftColor(0xff, 0x00, 0x00), rightColor(0x0e, 0x1d, 0xf0);
/*Players positions*/
int8_t psx_x = 0, psx_y = START_Y, pdx_x = WIDTH-1, pdx_y = START_Y;
/*Ball positions and directions */
int8_t ball_x = WIDTH/2, ball_y = HEIGHT/2, dir_x = DIR_LEFT, dir_y = DIR_BALANCE;
/* Status variables */
int8_t game_ongoing = 0, winner = -1, muted = 1;
/* Match Variables */
int8_t win_left=0, win_right=0;
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();
dxEncoder.begin();
dxEncoder.set(START_Y);
sxEncoder.begin();
sxEncoder.set(START_Y);
#ifdef BUZZER
buzzer.begin();
pinMode(12, INPUT_PULLUP);
digitalWrite(12, HIGH);
#endif
#ifdef LEDS
leds.begin();
for(uint8_t i = 0; i<8; i++){
leds.set(i, 0, 0, 0, LED_BRI);
}
leds.show();
#endif
#ifdef BACKGROUND_LEDS
randomSeed(analogRead(0));
ledsBackground.begin();
Scheduler.startLoop(flickerBackground);
ledsBackground.show();
#endif
#ifdef ANIMATION
for(auto f:pong_start){
matrix.loadFrame(f);
delay(90);
}
#endif
/*Ball start position */
frame[ball_y][ball_x] = 1;
Scheduler.startLoop(playerDXLoop);
Scheduler.startLoop(playerSXLoop);
game_ongoing = 1;
#ifdef DEBUG
Serial.println("Setup Completed");
#endif
}
void loop() {
if(game_ongoing){
#ifdef DEBUG
Serial.println("Game Ongoing");
#endif
// Clear old ball position
frame[ball_y][ball_x] = 0;
// Move the ball
ball_y += dir_y;
ball_x += dir_x;
// Check borders
if(ball_x < 0){ball_x = 0;}
else if(ball_x > WIDTH -1){ball_x = WIDTH - 1;}
if(ball_y < 0){ball_y = 0;}
else if(ball_y > HEIGHT-1){ball_y = HEIGHT-1;}
#ifdef DEBUG
Serial.print("Rendering Bitmap with ball position (x,y): ");
Serial.print(ball_x);
Serial.print(" , ");
Serial.println(ball_y);
#endif
// Draw matrix
frame[ball_y][ball_x] = 1;
matrix.renderBitmap(frame, 8, 12);
// Check Crashes
if(ball_x == 0 || (ball_x == 1 && dir_x == DIR_LEFT)){
//Check if gol or change dir
#ifdef DEBUG
Serial.println("[LEFT] Checking if gol or change dir");
#endif
if(ball_y >= psx_y && ball_y <= psx_y + BAR_WIDTH -1){
#ifdef DEBUG
Serial.println("[LEFT] Player Touched - Changing direction");
#endif
dir_x = -dir_x;
if(ball_y == psx_y){
dir_y = DIR_BOTTOM;
} else if(ball_y == psx_y + BAR_WIDTH -1){
dir_y = DIR_TOP;
} else {
dir_y = DIR_BALANCE;
}
playSound();
} else if(ball_x == 0){
#ifdef DEBUG
Serial.println("[LEFT] GOL");
#endif
game_ongoing = 0;
winner = RIGHT_WIN;
resetVariables();
}
} else if (ball_x == WIDTH -1 || (ball_x == WIDTH -2 && dir_x == DIR_RIGHT)){
//Check if gol or change dir
#ifdef DEBUG
Serial.println("[RIGHT] Checking if gol or change dir");
#endif
if(ball_y >= pdx_y && ball_y <= pdx_y + BAR_WIDTH -1){
#ifdef DEBUG
Serial.println("[RIGHT] Player Touched - Changing direction");
#endif
dir_x = -dir_x;
if(ball_y == pdx_y){
dir_y = DIR_BOTTOM;
} else if(ball_y == pdx_y + BAR_WIDTH -1){
dir_y = DIR_TOP;
} else {
dir_y = DIR_BALANCE;
}
playSound();
} else if (ball_x == WIDTH -1){
#ifdef DEBUG
Serial.println("[RIGHT] GOL");
#endif
game_ongoing = 0;
winner = LEFT_WIN;
resetVariables();
}
} else if(ball_y == 0 || ball_y == HEIGHT-1){
#ifdef DEBUG
Serial.println("Tentative Changing Y direction");
#endif
//Upper or lower border, change direction (not all the time otherwise it is a stalemate)
int8_t rnd = random(100);
if(rnd%2 == 0){
#ifdef DEBUG
Serial.println("Changing Y direction");
#endif
dir_y = -dir_y;
}
}
delay(BALL_SPEED);
}
delay(1);
}
/* Loop for the player on the DX */
void playerDXLoop(){
if(game_ongoing){
updateBar(pdx_x, pdx_y, 0);
handleEncoder(dxEncoder, &pdx_y);
updateBar(pdx_x, pdx_y, 1);
matrix.renderBitmap(frame, 8, 12);
if(pdx_y != START_Y){ muted = 0; }
} else {
dxEncoder.set(pdx_y);
}
delay(1);
}
/* Loop for the player on the SX */
void playerSXLoop(){
if(game_ongoing){
updateBar(psx_x, psx_y, 0);
handleEncoder(sxEncoder, &psx_y);
updateBar(psx_x, psx_y, 1);
matrix.renderBitmap(frame, 8, 12);
if(psx_y != START_Y){ muted = 0; }
} else {
sxEncoder.set(psx_y);
}
delay(1);
}
/* Handle rotary movement and update pos_y */
void handleEncoder(ModulinoKnob k, int8_t *pos_y){
int value = k.get();
if(value < 0) {k.set(0);}
if(value >= HEIGHT - BAR_WIDTH) {k.set((HEIGHT - BAR_WIDTH));}
value = k.get();
*pos_y = value;
}
/* Update pos_y in matrix */
void updateBar(int8_t x, int8_t start_y, byte state){
for(uint8_t k = 0; k<BAR_WIDTH; k++){
frame[start_y+k][x] = state;
}
}
/* Reset variables */
void resetVariables(){
/* Play Animation */
#ifdef ANIMATION
playEndAnimation(winner);
#endif
handleMatch(winner);
/* Reset Matrix */
memset(frame, 0, WIDTH * HEIGHT * sizeof(byte));
/* Reset Status */
ball_x = WIDTH/2;
ball_y = HEIGHT/2;
dir_x = DIR_LEFT;
dir_y = DIR_BALANCE;
psx_x = 0;
psx_y = START_Y;
pdx_x = WIDTH-1;
pdx_y = START_Y;
winner = -1;
frame[ball_y][ball_x] = 1;
dxEncoder.set(START_Y);
sxEncoder.set(START_Y);
delay(1500);
muted = 1;
game_ongoing = 1;
}
/* Add point to winner*/
void handleMatch(int8_t winner){
switch(winner){
case LEFT_WIN:
win_left++;
#ifdef LEDS
leds.set(win_left-1, leftColor, LED_BRI);
#endif
break;
case RIGHT_WIN:
win_right++;
#ifdef LEDS
leds.set(8-win_right, rightColor, LED_BRI);
#endif
break;
}
#ifdef LEDS
leds.show();
#endif
if(win_right == MATCH_TO_WIN || win_left == MATCH_TO_WIN){
showVictoryAnimation(winner);
win_right = 0;
win_left = 0;
}
}
/* Animate pixels for the winner player */
void showVictoryAnimation(int8_t winner) {
ModulinoColor *winColor;
uint8_t winnerAnim;
switch(winner){
case LEFT_WIN:
winColor = &leftColor;
winnerAnim = WINNER_ANIM_LEFT;
break;
case RIGHT_WIN:
winColor = &rightColor;
winnerAnim = WINNER_ANIM_RIGHT;
break;
}
for(uint8_t j = 0; j<6; j++){
#ifdef LEDS
for(uint8_t i = 0; i<8; i++){
leds.set(i, *winColor, LED_BRI);
}
leds.show();
#endif
#ifdef ANIMATION
matrix.loadFrame(winner_animation[winnerAnim]);
#endif
delay(250);
#ifdef LEDS
for(uint8_t i = 0; i<8; i++){
leds.set(i, 0, 0, 0, LED_BRI);
}
leds.show();
#endif
#ifdef ANIMATION
matrix.loadFrame(winner_animation[winnerAnim+1]);
#endif
delay(250);
}
}
/* Play sound */
void playSound(){
#ifdef BUZZER
if(muted){return;}
if(digitalRead(12) == LOW){
return;
}
buzzer.tone(300+25*ball_x, 200);
#endif
}
#ifdef ANIMATION
/* Show end game animation */
void playEndAnimation(int8_t start){
for(int8_t i = start; i<start+7; i++){
#ifdef BUZZER
buzzer.tone(250+25*(i-start), 150);
#endif
matrix.loadFrame(crash[i]);
delay(150);
}
}
#endif
#ifdef BACKGROUND_LEDS
#define FLICKER_TIME 150
#define FLICKER_MAX 180
#define FLICKER_DIFF 30
void flickerBackground(){
for(uint8_t i = 0; i<4; i++){
uint8_t rand = random(FLICKER_DIFF);
ledsBackground.set(i, rightColor, FLICKER_MAX-rand);
delay(1);
}
for(uint8_t i = 4; i<8; i++){
uint8_t rand = random(FLICKER_DIFF);
ledsBackground.set(i, leftColor, FLICKER_MAX-rand);
delay(1);
}
ledsBackground.show();
delay(FLICKER_TIME);
}
#endif