-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtestLED.ino
More file actions
72 lines (55 loc) · 2.15 KB
/
Copy pathtestLED.ino
File metadata and controls
72 lines (55 loc) · 2.15 KB
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
#include <FastLED.h>
#define NUM_LEDS 21
#define DATA_PIN 5
CRGB leds[NUM_LEDS];
int halfWaveLength = 8; //the number of segments ahead and behind a moving peak until the
int numColorPeaks = 3; //NUM_LEDS / halfWaveLength (Round down) + 2
long cylcleTime = 30000; //How many ms it takes a color to move one halfWaveLength
float peakPosition[3]; //[numColorPeaks]
int peakColors[3][3]; //[numColorPeaks]
int delayTime = 500;
void setup() { FastLED.addLeds<WS2811, DATA_PIN>(leds, NUM_LEDS);
for (int i = 0; i <numColorPeaks; i++) {
resetColor(i);
peakPosition[i] = (i - 1) * halfWaveLength;
}
}
void resetColor(int i) {
peakColors[i][0] = random(0,254); //red value
peakColors[i][1] = random(0,254); //grn value
peakColors[i][2] = random(0,254); //blu value
}
void loop() {
//Move peaks by time
for (int i = 0; i <numColorPeaks; i++) {
peakPosition[i] = peakPosition[i] + delayTime / cylcleTime * halfWaveLength;
//Check if any peaks are past (numColorPeaks - 2 ) * halfWaveLength. If so, move them back by numColorPeaks * halfWaveLength (sent back to beginning)
if (peakPosition[i] > ((numColorPeaks - 2 ) * halfWaveLength)) {
peakPosition[i] =peakPosition[i] - numColorPeaks * halfWaveLength;
resetColor(i);
}
}
//Calculate all segment color mixes
for (int n = 0; n < NUM_LEDS; n++) {
//set all colors to off
leds[n].r = 0;
leds[n].g = 0;
leds[n].b = 0;
}
//cycle through the peaks
int segStart;
float distanceProp;
for (int i = 0; i <numColorPeaks; i++) { //cycling through the peaks
segStart = ((int) peakPosition[i]) +1;
for (int j = segStart; j < (segStart + 2 * halfWaveLength - 1); j++) { //cycling through the segments attached to that peak
if ((j > 0) && (j <= NUM_LEDS) ) {//Check if this is an actual led
distanceProp = abs((peakPosition[i] - j) / halfWaveLength);
leds[j].r = leds[j].r + (int) (distanceProp * peakColors[i][0]);
leds[j].g = leds[j].g + (int) (distanceProp * peakColors[i][1]);
leds[j].b = leds[j].b + (int) (distanceProp * peakColors[i][2]);
}
}
}
FastLED.show();
delay(delayTime);
}