-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChineseWordTest.ino
579 lines (517 loc) · 16.5 KB
/
ChineseWordTest.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
571
572
573
574
575
576
577
578
579
#include "Arduino.h"
#include "SoftwareSerial.h"
#include <FastLED.h>
#include <SPI.h>
#include <SD.h>
#define LED_TYPE WS2812B
#define DATA_PIN 3 // Data pin led data written out
#define COLOR_ORDER GRB // Impact the show of the color
#define BRIGHTNESS 2
#define SPACE 20 // devide how many leds a block
#define NUM_LEDS 200 // How many leds in the strip
CRGB leds[NUM_LEDS]; // Array of leds
#define DISPLAYTIME 5 //display an effect for how long
#define FRAMES_PER_SECOND 300 //frames per second
#define COLORSTRINGLEN 51
File myFile;
void setup() {
delay(2000);// power-up safety delay
Serial.begin(9600);
Serial.println(F("resetting"));
LEDS.addLeds<LED_TYPE,DATA_PIN,GRB>(leds,NUM_LEDS).setCorrection(TypicalLEDStrip);;
LEDS.setBrightness(BRIGHTNESS);
if (!SD.begin(4)) {
Serial.println("initialization failed!");
while (1);
}
ClearAll();
}
// List of patterns to cycle through. Each is defined as a separate function below.
typedef void (*SimplePatternList[])();
SimplePatternList gPatterns = { //WhitePurple,
//Jungle,
//MiddleOut,
//Blink,
//SparkSlowToFast,
//GlowGradient,
//Confetti,
//Sinelon,
//NTHUpattern,
//ClearAll,
FillAll,
ChineseWord
};
uint8_t gCurrentPatternNumber = 0; // Index number of which pattern is current
void loop() {
// Call the current pattern function once, updating the 'leds' array
gPatterns[gCurrentPatternNumber]();
// insert a delay to keep the framerate modest
FastLED.delay(1000/FRAMES_PER_SECOND);
// change patterns periodically
EVERY_N_SECONDS( DISPLAYTIME ) {
ClearAll();
nextPattern(); }
//WhitePurple();
//Jungle();
//MiddleOut();
//Blink();
//SparkSlowToFast();
//GlowGradient();
//Confetti();
//Sinelon();
//NTHUpattern();
//ClearAll();
}
#define ARRAY_SIZE(A) (sizeof(A) / sizeof((A)[0]))
void nextPattern()
{
// add one to the current pattern number, and wrap around at the end
gCurrentPatternNumber = (gCurrentPatternNumber + 1) % ARRAY_SIZE( gPatterns);
}
void WhitePurple() {
ClearAll();
LEDS.setBrightness(random(30,50));
// First slide the led in one direction
for(int z=2;z>0;z--){
for(int i = 0; i < SPACE; i++) {
for(int j=0;j<NUM_LEDS/SPACE;j++){
if(j%2==0)leds[i+SPACE*j] = CRGB::White;
else leds[i+SPACE*j] = CRGB::Purple;
FastLED.show();
delay(1);
fadeall();
}
}
}
for(int z=2;z>0;z--){
for(int i = 0; i < SPACE; i++) {
for(int j=0;j<NUM_LEDS/SPACE;j++){
if(j%2==0)leds[SPACE*(j+1)-i] = CRGB::White;
else leds[SPACE*(j+1)-i] = CRGB::Purple;
FastLED.show();
delay(1);
fadeall();
}
}
}
Serial.println(F("WhitePurple():done!"));
}
void Jungle() {
ClearAll();
//dark to light
for(int i = 1; i < 60 ; i=i+1){
fadeToBlackBy( leds, NUM_LEDS, 20);
byte dothue = 430;
for( int i = 0; i < 8; i++) {
leds[beatsin16( i+9, 0, NUM_LEDS-1 )] |= CHSV(dothue, 200, 255);
dothue += 1;//select the color purple
}
//add some glitter
if(random8()<40)leds[ random8(NUM_LEDS) ] += CRGB::White;
//set dynamic bright
LEDS.setBrightness(i);
FastLED.show();
}
//light to dark
for(int i = 60; i > 1 ; i=i-1){
fadeToBlackBy( leds, NUM_LEDS, 20);
byte dothue = 430;
for( int i = 0; i < 10; i++) {
leds[beatsin16( i+9, 0, NUM_LEDS-1 )] |= CHSV(dothue, 200, 255);
dothue += 1;//select the color purple
}
//add some glitter
if(random8()<40)leds[ random8(NUM_LEDS) ] += CRGB::White;
//set dynamic bright
LEDS.setBrightness(i);
FastLED.show();
}
}
void MiddleOut() {
ClearAll();
LEDS.setBrightness(random(30,50));
for(int i = 1; i < NUM_LEDS/4 ; i++){
leds[NUM_LEDS/2+i] = CRGB::DarkOrchid;
leds[NUM_LEDS/2-i] = CRGB::DarkOrchid;
FastLED.show();
delay(10);
}
for(int i = 1; i < NUM_LEDS/4 ; i++){
leds[NUM_LEDS/4*3+i] = CRGB::MediumSlateBlue;
leds[NUM_LEDS/4-i] = CRGB::MediumSlateBlue;
FastLED.show();
delay(5);
}
for(int i = 1; i < NUM_LEDS/4 ; i++){
leds[NUM_LEDS/2+i] = CRGB::Black;
leds[NUM_LEDS/2-i] = CRGB::Black;
FastLED.show();
delay(20);
}
for(int i = 1; i < NUM_LEDS/4 ; i++){
leds[NUM_LEDS/2+i] = CRGB::DarkSalmon;
leds[NUM_LEDS/2-i] = CRGB::DarkSalmon;
FastLED.show();
delay(15);
}
for(int i = 1; i < NUM_LEDS/2 ; i++){
leds[NUM_LEDS-i] = CRGB::Black;
leds[i] = CRGB::Black;
FastLED.show();
delay(10);
}
for(int i = 1; i < NUM_LEDS/4 ; i++){
leds[NUM_LEDS-i] = CRGB::MediumSlateBlue;
leds[NUM_LEDS/2+i] = CRGB::MediumSlateBlue;
leds[NUM_LEDS/2-i] = CRGB::MediumSlateBlue;
leds[i] = CRGB::MediumSlateBlue;
FastLED.show();
delay(20);
}
for(int i = 1; i < NUM_LEDS/4 ; i++){
leds[NUM_LEDS-i] = CRGB::Black;
leds[NUM_LEDS/2+i] = CRGB::Black;
leds[NUM_LEDS/2-i] = CRGB::Black;
leds[i] = CRGB::Black;
FastLED.show();
delay(10);
}
for(int i = 1; i < NUM_LEDS/4 ; i++){
leds[NUM_LEDS/4-i] = CRGB::Gray;
leds[NUM_LEDS/4+i] = CRGB::Gray;
leds[NUM_LEDS/4*3-i] = CRGB::Gray;
leds[NUM_LEDS/4*3+i] = CRGB::Gray;
FastLED.show();
delay(10);
}
}
void Blink(){
ClearAll();
fill_solid(leds,NUM_LEDS,0);
for(int i = 0; i < 6 ; i++){
leds[ random8(NUM_LEDS) ] += CRGB::White;
leds[ random8(NUM_LEDS) ] += CRGB::Purple;
leds[ random8(NUM_LEDS) ] += CRGB::DarkOrchid;
LEDS.setBrightness(random(1,61));
}
FastLED.show();
delay(25);
}
void GlowGradient(){
ClearAll();
//fill_gradient(leds,0, CHSV(50,255,255) ,NUM_LEDS-1, CHSV(150,255,255), SHORTEST_HUES);
for(int i = 1; i < 60 ; i=i+1){
fill_gradient_RGB(leds,0, CRGB::Purple ,NUM_LEDS-1, CRGB::Grey);
for(int y=10;y>0;y--)leds[ random8(NUM_LEDS) ] = CRGB::Black;
LEDS.setBrightness(i);
FastLED.show();
delay(10);
}
for(int i = 59; i >0 ; i=i-1){
fill_gradient_RGB(leds,0,CRGB::Purple,NUM_LEDS-1,CRGB::Grey);
for(int y=10;y>0;y--)leds[ random8(NUM_LEDS) ] = CRGB::Black;
LEDS.setBrightness(i);
FastLED.show();
delay(10);
}
for(int i = 1; i < 60 ; i=i+1){
fill_gradient_RGB(leds,0, CRGB::Gray ,NUM_LEDS-1, CRGB::Purple);
for(int y=10;y>0;y--)leds[ random8(NUM_LEDS) ] = CRGB::Black;
LEDS.setBrightness(i);
FastLED.show();
delay(10);
}
for(int i = 59; i >0 ; i=i-1){
fill_gradient_RGB(leds,0,CRGB::Gray,NUM_LEDS-1,CRGB::Purple);
for(int y=10;y>0;y--)leds[ random8(NUM_LEDS) ] = CRGB::Black;
LEDS.setBrightness(i);
FastLED.show();
delay(10);
}
}
void SparkSlowToFast(){
ClearAll();
for(int j = 2; j >0 ; j-- ){
for(int i = 1; i < 60 ; i=i+1){
fill_gradient_RGB(leds,0, CRGB::Purple ,NUM_LEDS-1, CRGB::Gray);
LEDS.setBrightness(i);
FastLED.show();
delay(j);
}
delay(j*10);
for(int i = 1; i < 60 ; i=i+1){
fill_gradient_RGB(leds,0, CRGB::Gray ,NUM_LEDS-1, CRGB::Purple);
LEDS.setBrightness(i);
FastLED.show();
delay(j);
}
delay(j*10);
for(int i = 1; i < 60 ; i=i+1){
fill_gradient_RGB(leds,0, CRGB::Purple ,NUM_LEDS/2, CRGB::Gray);
fill_gradient_RGB(leds,NUM_LEDS/2, CRGB::Gray ,NUM_LEDS, CRGB::Purple);
LEDS.setBrightness(i);
FastLED.show();
delay(j);
}
delay(j*10);
for(int i = 1; i < 60 ; i=i+1){
fill_gradient_RGB(leds,0, CRGB::Gray ,NUM_LEDS/2, CRGB::Purple);
fill_gradient_RGB(leds,NUM_LEDS/2, CRGB::Purple ,NUM_LEDS, CRGB::Gray);
LEDS.setBrightness(i);
FastLED.show();
delay(j);
}
delay(j*10);
for(int i = 1; i < 60 ; i=i+1){
fill_gradient_RGB(leds,0, CRGB::Purple ,NUM_LEDS/4, CRGB::Gray);
fill_gradient_RGB(leds,NUM_LEDS/4, CRGB::Gray ,NUM_LEDS/2, CRGB::Purple);
fill_gradient_RGB(leds,NUM_LEDS/2, CRGB::Purple ,NUM_LEDS/4*3, CRGB::Gray);
fill_gradient_RGB(leds,NUM_LEDS/4*3, CRGB::Gray ,NUM_LEDS, CRGB::Purple);
LEDS.setBrightness(i);
FastLED.show();
delay(j);
}
delay(j*10);
for(int i = 1; i < 60 ; i=i+1){
fill_gradient_RGB(leds,0, CRGB::Gray ,NUM_LEDS/4, CRGB::Purple);
fill_gradient_RGB(leds,NUM_LEDS/4, CRGB::Purple ,NUM_LEDS/2, CRGB::Gray);
fill_gradient_RGB(leds,NUM_LEDS/2, CRGB::Gray ,NUM_LEDS/4*3, CRGB::Purple);
fill_gradient_RGB(leds,NUM_LEDS/4*3, CRGB::Purple ,NUM_LEDS, CRGB::Gray);
LEDS.setBrightness(i);
FastLED.show();
delay(j);
}
delay(j*10);
for(int i = 1; i < 60 ; i=i+1){
for(int z=0;z<8;z++){
if(z%2==0) fill_gradient_RGB(leds,NUM_LEDS/8*z , CRGB::Gray ,NUM_LEDS/8*(z+1) , CRGB::Purple);
else fill_gradient_RGB(leds,NUM_LEDS/8*z , CRGB::Black ,NUM_LEDS/8*(z+1) , CRGB::Black);
}
LEDS.setBrightness(i);
FastLED.show();
delay(j);
}
delay(j*10);
for(int i = 1; i < 60 ; i=i+1){
for(int z=0;z<8;z++){
if(z%2==0) fill_gradient_RGB(leds,NUM_LEDS/8*z , CRGB::Black ,NUM_LEDS/8*(z+1) , CRGB::Black);
else fill_gradient_RGB(leds,NUM_LEDS/8*z , CRGB::Purple ,NUM_LEDS/8*(z+1) , CRGB::Gray);
}
LEDS.setBrightness(i);
FastLED.show();
delay(j);
}
delay(j*10);
}
}
void Confetti()
{
ClearAll();
LEDS.setBrightness(random(30,50));
uint8_t gHue = 440; //near purple
// random colored speckles that blink in and fade smoothly
fadeToBlackBy( leds, NUM_LEDS, 4);
int pos = random16(NUM_LEDS);
leds[pos] += CHSV( gHue + random8(30), 200+ random8(64), 255);
FastLED.show();
//delay(5);
}
void Sinelon()
{
for(int j = 5; j >0 ; j-- ){
ClearAll();
LEDS.setBrightness(random(30,50));
// a colored dot sweeping back and forth, with fading trails
fadeToBlackBy( leds, NUM_LEDS, 10);
//fadeToBlackBy(leds,num_led,x):x=fade out time,if smaller fade out slower
//beatsin16(x,low,high):x=bigger faster
int pos1 = beatsin16( 20, 0, NUM_LEDS);
leds[pos1] += CRGB::Purple;
int pos2 = beatsin16( 20, 0, NUM_LEDS);
leds[NUM_LEDS-1-pos2] += CRGB::White;
FastLED.show();
delay(10);
}
}
void fadeall() {
for(int i = 0; i < NUM_LEDS; i++) {
leds[i].nscale8(1000); }
}
void NTHUpattern(){
ClearAll();
LEDS.setBrightness(20);
int dDevideIn = 5;
int delaytime = 150;
int bound_0 = NUM_LEDS/5*0;
int bound_1 = NUM_LEDS/5*1;
int bound_2 = NUM_LEDS/5*2;
int bound_3 = NUM_LEDS/5*3;
int bound_4 = NUM_LEDS/5*4;
int bound_5 = NUM_LEDS/5*5;
fill_solid(leds,NUM_LEDS,0);
FastLED.show();
delay(delaytime*10);
//====================================
//N:
//1
fill_solid(leds,NUM_LEDS,0);
fill_gradient_RGB(leds,bound_0,CRGB::Purple,bound_5,CRGB::Purple);
FastLED.show(); delay(delaytime);
//2
fill_solid(leds,NUM_LEDS,0);
fill_gradient_RGB(leds,bound_1,CRGB::Purple,bound_2,CRGB::Purple);
FastLED.show(); delay(delaytime);
//3
fill_solid(leds,NUM_LEDS,0);
fill_gradient_RGB(leds,bound_2,CRGB::Purple,bound_3,CRGB::Purple);
FastLED.show(); delay(delaytime);
//4
fill_solid(leds,NUM_LEDS,0);
fill_gradient_RGB(leds,bound_3,CRGB::Purple,bound_4,CRGB::Purple);
FastLED.show(); delay(delaytime);
//5
fill_solid(leds,NUM_LEDS,0);
fill_gradient_RGB(leds,bound_0,CRGB::Purple,bound_5,CRGB::Purple);
FastLED.show(); delay(delaytime);
//====================================
fill_solid(leds,NUM_LEDS,0);
FastLED.show();
delay(delaytime);
delay(delaytime);
//====================================
//T:
//1
fill_solid(leds,NUM_LEDS,0);
fill_gradient_RGB(leds,bound_0,CRGB::Purple,bound_1,CRGB::Purple);
FastLED.show(); delay(delaytime);
//2
fill_solid(leds,NUM_LEDS,0);
fill_gradient_RGB(leds,bound_0,CRGB::Purple,bound_1,CRGB::Purple);
FastLED.show(); delay(delaytime);
//3
fill_solid(leds,NUM_LEDS,0);
fill_gradient_RGB(leds,bound_0,CRGB::Purple,bound_5,CRGB::Purple);
FastLED.show(); delay(delaytime);
//4
fill_solid(leds,NUM_LEDS,0);
fill_gradient_RGB(leds,bound_0,CRGB::Purple,bound_1,CRGB::Purple);
FastLED.show(); delay(delaytime);
//5
fill_solid(leds,NUM_LEDS,0);
fill_gradient_RGB(leds,bound_0,CRGB::Purple,bound_1,CRGB::Purple);
FastLED.show(); delay(delaytime);
//====================================
fill_solid(leds,NUM_LEDS,0);
FastLED.show();
delay(delaytime);
delay(delaytime);
//====================================
//H:
//1
fill_solid(leds,NUM_LEDS,0);
fill_gradient_RGB(leds,bound_0,CRGB::Purple,bound_5,CRGB::Purple);
FastLED.show(); delay(delaytime);
//2
fill_solid(leds,NUM_LEDS,0);
fill_gradient_RGB(leds,bound_2,CRGB::Purple,bound_3,CRGB::Purple);
FastLED.show(); delay(delaytime);
//3
fill_solid(leds,NUM_LEDS,0);
fill_gradient_RGB(leds,bound_2,CRGB::Purple,bound_3,CRGB::Purple);
FastLED.show(); delay(delaytime);
//4
fill_solid(leds,NUM_LEDS,0);
fill_gradient_RGB(leds,bound_2,CRGB::Purple,bound_3,CRGB::Purple);
FastLED.show(); delay(delaytime);
//5
fill_solid(leds,NUM_LEDS,0);
fill_gradient_RGB(leds,bound_0,CRGB::Purple,bound_5,CRGB::Purple);
FastLED.show(); delay(delaytime);
//====================================
fill_solid(leds,NUM_LEDS,0);
FastLED.show();
delay(delaytime);
delay(delaytime);
//====================================
//U:
//1
fill_solid(leds,NUM_LEDS,0);
fill_gradient_RGB(leds,bound_0,CRGB::Purple,bound_5,CRGB::Purple);
FastLED.show(); delay(delaytime);
//2
fill_solid(leds,NUM_LEDS,0);
fill_gradient_RGB(leds,bound_4,CRGB::Purple,bound_5,CRGB::Purple);
FastLED.show(); delay(delaytime);
//3
fill_solid(leds,NUM_LEDS,0);
fill_gradient_RGB(leds,bound_4,CRGB::Purple,bound_5,CRGB::Purple);
FastLED.show(); delay(delaytime);
//4
fill_solid(leds,NUM_LEDS,0);
fill_gradient_RGB(leds,bound_4,CRGB::Purple,bound_5,CRGB::Purple);
FastLED.show(); delay(delaytime);
//5
fill_solid(leds,NUM_LEDS,0);
fill_gradient_RGB(leds,bound_0,CRGB::Purple,bound_5,CRGB::Purple);
FastLED.show(); delay(delaytime);
//====================================
fill_solid(leds,NUM_LEDS,0);
FastLED.show();
delay(delaytime*10);
}
void ChineseWord(){
LEDS.setBrightness(50);
readSD();
Serial.println(F("ChineseWord():done!"));
}
void readSD(){
myFile = SD.open("output.txt");
if (myFile) {
//Serial.println("test.txt:");
// read from the file until there's nothing else in it:
int zz=0;
while (myFile.available()) {
String colorstring = "";
colorstring = myFile.readStringUntil('\n');
Serial.println(colorstring);
char buf[COLORSTRINGLEN];
int len=COLORSTRINGLEN;
colorstring.toCharArray(buf, len);
// make char to int
//int a = (buf[9]-'0') + (buf[10]-'0');
//Serial.print(F("a:"));Serial.println(a);
//for(int y=0;y<colorstring.length();y++){
// Serial.print(y);Serial.print(":");Serial.println(buf[y]);
//}
CRGB tmp = CRGB::Black;
int delaytime = 150;
fill_solid(leds,NUM_LEDS,0);
for(int x=0;x<colorstring.length();x++){//run buf & light
if((buf[x]-'0') == 0){tmp = CRGB::Black;}
else {tmp = CRGB::Purple;}
//int bound_0 = NUM_LEDS/colorstring.length()*x;
//int bound_1 = NUM_LEDS/colorstring.length()*(x+1);
//fill_gradient_RGB(leds,bound_0,tmp,bound_1,tmp);
fill_solid(leds+NUM_LEDS/colorstring.length()*x,NUM_LEDS/colorstring.length(),tmp);
}
FastLED.show();//delay(delaytime);
zz++;
//if(zz==10){while(1);}
}
// close the file:
myFile.close();
}
else {
// if the file didn't open, print an error:
Serial.println("error opening txt");
}
}
void ClearAll() {
fill_solid(leds,NUM_LEDS,0);
FastLED.show();
}
void FillAll() {
LEDS.setBrightness(50);
fill_solid(leds,NUM_LEDS,CRGB::Purple);
FastLED.show();
}