- Dec 7, 2009
- 10,132
- 382
- 126
I have a microcontroller lighting an RGB LED strip with addressable LEDs doing a sweep back and forth similar to the old Knight Rider TV show (yeah the HOFF lol) Only this will be for holiday lighting not simulating AI automobilies.
For the forward sweep I had these statements:
Which I managed to change into a for loop, noticing that
2^3=8,
2^2=4,
2^1=2 and
2^0=1
so I changed those 4 statements into
pow(2,x) returns 2 to the power of x (the 0.5 is just to correct rounding errors)
it works fine, and now it's expandable to more than 4 just by changing 1 number and of course I don't need as many statements as pixels
the problem is with going in reverse the statements look like this:
Now to change these 4 statements into a for loop, I'm not sure how to find the mathematical relationship between what is subtracted from i and what r,g and b are divided by.
The other one was obviously 2 to the power of x but this one is eluding me for some reason.
thanks AT braintrust!
Pic of the LEDs sweeping to the left. I picked red (255, 0, 0) for the pic to contrast the white table but the dynamic range of the camera isn't nearly as good as the human eye so it looks better in person. The trailing leds are dimming as they should be. The code works, just not easily expandable without copy pastaing lines of code and modding each one which would suck.
This is the whole function in case you want to see it. Not the whole program though, if you need that for some reason let me know. I'm still working on it so it's messy for now.
For the forward sweep I had these statements:
Code:
strip.setPixelColor(i, strip.Color(R, G, B)); // turn the pixel on
strip.setPixelColor(i-1, strip.Color(R/2, G/2, B/2)); // turn the pixel on
strip.setPixelColor(i-2, strip.Color(R/4, G/4, B/4)); // turn the pixel on
strip.setPixelColor(i-3, strip.Color(R/8, G/8, B/8)); // turn the pixel on
Which I managed to change into a for loop, noticing that
2^3=8,
2^2=4,
2^1=2 and
2^0=1
so I changed those 4 statements into
Code:
for (int x=0; x<4; x++) // 4 pixels with dimming trail
{
strip.setPixelColor(i-x, strip.Color(R/(pow(2,x)+0.5), G/(pow(2,x)+0.5), B/(pow(2,x)+0.5))); // turn the pixel on
}
it works fine, and now it's expandable to more than 4 just by changing 1 number and of course I don't need as many statements as pixels
the problem is with going in reverse the statements look like this:
Code:
strip.setPixelColor(i, strip.Color(R/8, G/8, B/8)); // turn the pixel on
strip.setPixelColor(i-1, strip.Color(R/4, G/4, B/4)); // turn the pixel on
strip.setPixelColor(i-2, strip.Color(R/2, G/2, B/2)); // turn the pixel on
strip.setPixelColor(i-3, strip.Color(R, G, B)); // turn the pixel on
Now to change these 4 statements into a for loop, I'm not sure how to find the mathematical relationship between what is subtracted from i and what r,g and b are divided by.
The other one was obviously 2 to the power of x but this one is eluding me for some reason.
thanks AT braintrust!
Pic of the LEDs sweeping to the left. I picked red (255, 0, 0) for the pic to contrast the white table but the dynamic range of the camera isn't nearly as good as the human eye so it looks better in person. The trailing leds are dimming as they should be. The code works, just not easily expandable without copy pastaing lines of code and modding each one which would suck.
This is the whole function in case you want to see it. Not the whole program though, if you need that for some reason let me know. I'm still working on it so it's messy for now.
Code:
// Sweeps 4 pixels back and forth
void Sweep(uint16_t wait, uint8_t R, uint8_t G, uint8_t B)
{
for(uint16_t i=0; i<strip.numPixels(); i++) // sweep from 0 to number of Pixels
{
/* strip.setPixelColor(i, strip.Color(R, G, B)); // turn the pixel on
strip.setPixelColor(i-1, strip.Color(R/2, G/2, B/2)); // turn the pixel on
strip.setPixelColor(i-2, strip.Color(R/4, G/4, B/4)); // turn the pixel on
strip.setPixelColor(i-3, strip.Color(R/8, G/8, B/8)); // turn the pixel on
*/
for (int x=0; x<4; x++) // 4 pixels with dimming trail
{
strip.setPixelColor(i-x, strip.Color(R/(pow(2,x)+0.5), G/(pow(2,x)+0.5), B/(pow(2,x)+0.5))); // turn the pixel on
}
strip.show();
delay(wait); //wait
for(int d=0; d<4; d++)
strip.setPixelColor(i-d, 0); // turn the pixels off
}
for(uint16_t i=strip.numPixels(); i>3; --i) // now reverse the sweep
{
strip.setPixelColor(i, strip.Color(R/8, G/8, B/8)); // turn the pixel on
strip.setPixelColor(i-1, strip.Color(R/4, G/4, B/4)); // turn the pixel on
strip.setPixelColor(i-2, strip.Color(R/2, G/2, B/2)); // turn the pixel on
strip.setPixelColor(i-3, strip.Color(R, G, B)); // turn the pixel on
strip.show();
delay(wait); //wait
for(int d=0; d<4; d++)
strip.setPixelColor(i-d, 0); // turn the pixel off
}
}
