Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
140 changes: 87 additions & 53 deletions testLED.ino
Original file line number Diff line number Diff line change
@@ -1,72 +1,106 @@
#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]
#define WAVELENGTH 768 // # of steps peak-to-peak
#define MAXBRIGHT 255 // maximum brightness level (min is 0)
#define DELAY_MS 100 // delay before updating colors, in ms

/*
we're going to use a triangle wave that's truncated below 0, like so:
MAX |\ /\
| \ / \
| \ / \
| \ / \
0___|____\__________/________\_
|

int peakColors[3][3]; //[numColorPeaks]
int delayTime = 500;
but then overlay 3 of these, one each for R, G, B:
R G B R G
MAX |\ /\ /\ oops /\ /\
| \ / \ / \ / \ /
| X X \ / X
| / \ / \ \ / / \
0___|/___\/___\____\/____/___\_
|

so really we want to cut off half of the negative (zero) part of the cycle:
MAX |\ /\
| \ / \
| \ / \
| \ / \
0___|____\_____/________\_
|

for this pattern:
R G B R G B
MAX |\ /\ /\ /\ /\ /\
| \ / \ / \ / \ / \ /
| X X X X X
| / \ / \ / \ / \ / \
0___|/___\/___\/___\/___\/___\/_
|

so, using WAVELENGTH and MAXBRIGHT, we precalculate an array of values sampled
from the conceptual (dis)continuous function, then shift along those at each
step, with a different offset for each color
*/

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
CRGB leds[NUM_LEDS];
int waveform[WAVELENGTH]
void setup() {
FastLED.addLeds<WS2811, DATA_PIN>(leds, NUM_LEDS);

}
// TODO: delete most of this code by using the FastLED triwave8() func
// also look into other provided funcs
// (maybe beatsin8 would be even better?)

void loop() {
/*
start moving upward from the wave minimum, so we're going to generate this:
MAX | /\
| / \
0___|___/____\_
*/
double slope = MAXBRIGHT / (WAVELENGTH / 3);
int val = -1 * MAXBRIGHT;
for (int i=0; i < WAVELENGTH; i++) {
// TODO: probably going to be some errors in here
// wrt int -> double -> int conversion

// clamp to 0 while val is negative
if (val >= 0) {
waveform[i] = val;
} else {
waveform[i] = 0;
}

//Move peaks by time
for (int i = 0; i <numColorPeaks; i++) {
peakPosition[i] = peakPosition[i] + delayTime / cylcleTime * halfWaveLength;
val = floor(val + slope);

//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);
// once we hit max, start going down
if (val >= MAXBRIGHT) {
val = MAXBRIGHT;
slope *= -1;
}
}
}

//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]);
}
}
}
// these offsets will make the cycle start at 100% red,
// as in the diagram above
int Ridx = (2 * WAVELENGTH) / 3;
int Gidx = WAVELENGTH / 3;
int Bidx = 0;
void loop() {
// scan through the full waveform to assign a different value
// to each LED, looping back to the start as necessary
for(int n=0; n < NUM_LEDS; n++) {
leds[n].r = waveform[Ridx];
leds[n].g = waveform[Gidx];
leds[n].b = waveform[Bidx];

Ridx = (Ridx + 1) % WAVELENGTH;
Gidx = (Gidx + 1) % WAVELENGTH;
Bidx = (Bidx + 1) % WAVELENGTH;
}

FastLED.show();
delay(delayTime);

delay(DELAY_MS);
}