Hi !
So, I have one simple led strip connected on a Arduino Board.
Here is what i'm trying to accomplish. I want gradient lights to scroll through the led strip, and having at the same time random leds to blink and fade. I don't have any trouble generating the gradient and the random blink but i'm not able to make them work at the same time.
I'm working with this strip : Pololu - Addressable RGB 150-LED Strip, 5V, 5m (WS2812B)
Here's my code :
#include <PololuLedStrip.h>
// Create an ledStrip object on pin 11.
#define LED_SIGNAL_PIN 11
PololuLedStrip<LED_SIGNAL_PIN> ledStrip;
#define LED_COUNT 150
rgb_color colors[LED_COUNT];
void setup()
{
}
void loop()
{
doGradient();
brightTwinkle();
}
void doGradient() {
byte time = millis() >> 2;
for(uint16_t i = 0; i < LED_COUNT; i++)
{
byte x = time - 8*i;
colors[i] = (rgb_color){ x, 255 - x, x };
}
// Write the colors to the LED strip.
ledStrip.write(colors, LED_COUNT);
}
// This function fades val by decreasing it by an amount proportional
// to its current value. The fadeTime argument determines the
// how quickly the value fades. The new value of val will be:
// val = val - val*2^(-fadeTime)
// So a smaller fadeTime value leads to a quicker fade.
// If val is greater than zero, val will always be decreased by
// at least 1.
// val is a pointer to the byte to be faded.
void fade(unsigned char *val, unsigned char fadeTime)
{
if (*val != 0)
{
unsigned char subAmt = *val >> fadeTime; // val * 2^-fadeTime
if (subAmt < 1)
subAmt = 1; // make sure we always decrease by at least 1
*val -= subAmt; // decrease value of byte pointed to by val
}
}
// Helper function for adjusting the colors for the BrightTwinkle
// and ColorExplosion patterns. Odd colors get brighter and even
// colors get dimmer.
void brightTwinkleColorAdjust(unsigned char *color)
{
if (*color == 255)
{
// if reached max brightness, set to an even value to start fade
*color = 254;
}
else if (*color % 2)
{
// if odd, approximately double the brightness
// you should only use odd values that are of the form 2^n-1,
// which then gets a new value of 2^(n+1)-1
// using other odd values will break things
*color = *color * 2 + 1;
}
else if (*color > 0)
{
fade(color, 100);
if (*color % 2)
{
(*color)--; // if faded color is odd, subtract one to keep it even
}
}
}
void brightTwinkle()
{
for (int i = 0; i < LED_COUNT; i++)
{
brightTwinkleColorAdjust(&colors[i].red);
brightTwinkleColorAdjust(&colors[i].green);
brightTwinkleColorAdjust(&colors[i].blue);
}
// if we are generating new twinkles, randomly pick 1 new LEDs to light up
for (int i = 0; i < 1; i++)
{
int j = random(LED_COUNT);
colors[j] = (rgb_color){1, 1, 1}; // white
}
ledStrip.write(colors, LED_COUNT);
}
So the doGradient() function and the brightTwinkle() works perfectly if you try them individually, but i can't just make them works at the same time. My guess is that the loop of the gradient stops the fading every time it goes through the loop, starting all over.
Got to say i'm super begginer (and got a dubious english), so any help will be truly appreciate.
Many thanks !