-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathLEDmatrix_completeV2_english.ino
522 lines (469 loc) · 14.2 KB
/
LEDmatrix_completeV2_english.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
// LEDmatrix with WS2812B RGB LEDs: complete Program
// You need Libraries from Adafruit to control the WS2812B RGB LEDs
// Video on Youtube: https://youtu.be/ykgVRxlKJrg
//
// modified 04 July 2017
// by techniccontroller
#include <Adafruit_GFX.h>
#include <Adafruit_NeoMatrix.h>
#include <Adafruit_NeoPixel.h>
#include <SoftwareSerial.h>
#include <EEPROM.h>
#define PIN 2
#define LEFT 1
#define RIGHT 2
#define EEPROM_MIN_ADDR 0
#define EEPROM_MAX_ADDR 100
#define LEN 100
#define LINE 10
#define RECT 5
// own datatype for matrix movement (snake and spiral)
enum direction {right, left, up, down};
// width of the led matrix
const int width = 6;
// height of the led matrix
const int height = 10;
// some variables relevant for the snake program
int dx = 0, dy = 1;
// default tex, when no text is in the eeprom from previous sessions
const String defaultText = "Hello world";
// temp variable for storing the displayed text
String in = defaultText;
Adafruit_NeoMatrix matrix = Adafruit_NeoMatrix(width, height, PIN,
NEO_MATRIX_TOP + NEO_MATRIX_LEFT +
NEO_MATRIX_COLUMNS + NEO_MATRIX_PROGRESSIVE,
NEO_GRB + NEO_KHZ800);
SoftwareSerial BTserial(3, 4); // RX | TX
// six predefined colors (red, yellow, purple, orange, green, blue)
const uint16_t colors[] = {
matrix.Color(255, 0, 0),
matrix.Color(200, 200, 0),
matrix.Color(255, 0, 200),
matrix.Color(255, 128, 0),
matrix.Color(0, 128, 0),
matrix.Color(0, 0, 255) };
void setup() {
matrix.begin();
matrix.setTextWrap(false);
matrix.setBrightness(40);
matrix.setTextColor(colors[0]);
randomSeed(analogRead(0));
BTserial.begin(115200);
Serial.begin(9600);
char chararray[LEN];
if(eeprom_read_string(10, chararray, LEN)) {
Serial.println(chararray);
in = chararray;
}
}
void loop() {
if(BTserial.available() > 0){
in = BTserial.readString();
char temparray[in.length()+1];
in.toCharArray(temparray, in.length()+1);
if(strstr(temparray, "new") != NULL){
in = strstr(temparray, "new")+3;
char temp[in.length()+1];
in.toCharArray(temp, in.length()+1);
eeprom_write_string(10, temp);
}
else{
in = defaultText;
char temp[in.length()+1];
in.toCharArray(temp, in.length()+1);
eeprom_write_string(10, temp);
}
}
colorgradient(LINE);
text(random(6));
colorgradient(RECT);
tetris();
spiral(false);
spiral(true);
snake(colors[random(6)]);
lines(random(6));
}
void lines(int colorID){
matrix.fillScreen(0);
for(int i = 0; i < 16; i++){
matrix.drawPixel(1, i, colors[colorID]);
matrix.drawPixel(3, i-3, colors[colorID]);
matrix.drawPixel(5, i-6, colors[colorID]);
matrix.show();
delay(250);
}
for(int i = -1; i < 30; i++){
if(i%4 != 0){
matrix.drawPixel(i+1, 1, colors[(colorID+2)%6]);
matrix.drawPixel(i-5, 3, colors[(colorID+2)%6]);
matrix.drawPixel(i-11, 5, colors[(colorID+2)%6]);
matrix.drawPixel(i-17, 7, colors[(colorID+2)%6]);
matrix.drawPixel(i-23, 9, colors[(colorID+2)%6]);
}
matrix.show();
delay(250);
}
}
void spiral(bool empty){
dx = 0, dy = 1; // Veränderung
direction dir1 = down; // aktuelle Richtung
int x = 2;
int y = 4;
if(!empty)matrix.fillScreen(0);
int counter1 = 0;
int countEdge = 1;
int countCorner = 0;
bool breiter = true;
int randNum = random(255);
for(int i = 0; i < 42; i++){
if(empty){
matrix.drawPixel(x, y, 0);
}
else{
matrix.drawPixel(x, y, Wheel((randNum +i*6)%255));
}
Serial.print(counter1);
Serial.print(countEdge);
Serial.println(countCorner);
if(countCorner == 2 && breiter){
countEdge +=1;
breiter = false;
}
if(counter1 >= countEdge){
dir1 = nextDir(dir1, LEFT);
counter1 = 0;
countCorner++;
}
if(countCorner >= 4){
countCorner = 0;
countEdge += 1;
breiter = true;
}
x += dx;
y += dy;
matrix.show();
counter1++;
delay(200);
}
}
void snake(const uint16_t color){
dx = 0, dy = 1; // Change
direction dir1 = down; // current direction
int snake1[2][3] = {{ 1,1,1}, // snake coordinates (x and y)
{ 0,1,2}};
int randomy = random(1,8); // Random variable for y-direction
int randomx = random(1,4); // Random variable for x-direction
int e = LEFT; // next turn
for(int i = 0; i < 200; i++){
matrix.fillScreen(0);
// move one step forward
snake1[0][0] = snake1[0][1];
snake1[0][1] = snake1[0][2];
snake1[0][2] = snake1[0][2]+dx;
snake1[1][0] = snake1[1][1];
snake1[1][1] = snake1[1][2];
snake1[1][2] = snake1[1][2]+dy;
// collision with wall?
if( (dir1 == down && snake1[1][2] == height-1) ||
(dir1 == up && snake1[1][2] == 0) ||
(dir1 == right && snake1[0][2] == width-1) ||
(dir1 == left && snake1[0][2] == 0)){
dir1 = nextDir(dir1, e);
}
// Random branching at the side edges
else if((dir1 == up && snake1[1][2] == randomy && snake1[0][2] == width-1) || (dir1 == down && snake1[1][2] == randomy && snake1[0][2] == 0)){
dir1 = nextDir(dir1, LEFT);
e = (e+2)%2+1;
}
else if((dir1 == down && snake1[1][2] == randomy && snake1[0][2] == width-1) || (dir1 == up && snake1[1][2] == randomy && snake1[0][2] == 0)){
dir1 = nextDir(dir1, RIGHT);
e = (e+2)%2+1;
}
else if((dir1 == left && snake1[0][2] == randomx && snake1[1][2] == 0) || (dir1 == right && snake1[0][2] == randomx && snake1[1][2] == height-1)){
dir1 = nextDir(dir1, LEFT);
e = (e+2)%2+1;
}
else if((dir1 == right && snake1[0][2] == randomx && snake1[1][2] == 0) || (dir1 == left && snake1[0][2] == randomx && snake1[1][2] == height-1)){
dir1 = nextDir(dir1, RIGHT);
e = (e+2)%2+1;
}
for(int i = 0; i < 3; i++){
// draw the snake
matrix.drawPixel(snake1[0][i], snake1[1][i], color);
}
matrix.show();
if(i%20== 0){
randomy = random(1,8);
randomx = random(1,4);
}
delay(200);
}
}
direction nextDir(direction dir, int d){
// d = 0 -> continue straight on
// d = 1 -> turn LEFT
// d = 2 -> turn RIGHT
direction selection[3];
switch(dir){
case right:
selection[0] = right;
selection[1] = up;
selection[2] = down;
break;
case left:
selection[0] = left;
selection[1] = down;
selection[2] = up;
break;
case up:
selection[0] = up;
selection[1] = left;
selection[2] = right;
break;
case down:
selection[0] = down;
selection[1] = right;
selection[2] = left;
break;
}
direction next = selection[d];
switch(next){
case right:
dx = 1;
dy = 0;
break;
case left:
dx = -1;
dy = 0;
break;
case up:
dx = 0;
dy = -1;
break;
case down:
dx = 0;
dy = 1;
break;
}
return next;
}
void colorgradient(int shape){
int wert = 0;
for(int k = 0; k < 500; k++){
matrix.fillScreen(0); // clear screen
for(int i = 0; i < shape; i++){
if(shape == LINE) matrix.drawLine(0,i,5,i, Wheel(((i * 256 / 20) + wert) & 255));
else if(shape == RECT) matrix.drawRect(2-i,4-i,2+i*2,2+i*2, Wheel(((i * 256 / 20) + wert) & 255));
}
matrix.show(); // show the screen
delay(50);
wert--;
if(wert <= 0) wert = 255;
}
}
void tetris(){
boolean figures[9][3][3]={{ {0,0,0},
{0,0,0},
{0,0,0}},
{ {1,0,0},
{1,0,0},
{1,0,0}},
{ {0,0,0},
{1,0,0},
{1,0,0}},
{ {0,0,0},
{1,1,0},
{1,0,0}},
{ {0,0,0},
{0,0,0},
{1,1,0}},
{ {0,0,0},
{1,1,0},
{1,1,0}},
{ {0,0,0},
{0,0,0},
{1,1,1}},
{ {0,0,0},
{1,1,1},
{1,0,0}},
{ {0,0,0},
{0,0,1},
{1,1,1}}};
int screen[height+3][width] = {{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}};
boolean tomove[20];
int counterTime = 0;
int counterID = 0;
int randx = 0;
int randTile = 0;
boolean finish = true;
boolean lose = false;
while(!lose){
matrix.fillScreen(0); // clear screen
counterTime++;
if(finish){
lose = false;
for(int s = 0; s < 6; s++){
if(screen[3][s] != 0) lose = true;
}
if(lose){
counterID = 0;
for(int i = 0; i < 6; i++){
matrix.fillScreen(0);
for(int s = 0; s < width; s++){
for(int z = 0; z < height+3; z++){
if(screen[z][s] != 0){
matrix.drawPixel(s+i+1,z-3,colors[(screen[z][s]%6)]);
}
else{
matrix.drawPixel(s+i+1,z-3,0);
}
}
}
matrix.show();
delay(130);
}
for(int s = 0; s < width; s++){
for(int z = 0; z < height+3; z++){
screen[z][s] = 0;
}
}
}
counterID++;
randTile = random(1,9);
randx = random(0,6 - (randTile/3));
for(int s1 = 0, s2 = randx; s1 <= randTile/3; s1++, s2++){
for(int z = 0; z < 3; z++){
if(figures[randTile][z][s1]) screen[z][s2] = counterID;
}
}
}
for(int i = 0; i < 20; i++) tomove[i]=true;
for(int s = 0; s < width; s++){
for(int z = 0; z < height+3; z++){
if(screen[z][s] != 0){
if(z == height+2 || (screen[z+1][s] != 0 && screen[z+1][s] != screen[z][s])){
tomove[screen[z][s]] = false;
}
matrix.drawPixel(s,z-3,colors[(screen[z][s]%6)]);
}
}
}
finish = true;
for(int s = width-1; s >= 0; s--){
for(int z = height+1; z >= 0; z--){
if(screen[z][s] != 0 && tomove[screen[z][s]]){
screen[z+1][s] = screen[z][s];
screen[z][s] = 0;
finish = false;
}
}
}
matrix.show(); // show the screen
delay(300);
}
}
void text(int colorbegin){
int x = matrix.width();
int pass = 0;
while( pass < 3){
matrix.fillScreen(0);
matrix.setCursor(x, 0);
int len = in.length();
matrix.print(in);
if(--x < -len*6) {
x = matrix.width();
pass++;
matrix.setTextColor(colors[(colorbegin+pass)%6]);
}
matrix.show();
delay(100);
}
}
// Input a value 0 to 255 to get a color value.
// The colours are a transition r - g - b - back to r.
uint32_t Wheel(byte WheelPos) {
WheelPos = 255 - WheelPos;
if(WheelPos < 85) {
return matrix.Color(255 - WheelPos * 3, 0, WheelPos * 3);
}
if(WheelPos < 170) {
WheelPos -= 85;
return matrix.Color(0, WheelPos * 3, 255 - WheelPos * 3);
}
WheelPos -= 170;
return matrix.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}
//Write a sequence of bytes starting at the specified address.
//Returns True if the entire array has been written,
//Returns False if start or end address is not between the minimum and maximum allowed range.
//If False was returned, nothing was written
boolean eeprom_write_bytes(int startAddr, const byte* array, int numBytes) {
int i;
if (!eeprom_is_addr_ok(startAddr) || !eeprom_is_addr_ok(startAddr + numBytes)) return false;
for (i = 0; i < numBytes; i++) {
EEPROM.write(startAddr + i, array[i]);
} return true;
}
//Writes an int value to the specified address.
boolean eeprom_write_int(int addr, int value) {
byte *ptr;
ptr = (byte*)&value;
return eeprom_write_bytes(addr, ptr, sizeof(value));
}
//Reads an integer value at the specified address
boolean eeprom_read_int(int addr, int* value) {
return eeprom_read_bytes(addr, (byte*)value, sizeof(int));
}
//Reads the specified number of bytes at the specified address
boolean eeprom_read_bytes(int startAddr, byte array[], int numBytes) {
int i;
if (!eeprom_is_addr_ok(startAddr) || !eeprom_is_addr_ok(startAddr + numBytes)) return false;
for (i = 0; i < numBytes; i++) {
array[i] = EEPROM.read(startAddr + i);
} return true;
}
//Returns True if the specified address is between the minimum and the maximum allowed range.
//Invoked by other superordinate functions to avoid errors.
boolean eeprom_is_addr_ok(int addr) {
return ((addr >= EEPROM_MIN_ADDR) && (addr <= EEPROM_MAX_ADDR));
}
//Write a string, starting at the specified address
boolean eeprom_write_string(int addr, const char* string) {
int numBytes;
numBytes = strlen(string) + 1;
return eeprom_write_bytes(addr, (const byte*)string, numBytes);
}
//Reads a string from the specified address
boolean eeprom_read_string(int addr, char* buffer, int bufSize) {
byte ch;
int bytesRead;
if (!eeprom_is_addr_ok(addr)) return false;
if (bufSize == 0) return false;
if (bufSize == 1) {
buffer[0] = 0;
return true;
}
bytesRead = 0;
ch = EEPROM.read(addr + bytesRead);
buffer[bytesRead] = ch;
bytesRead++;
while ((ch != 0x00) && (bytesRead < bufSize) && ((addr + bytesRead) <= EEPROM_MAX_ADDR)) {
ch = EEPROM.read(addr + bytesRead);
buffer[bytesRead] = ch;
bytesRead++;
}
if ((ch != 0x00) && (bytesRead >= 1)) buffer[bytesRead - 1] = 0;
return true;
}