-
Notifications
You must be signed in to change notification settings - Fork 0
/
LED_Matrix_V0.4.ino
486 lines (389 loc) · 12.7 KB
/
LED_Matrix_V0.4.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
#define DS_pin1 7 //for columns (anode)
#define SH_CP_pin1 5
#define ST_CP_pin1 6
#define DS_pin2 4 //for rows (cathode)
#define SH_CP_pin2 13
#define ST_CP_pin2 3
#define Vx A0 //Joystick
#define Vy A1
#define Sw A2
// Global Constant
// Global constants for BLOCK BREAKER
int x=8, y=14, right=1, up=1;
unsigned int a[16] = {0x8000, 0x4000, 0x2000, 0x1000, 0x0800, 0x0400, 0x0200, 0x0100, 0x0080, 0x0040, 0x0020, 0x0010, 0x0008, 0x0004, 0x0002, 0x0001};
//values for a single column at a time, starting from the leftmost column to the rightmost column
// Don't edit this function, working properly
void shift_resistor_receive_data(int data1, int data2){
int i=0;
for (i = 0; i < 16; i++){
if ( data1 & 0b00000000000000001){
digitalWrite(DS_pin1, HIGH);
}
else{
digitalWrite(DS_pin1, LOW);
}
data1 = data1 >> 1;
digitalWrite(SH_CP_pin1, HIGH);
digitalWrite(SH_CP_pin1, LOW);
}
digitalWrite(ST_CP_pin1, HIGH);
digitalWrite(ST_CP_pin1, LOW);
for (i = 0; i < 16; i++){
if ( data2 & 0b1000000000000000){
digitalWrite(DS_pin2, HIGH);
}
else{
digitalWrite(DS_pin2, LOW);
}
data2 = data2 << 1;
digitalWrite(SH_CP_pin2, HIGH);
digitalWrite(SH_CP_pin2, LOW);
}
digitalWrite(ST_CP_pin2, HIGH);
digitalWrite(ST_CP_pin2, LOW);
}
// Don't edit this function, working properly
void print_array(bool shape[16][16]){
int i=0;
for (int j = 0; j < 16; j++){
unsigned int value = 0;
unsigned int temp = 1;
for (i = 0; i < 16; i++){
if (i != 0)
temp = temp * 2;
value = value + int(shape[i][j]) * temp;
}
shift_resistor_receive_data(a[j], value); //constantly column is moving right and column wise data of the row is getting printed
delayMicroseconds(100); //a[j] value is for one column, and value data from the 2d array representing a single LED
shift_resistor_receive_data(0x0000, 0xFFFF); //clears whole matrix before the next data is displayed on the screen
}
}
// Can be used for left shifting of 16x16 matrix
// Don't edit this function, working properly
void shift_array(bool shape[16][16]){
int i=0;
int j;
for(i=0; i<16; i++){
bool temp=shape[i][0];
for(j=0; j<16; j++){
shape[i][j]=shape[i][j+1];
}
shape[i][15]=temp;
}
}
void setup() {
Serial.begin(9600);
pinMode(DS_pin1, OUTPUT);
pinMode(SH_CP_pin1, OUTPUT);
pinMode(ST_CP_pin1, OUTPUT);
pinMode(DS_pin2, OUTPUT);
pinMode(SH_CP_pin2, OUTPUT);
pinMode(ST_CP_pin2, OUTPUT);
pinMode(Vx, INPUT);
pinMode(Vy, INPUT);
pinMode(Sw, INPUT);
shift_resistor_receive_data(0xFFFF, 0x0000); //lights all the LEDs to check which ones are defective ones
delay(100);
}
bool LED_Matrix[16][16]={
{1, 1, 1, 0, 1, 1, 1, 1, 0, 0, 1, 0, 0, 1, 1, 1},
{1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 1},
{1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 1, 0, 1, 0, 1, 1},
{1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 1},
{1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{0, 1, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 1},
{0, 0, 0, 1, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1},
{0, 1, 0, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 1, 1},
{0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1},
{0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1},
{1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1},
{1, 1, 1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, 0, 0, 1, 0, 0, 1, 1},
{1, 1, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 1},
{1, 1, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 1},
{1, 1, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 1},
{1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 1, 1},
{1, 1, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1},
{1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 1},
{1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 1},
{1, 1, 0, 0, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 1, 1},
{1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1},
{1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 1},
{1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 1},
{1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 1, 1}
};
// holds the screen till the user pushes the center button
// Don't edit this function, working properly
void waitForPush(){
int s = 100, count = 5, switchValue=0;
while(count--){
s = analogRead(Sw);
switchValue += s;
}
switchValue /= 5;
while(switchValue){
count = 5;
print_array(push);
while(count--){
s = analogRead(Sw);
switchValue += s;
}
switchValue /= 5;
}
}
bool gameOver[16][16]={
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 0, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 0, 1},
{0, 1, 1, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 1, 1},
{0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1},
{0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1},
{1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 0, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 0, 1, 0, 0, 1},
{1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0},
{1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1},
{1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0},
{1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 0, 1, 0},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
};
//#######################################################################################
// GAME :: BLOCK BREAKER
// Block Breaker Game Program Starts here
bool homeGame1[16][16]={
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0},
{0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0},
{0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0},
{0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0},
{0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0},
{0, 1, 1, 0, 0, 0, 1, 0, 0, 0,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 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1}
};
// draw paddle position from analog input for the game
// Don't change this function working fine
void paddlePosition(){
//Right = 0
//Left = 1023
int i=0;
int sensorValue = analogRead(A0);
if (sensorValue > 852) // Extreme Left
for(i=0; i<16; i++){
if(i>=0 && i < 5)
game1[15][i] = 0;
else
game1[15][i] = 1;
}
else if (sensorValue > 652 && sensorValue < 853)
for(i=0; i<16; i++){
if(i>1 && i < 7)
game1[15][i] = 0;
else
game1[15][i] = 1;
}
else if (sensorValue > 512)
for(i=0; i<16; i++){
if(i>3 && i < 9)
game1[15][i] = 0;
else
game1[15][i] = 1;
}
else if (sensorValue < 512 && sensorValue > 342)
for(i=0; i<16; i++){
if(i>5 && i < 11)
game1[15][i] = 0;
else
game1[15][i] = 1;
}
else if (sensorValue < 342 && sensorValue > 172)
for(i=0; i<16; i++){
if(i>8 && i < 14)
game1[15][i] = 0;
else
game1[15][i] = 1;
}
else if (sensorValue < 172) // Extreme Right
for(i=0; i<16; i++){
if(i>10 && i <= 15)
game1[15][i] = 0;
else
game1[15][i] = 1;
}
else
for(i=0; i<16; i++){
if(i>5 && i < 11)
game1[15][i] = 0;
else
game1[15][i] = 1;
}
}
// move ball
void moveBall(){
game1[y][x]=1;
// check wall collisions and reverse
x -= right;
y -= up;
if(game1[y][x]==0)
{
up =- up;
}
else if(game1[y][x]==0 && game1[y+1][x]==0 && game1[y][x-1]==0)
{
game1[y+1][x]=1;
game1[y][x-1]=1;
up =- up;
}
if(game1[15][x]==0 && y == 14)
{
up =- up;
}
if(x == 0 || x == 15)
right = -right;
if(y == 0 )
up = -up;
game1[y][x]=0;
// // check block or paddle collisions, delete block and reverse
// if(game1[y+up][x] == 0){
// game1[15-x][15-y-up] = 1;
// up = -up;
// }
// if (game1[y][x+right] == 0) {
// game1[15-x-right][15-y] = 1;
// right = -right;
// }
// if (game1[y+up][x+right] == 0) {
// game1[15-x-right][15-y-up] = 1;
// right = -right;
// up = -up;
//
// // more potential for collisions after diagnal bounce so check again
// if (x == 0 || x == 15) {
// right = -right;
// }
// if (y == 0 || y == 15) {
// up = -up;
// }
// if (game1[y+up][x+right] == 0) {
// game1[15-x-right][15-y-up] = 1;
// up = -up;
// right = -right;
// }
//
//
//
// }
// set new posistion
// y += up;
// x += right;
//
//
// // draw new ball posistion
// game1[y][x] = 0;
}
void startGame1(){
int i=0;
bool endGame1 = false;
for(i=0; i<80; i++){
print_array(homeGame1);
}
//waitForPush();
game1[y][x] = 0; //starting position of ball
while(!endGame1){
for(i=0; i<15; i++){
print_array(game1);
paddlePosition();
}
moveBall();
if(y==15)
endGame1 = true;
}
for(i=0; i<50; i++){
print_array(gameOver);
}
}
// Block Breaker Game Program Finishes here
//#######################################################################################
//#######################################################################################
// GAME :: SNAKE GAME
// Snake Game Program Starts here
void startGame2(){
}
// Snake Game Game Program Finishes here
//#######################################################################################
// Menu asking user which game to play
void startGame(){
int i=0;
for(i=0; i<80; i++){
print_array(LED_Matrix);
}
int option = analogRead(Vy);
Serial.println(option);
while(option > 400 && option < 600){
option = analogRead(Vy);
print_array(menu);
Serial.println(option);
}
if(option < 400)
startGame1();
if(option > 600)
startGame2();
}
void loop() {
print_array(push);
startGame();
}