-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleds_fft.ino
517 lines (443 loc) · 12.6 KB
/
leds_fft.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
/* LED Output: FFT of Audio Signal
(Spectrum Analyzer)
Author: J.F. MacArt
Date: 10 Dec 2016
*/
#include <EEPROM.h>
#include<fix_fft.h>
#include<FastLED.h>
#define MAX_BRIGHT 255
#define NUM_LEVELS 6
#define num_hist 2
#define pin_adc 0
// Global variables for FFT
char im[128];
char data[128];
int outVal[NUM_LEVELS][num_hist];
int outHue[NUM_LEVELS];
int ii=0; // Don't modify outside main loop!
int val;
uint8_t pattern=0;
bool nextPattern = false;
// Global variables for FastLED
byte state = 1;
int index = 0; //-LED INDEX (0 to LED_COUNT-1)
byte thisbright = 128;
byte thissat = 255;
int thisdelay = 0;
byte thisstep = 10;
byte thishue = 0;
byte interrupt = 0;
// How many leds are in the strip?
// Be careful with the num leds. The animations that make use of the levels
// will walk right off the led array. The numbers are specific to my led
// tree. If you are using less leds or are not interested in the layer animations
// you should remove them from the loop or set USE_LEVEL_ANIMATIONS to 0
//#define NUM_LEDS 30 // LED Strip
//#define LED_TYPE WS2812B // LED Strip
//#define COLOR_ORDER GRB // LED Strip
#define NUM_LEDS 300 //348 // Tree
#define LED_TYPE WS2811 // Tree
#define COLOR_ORDER RGB // Tree
//int ledBin[NUM_LEDS];
//byte USE_LEVEL_ANIMATIONS = 1; // if 1, will auto advance past level animations
//const PROGMEM prog_uint16_t levels[NUM_LEVELS] = {58, 108, 149, 187, 224, 264, 292, 300};
//PROGMEM const uint16_t levels[NUM_LEVELS] = {58, 108, 149, 187, 224, 264, 292, 300};
// Data pin that led data will be written out over
#define DATA_PIN 10
// This is an array of leds. One item for each led in your strip.
CRGB leds[NUM_LEDS];
/////////////////////////////////////////////////////////////////////////
// Global setup for FastLED and FFT routine
/////////////////////////////////////////////////////////////////////////
void loadSettings();
void advancePattern();
void setup() {
Serial.begin(9600);
// sanity check delay - allows reprogramming if accidently blowing power w/leds
delay(2000);
// Load saved parameters from EEPROM
loadSettings();
Serial.print("Loaded pattern ");
Serial.println(pattern);
pattern = pattern-1; // fix for init bug
// FastLED initialization
FastLED.addLeds<LED_TYPE, DATA_PIN, COLOR_ORDER>(leds, NUM_LEDS);
FastLED.setBrightness(MAX_BRIGHT);
one_color_all(0,0,0);
FastLED.show();
delay(1000);
int n,m;
pinMode(pin_adc, INPUT);
// Set initial values
Serial.println("Setting initial values...\n");
for (n=0;n<NUM_LEVELS;n++) {
for (m=0;m<num_hist;m++) {
outVal[n][m] = 0;
}
}
// Bin the leds for FFT output
Serial.println("Binning the LEDs...\n");
/*
int ledBin[NUM_LEDS];
int bin = 0;
int count = 0;
for (n=0;n<NUM_LEDS;n++) {
if (count>(NUM_LEDS/NUM_LEVELS)) {
bin++;
count = 0;
}
ledBin[n] = bin;
count++;
}
*/
// Set initial hues
int hue = 0;
for (n=0;n<NUM_LEVELS;n++) {
outHue[n] = hue;
hue = hue + 255/NUM_LEVELS;
}
// Test the bins
Serial.println("Testing the bins...\n");
/*
for (n=0;n<NUM_LEVELS;n++) {
for (m=0;m<NUM_LEDS;m++) {
if (ledBin[m]==n) {
leds[m] = CHSV(outHue[n], thissat, thisbright);
}
else{
leds[m] = CHSV(0, 0, 0);
}
}
FastLED.show();
delay(500);
}
*/
Serial.println("Starting!\n");
}
/////////////////////////////////////////////////////////////////////////
// LED color patterns
/////////////////////////////////////////////////////////////////////////
void one_color_all(int cred, int cgrn, int cblu)
{ //-SET ALL LEDS TO ONE COLOR
for(int i = 0 ; i < NUM_LEDS; i++ ) {
leds[i].setRGB( cred, cgrn, cblu).nscale8_video(thisbright);
}
}
void clear_all()
{
one_color_all(0, 0, 0);
LEDS.show();
delay(50);
}
void colorTest()
{
one_color_all(0,0,0);
FastLED.show();
delay(500);
one_color_all(255,0,0);
FastLED.show();
delay(500);
// one_color_all(0,0,0);
// FastLED.show();
// delay(1000);
one_color_all(0,255,0);
FastLED.show();
delay(500);
// one_color_all(0,0,0);
// FastLED.show();
// delay(1000);
one_color_all(0,0,255);
FastLED.show();
delay(500);
}
void dotTest()
{
// Move a single white led
for(int whiteLed = 0; whiteLed < NUM_LEDS; whiteLed = whiteLed + 1) {
// Turn our current led on to white, then show the leds
leds[whiteLed] = CRGB::White;
// Show the leds (only one of which is set to white, from above)
FastLED.show();
// Wait a little bit
delay(10);
// Turn our current led back to black for the next loop around
leds[whiteLed] = CRGB::Black;
}
}
void random_burst()
{
int rndidx = random16(0, NUM_LEDS);
int rndhue = random8(0, 255);
int rndbright = random8(10, thisbright);
leds[rndidx] = CHSV(rndhue, thissat, rndbright);
delay(random8(0, thisdelay));
}
void random_burst_white()
{
int rndidx = random16(0, NUM_LEDS);
int hue = 60; // set to yellow
int rndsat = random8(0,100);
int rndbright = random8(10, thisbright);
leds[rndidx] = CHSV(hue, rndsat, rndbright);
delay(random8(0, thisdelay));
}
void rgb_propeller()
{
thishue = 0;
index++;
int ghue = (thishue + 80) % 255;
int bhue = (thishue + 160) % 255;
int N3 = int(NUM_LEDS/3);
int N6 = int(NUM_LEDS/6);
int N12 = int(NUM_LEDS/12);
for(int i = 0; i < N3; i++ ) {
int j0 = (index + i + NUM_LEDS - N12) % NUM_LEDS;
int j1 = (j0+N3) % NUM_LEDS;
int j2 = (j1+N3) % NUM_LEDS;
leds[j0] = CHSV(thishue, thissat, thisbright);
leds[j1] = CHSV(ghue, thissat, thisbright);
leds[j2] = CHSV(bhue, thissat, thisbright);
}
}
void candycane()
{
index++;
int N3 = int(NUM_LEDS/3);
int N6 = int(NUM_LEDS/6);
int N12 = int(NUM_LEDS/12);
for(int i = 0; i < N6; i++ ) {
int j0 = (index + i + NUM_LEDS - N12) % NUM_LEDS;
int j1 = (j0+N6) % NUM_LEDS;
int j2 = (j1+N6) % NUM_LEDS;
int j3 = (j2+N6) % NUM_LEDS;
int j4 = (j3+N6) % NUM_LEDS;
int j5 = (j4+N6) % NUM_LEDS;
leds[j0] = CRGB(255, 255, 255).nscale8_video(thisbright*.75);
leds[j1] = CRGB(255, 0, 0).nscale8_video(thisbright);
leds[j2] = CRGB(255, 255, 255).nscale8_video(thisbright*.75);
leds[j3] = CRGB(255, 0, 0).nscale8_video(thisbright);
leds[j4] = CRGB(255, 255, 255).nscale8_video(thisbright*.75);
leds[j5] = CRGB(255, 0, 0).nscale8_video(thisbright);
}
}
CHSV sv_ramp( uint8_t hue, uint8_t ramp) {
uint8_t brightness;
uint8_t saturation;
if( ramp < 128) {
// fade toward black
brightness = ramp * 2;
brightness = dim8_video( brightness);
// uint8_t global_brightness = FastLED.getBrightness();
// uint8_t min_step = 256 / global_brightness;
// brightness = qadd8( min_step * 16, brightness);
saturation = 255;
} else {
// desaturate toward white
brightness = 255;
saturation = 255 - ((ramp - 128) * 2);
saturation = 255 - dim8_video( 255 - saturation);
}
return CHSV( hue, saturation, brightness); }
void loop5( CRGB* L )
{
uint8_t GB = FastLED.getBrightness();
uint8_t boost = 0;
// if( GB < 65) boost += 8;
// if( GB < 33) boost += 8;
uint8_t N = 2;
static uint16_t starttheta = 0;
starttheta += 100 / N;
static uint16_t starthue16 = 0;
starthue16 += 20 / N;
uint16_t hue16 = starthue16;
uint16_t theta = starttheta;
for( int i = 0; i < NUM_LEDS; i++) {
uint8_t frac = (sin16( theta) + 32768) / 256;
frac = scale8(frac,160) + 32;
theta += 3700;
hue16 += 2000;
uint8_t hue = hue16 / 256;
L[i] = sv_ramp( hue, frac + boost);
}
}
CHSV sv_ramp_white( uint8_t hue, uint8_t ramp) {
uint8_t brightness;
uint8_t saturation;
if( ramp < 128) {
// fade toward black
brightness = ramp * 2;
brightness = dim8_video( brightness);
// uint8_t global_brightness = FastLED.getBrightness();
// uint8_t min_step = 256 / global_brightness;
// brightness = qadd8( min_step * 16, brightness);
saturation = 175;
} else {
// desaturate toward white
brightness = 255;
saturation = 255 - ((ramp - 128) * 2);
saturation = scale8(255 - dim8_video( 255 - saturation),175);
}
return CHSV( hue, saturation, brightness); }
void treeWhiteTwinkle( CRGB* L )
{
uint8_t GB = FastLED.getBrightness();
uint8_t boost = 0;
// if( GB < 65) boost += 8;
// if( GB < 33) boost += 8;
uint8_t N = 2;
static uint16_t starttheta = 0;
starttheta += 100 / N;
// static uint16_t starthue16 = 0;
// starthue16 += 20 / N;
// uint16_t hue16 = starthue16;
uint16_t theta = starttheta;
for( int i = 0; i < NUM_LEDS; i++) {
uint8_t frac = (sin16( theta) + 32768) / 256;
frac = scale8(frac,160) + 32;
theta += 3700;
//hue16 += 2000;
uint8_t hue = 60; //set to yellow! //hue16 / 256; // 43 might be more "yellow"
L[i] = sv_ramp_white( hue, frac + boost);
}
}
void rainbow_fade()
{
thishue++;
if (thishue > 255) {thishue = 0;}
for(int idex = 0 ; idex < NUM_LEDS; idex++ ) {
leds[idex] = CHSV(thishue, thissat, thisbright);
}
}
/////////////////////////////////////////////////////////////////////////
// Main loop
/////////////////////////////////////////////////////////////////////////
boolean INIT = 0;
/*
byte alt = 0;
boolean doRandom = 0;
boolean stateInc = 0;
int randState = 1;
*/
unsigned long delayTimer = 0;
// Second vector of led's for transition
//CHSV hsv_target[NUM_LEDS];
void loop(){
int n;
//int b;
int sum;
int outMult[NUM_LEVELS] = {10,15,25,30,35,35};
float alpha[NUM_LEVELS] = {0.3,0.25,0.22,0.22,0.22,0.22};
//int adjdelay = thisdelay;
char buf[35];
if (!INIT) {
//Light the tree
dotTest();
//colorTest();
clear_all();
INIT = 1;
}
// Default pattern
//rainbow_fade();
//adjdelay = thisdelay;
if (ii < 128) {
// Take data
val = analogRead(pin_adc);
// Signal conditioned to 2.5 V +/- 2.5 V
// -- (analog reads in 512 +/- 512) -- use full range of 8-bit FFT (-128 to 128)
data[ii] = (val-512)/4;
im[ii] = 0;
ii++;
}
else {
// Do the in-place, fixed-point FFT
fix_fft(data,im,7,0);
// I am only interested in the absolute value of the transformation
for (int i=0; i< 64;i++){
data[i] = sqrt(data[i] * data[i] + im[i] * im[i]);
}
// Keep an exponential moving average for each output bin
for (n=0;n<NUM_LEVELS;n++) {
sum = 0;
for (int i=1;i<=10;i++) {
if (data[n*10+i]>sum) {
sum = data[n*10+i];
}
}
// Avoid flickering
if (sum<3) {
sum = 0;
}
//sum = sum*outMult[n];
sum = sum*outMult[n];
// Update the average
outVal[n][1] = outVal[n][0];
outVal[n][0] += alpha[n]*(sum - outVal[n][0]);
// Clip at max brightness
if (outVal[n][0]>MAX_BRIGHT) {
outVal[n][0] = MAX_BRIGHT;
}
}
if (outVal[0][0]>0) {
delayTimer = millis();
// Print to monitor
sprintf(buf,"out %i,%i,%i,%i,%i,%i",outVal[0][0],outVal[1][0],outVal[2][0],outVal[3][0],outVal[4][0],outVal[5][0]);
Serial.println(buf);
}
if ((millis()-delayTimer)>10000) {
//Serial.println(pattern);
if (nextPattern) {
nextPattern = false;
advancePattern(); // Saves state to EEPROM
Serial.print("Switching to pattern ");
Serial.println(pattern);
}
// more than ten seconds of silence, show white LEDs
if (pattern==0) {
treeWhiteTwinkle(leds);
} else if (pattern==1) {
loop5(leds);
} else if (pattern==2) {
random_burst();
} else if (pattern==3) {
random_burst_white();
} else {
// Reset to the first pattern
pattern = 0;
}
}
else {
nextPattern = true;
// Next step -- update color and brightness
//thishue++;
//if (thishue > 255) {thishue = 0;}
for (n=0;n<NUM_LEVELS;n++) {
outHue[n]++;
if (outHue[n] > 255) {outHue[n] = 0;}
}
int bin=0;
int count=0;
for(int idex = 0 ; idex < NUM_LEDS; idex++ ) {
// Get the bin
if (count>(NUM_LEDS/NUM_LEVELS)) {
bin++;
count=0;
}
// Set the brightness from the FFT
leds[idex] = CHSV(outHue[bin], thissat, outVal[bin][0]);
count++;
// Can't do this with >200 LEDs; ledBin is too big for Arduino dynamic memory
//leds[idex] = CHSV(outHue[ledBin[idex]], thissat, outVal[ledBin[idex]][0]);
}
}
FastLED.show();
ii=0;
}
}
void loadSettings() {
pattern = EEPROM.read(0);
}
void advancePattern() {
pattern = pattern+1 > 3 ? 0 : pattern+1;
EEPROM.write(0, pattern);
}