OK, try this ... it took me a bit to make something that can handle more than 1 LED simultaneous in all possible random ways.
Color palette (ie the colors we randomly choose from are defined here (3 in this example):
CRGB palette[NUMCOLORS]= { CRGB(0xff,0,0), CRGB(0,0xff,0), CRGB(0,0,0xff) };
In the InitLED() function you can play a little with the values, but take the wrong values and things might not work as desired.
That's also the reason why I commented out all the Serial message (begin, print and println). You can uncomment them if you'd like to track what is going on.
Random activation moment:
LEDStatus[led].active = (random(60)<50);
Make the number 60 higher and it becomes less likely that a LED will light up.
Brightness can be limited with the maxvalue (chooses a value between 16 and 128):
LEDStatus[led].maxvalue = random(16,128);
and max darkness (16 is not visible on my LED strip and is convenient with the if <= comparison:
LEDStatus[led].minvalue = 16;
The stepsize determines how fast a fade goes, higher number means faster, max value is 256 (instant on/off).
Zero would mean no fading, so we have to avoid that, hence the constant "MINSTEPSIZE".
LEDStatus[led].stepsize = MINSTEPSIZE+random(16);
Here is the full code ...
#include <FastLED.h>
#define NUMLEDS 3 // number of LEDs we want to work with
#define NUMCOLORS 3 // number of colors we'd like to define
#define LEDPIN 5 // The Arduino pin we connect our LEDs Din to
#define MINSTEPSIZE 4 // We need to set a minimum stepsize, >0 !!
struct ledstatus {
CRGB colorBase; // The color at it's brightest setting
boolean active; // is fading in/out or not (=off)
boolean isFading; // is in the progress of fading (true) or brightening (false) if active
byte maxvalue; // max brightness value (0-256, 256=brightest)
byte minvalue; // min brightness value (0-256, 0=black)
byte currentvalue; // current brightness tracking (0-256)
byte stepsize; // fade step size (0-256, 5 = 5/256th)
};
ledstatus LEDStatus[NUMLEDS];
CRGB palette[NUMCOLORS]= { CRGB(0xff,0,0), CRGB(0,0xff,0), CRGB(0,0,0xff) };
CRGB leds[NUMLEDS];
void setup() {
FastLED.addLeds<NEOPIXEL, LEDPIN>(leds, NUMLEDS);
randomSeed(analogRead(0));
// Serial.begin(9600);
for(int counter; counter<NUMLEDS; counter++) {
InitLED(counter);
}
}
void loop() {
EVERY_N_MILLIS(50) // do this only every x milliseconds
{
for(int counter; counter<NUMLEDS; counter++) // prep all LEDs
{
if(LEDStatus[counter].active) // if LED active, act on fade in or out
{ // LED is active - is it fading or brightening?
if(LEDStatus[counter].isFading) // -> Fading
{
// Serial.print(counter);
// Serial.println(" LED is Fading");
leds[counter].nscale8(LEDStatus[counter].currentvalue);
LEDStatus[counter].currentvalue=LEDStatus[counter].currentvalue-LEDStatus[counter].stepsize;
if(LEDStatus[counter].currentvalue<=LEDStatus[counter].minvalue) // we reached the lowest value
{
//LEDStatus[counter].active=false;
InitLED(counter);
leds[counter] = CRGB::Black;
}
}
else
{ // -> brightening
// Serial.print(counter);
// Serial.println(" LED is Brightening");
LEDStatus[counter].currentvalue=LEDStatus[counter].currentvalue+LEDStatus[counter].stepsize;
leds[counter]=LEDStatus[counter].colorBase;
leds[counter].nscale8(LEDStatus[counter].currentvalue);
if(LEDStatus[counter].currentvalue>=LEDStatus[counter].maxvalue) // we reached the highest value
{
LEDStatus[counter].isFading = true; // start fading in next cycle
LEDStatus[counter].stepsize = MINSTEPSIZE+random(8); // make fade out different from fade in speed
}
}
}
else // led not active, set to black and guess new color and random activation
{
// Serial.print(counter);
// Serial.println(" LED is inactive");
leds[counter] = CRGB::Black;
InitLED(counter);
}
}
FastLED.show();
} // EVERY_N_MILLIS
}
void InitLED(int led) {
// Serial.print(led);
// Serial.println(" LED INIT");
LEDStatus[led].active = (random(60)<50);
LEDStatus[led].colorBase = palette[random(NUMCOLORS)];
LEDStatus[led].isFading = false;
LEDStatus[led].maxvalue = random(16,128);
LEDStatus[led].minvalue = 16;
LEDStatus[led].currentvalue = LEDStatus[led].minvalue;
LEDStatus[led].stepsize = MINSTEPSIZE+random(16);
}
I'll post a little video after this code section ...