FastLED (WS2812B) - Fading in a sequence, one LED at a time

Hi all. I'm trying to get a WS2812B LED strip to fade in a sequence of LEDs, but fade in just one at a time and overlapping timing, if possible. I can get the LEDs to turn on in sequence and I can get them all to fade in and out at the same time, but I can't figure out how to combine the two.

Here's the current code I'm working with. The problem is that they fade in incrementally in sequence. What I mean is that they'll all sequentially get set to x brightness, then they'll all get set to x+1, then all to x+2, etc. I don't mind that on the fade out, but it's the fade in that's problematic. What am I doing wrong? Thanks in advance.

#include <FastLED.h>
#define LED_PIN     3
#define NUM_LEDS    7
CRGB leds[NUM_LEDS];

int fadeAmount = 15;
int brightness = 0;

void setup() {
  FastLED.addLeds<WS2812B, LED_PIN, GRB>(leds, NUM_LEDS);
}

void loop() {

  brightness = brightness + fadeAmount;
  // reverse the direction of the fading at the ends of the fade: 
  if(brightness == 0 || brightness == 255)
  {
    fadeAmount = -fadeAmount ; 
  }
    
  for(int i=0;i<1;i++){
    leds[i] = CRGB(0x006400);
    leds[i].fadeLightBy(brightness);
    FastLED.show();
    delay(100);
  }
  
  for(int i=1;i<2;i++){
    leds[i] = CRGB(0x006400);
    leds[i].fadeLightBy(brightness);
    FastLED.show();
    delay(100);
  }

  for(int i=2;i<3;i++){
    leds[i] = CRGB(0x006400);
    leds[i].fadeLightBy(brightness);
    FastLED.show();
    delay(100);
  }

  for(int i=3;i<4;i++){
    leds[i] = CRGB(0x006400);
    leds[i].fadeLightBy(brightness);
    FastLED.show();
    delay(100);
  }

  for(int i=4;i<5;i++){
    leds[i] = CRGB(0xFF0000);
    leds[i].fadeLightBy(brightness);
    FastLED.show();
    delay(100);
  }

  for(int i=5;i<6;i++){
    leds[i] = CRGB(0xFF0000);
    leds[i].fadeLightBy(brightness);
    FastLED.show();
    delay(100);
  }

  for(int i=6;i<7;i++){
    leds[i] = CRGB(0xFFD700);
    leds[i].fadeLightBy(brightness);
    FastLED.show();
    delay(100);
  }
  
}

double check what fadeLightBy() does. It might fade every led down

what are the for loops for? you have only 1 valid index in there...

it's the same as if you were doing

void loop() {

  brightness = brightness + fadeAmount;
  // reverse the direction of the fading at the ends of the fade: 
  if (brightness == 0 || brightness == 255)  fadeAmount = -fadeAmount ; 

   for(int i=0;i<7;i++){
     leds[i] = CRGB(0x006400);
     leds[i].fadeLightBy(brightness);
     FastLED.show();
     delay(100);
   }
}

You could use an HSV representation of your color and fade in or out by changing the V.

PS/
(you code works because 255 can be divided by 15, instead of testing for equality you should test for > or < and of course not go beyond 255 or negative)

Hi @majhi
Is this your need?

RV mineirin

#include <FastLED.h>
#define LED_PIN     3
#define NUM_LEDS    7
CRGB leds[NUM_LEDS];
int Steps = 16;
int fadeAmount = -(256 / Steps); // 256 / Steps
int brightness = 0;
byte factor = 0;
int myTime  = 70;
int myTime2  = 50;
//----------------------------------------------------------------------------------
void LEDcontrol(int i) {
  for (int j = 0; j < Steps; j++)
  {
    if (i == 0 or i == 1 or i == 2 or i == 3) leds[i] = CRGB(0x006400);
    if (i == 4 or i == 5) leds[i] = CRGB(0xFF0000);
    if (i == 6)  leds[i] = CRGB(0xFFD700);
    if (i > 6)   leds[i] = CRGB(0xFFD700);
    brightness = brightness + fadeAmount;
    leds[i].fadeLightBy(brightness);
    FastLED.show();
    delay(myTime);
  }
}
//----------------------------------------------------------------------------------
void setup() {
  FastLED.addLeds<WS2812B, LED_PIN, GRB>(leds, NUM_LEDS);
}
//----------------------------------------------------------------------------------
void loop() {

  for (int i = 0; i < NUM_LEDS; i++)
  {
    brightness = factor;
    LEDcontrol(i);
    delay(myTime2);
  }
  fadeAmount = -fadeAmount ;
  factor = factor ^ 0xFF ;
}
1 Like

@ruilviana That works perfectly, thank you!! I have a couple questions regarding your code so I can learn from this. What does "byte factor" do? Also I see that brightness (int) will be equal to factor (byte) in loop. What sets an int and byte apart in storing a value?

Hi
The factor variable resets the brightness value to 0 or 255,
depending on the fade direction.

The byte type is unsigned and can contain values from 0 to 255.
The int type is signed and can contain values from 32,767 through -32,767.

RV mineirin

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.