FastLED Sequence

Hiya,

I’m trying to get an LED strip to perform a sequence using FastLED. My code currently looks like this.

#include "FastLED.h"


#define DATA_PIN    12       // change to your data pin
#define COLOR_ORDER GRB      // if colors are mismatched; change this
#define NUM_LEDS    40       // change to the number of LEDs in your strip
#define LED_TYPE    WS2812B

uint8_t max_bright = 255;                                     // Overall brightness.
int lead_led = 0;


CRGB leds[NUM_LEDS];

void setup()

{

  FastLED.addLeds<LED_TYPE, DATA_PIN, COLOR_ORDER>(leds, NUM_LEDS);

}


void loop() {

   white();
   blue();

}



void white(){  
for(int dot = 0; dot < NUM_LEDS; dot++)
  {
    leds[dot].fadeToBlackBy(120);
     
  }
  
  leds[lead_led]= CRGB::White;


  
  lead_led = lead_led + 1;
  if(lead_led == NUM_LEDS) { lead_led = 0; }
  
  FastLED.show();
  delay(15);
 
      

}


void blue(){  
for(int dot = 0; dot < NUM_LEDS; dot++)
  {
    leds[dot].fadeToBlackBy(120);
     
  }
  
  leds[lead_led]= CRGB::Blue;


  
  lead_led = lead_led + 1;
  if(lead_led == NUM_LEDS) { lead_led = 0; }
  
  FastLED.show();
  delay(15);
      

}

It’s doing both the white and blue sections at the same time and i need to seperate it so it runs the white section then the blue section. I’ll add more into it later but i guess that’s just a matter of rinse and repeat the method?

It's doing both the white and blue sections at the same time

No it is not, it is doing them one after the other very quickly. Add extra delays if you want to slow down things.

Seems like wherever i put more delays it just slows the run down and i’m still getting every 2nd LED blue

loop() runs over and over and over, so now you’re alternating white() and blue() over and over.
What you probably want to do is run white() a bunch of times and then run blue() that same number of times.

and i’m still getting every 2nd LED blue

Yes that is what you wrote the code to do. What exactly do you want to do?

Each call to the two functions reduces the brightness of all LEDs, and then changes ONE LED to be full brightness blue or white. As the variable lead_led is global and gets incremented only once per function call, every other function call alternate what LEDs will be what colours.

That'll be what's gone wrong then. So i want the chase to run in white then blue, not a combination of the 2.

So i want the chase to run in white then blue,

Can you explain what you mean by that?

Is this more like what you want to do? :-

void white(){ 
for(int lead_led; lead_led<NUM_LEDS; lead_led++){
for(int dot = 0; dot < NUM_LEDS; dot++){
    leds[dot].fadeToBlackBy(120);  
  }
  leds[lead_led]= CRGB::White; 
  FastLED.show();
  delay(15);
  }
}

So i’ve got the concept of flashing through the array in one colour but now. Once it’s flashed through once in white i then want it to flash in blue and alternate between the two colours.

So change the blue function to work like the white one I showed you.

Grumpy_Mike:
Is this more like what you want to do? :-

void white(){ 

for(int lead_led; lead_led<NUM_LEDS; lead_led++){
for(int dot = 0; dot < NUM_LEDS; dot++){
   leds[dot].fadeToBlackBy(120);  
 }
 leds[lead_led]= CRGB::White;
 FastLED.show();
 delay(15);
 }
}

Exactly that! Nice one

Just so i understand fully. The line that you added, can you just explain it?

The line that you added, can you just explain it?

It adds an outer loop so that all the LEDs are set to the colour in turn and not just one like you had.

Your code is very inefficient as the two functions are nearly the same. This tells you that their is a simpler way of doing things. So just pass into the function the colour you want to use, like this:-

#include "FastLED.h"

#define DATA_PIN    12       // change to your data pin
#define COLOR_ORDER GRB      // if colors are mismatched; change this
#define NUM_LEDS    40       // change to the number of LEDs in your strip
#define LED_TYPE    WS2812B

uint8_t max_bright = 255;                                     // Overall brightness.

CRGB leds[NUM_LEDS];

void setup() {
  FastLED.addLeds<LED_TYPE, DATA_PIN, COLOR_ORDER>(leds, NUM_LEDS);
}

void loop() {
   chase(CRGB::White);
   chase(CRGB::Blue);
}

void chase(CRGB col){ 
for(int lead_led; lead_led<NUM_LEDS; lead_led++){
for(int dot = 0; dot < NUM_LEDS; dot++){
    leds[dot].fadeToBlackBy(120);  
  }
  leds[lead_led]=col; 
  FastLED.show();
  delay(15);
  }
}

Oh aye, i get you. So could i do the same with the delay just have chase delay(15); ?

So could i do the same with the delay just have …

You can but not like that. You have to pass the delay value into the chase function as another variable.

void loop() {
   chase(CRGB::White,15);
   chase(CRGB::Blue,30);
}

void chase(CRGB col, int del){ 
for(int lead_led; lead_led<NUM_LEDS; lead_led++){
for(int dot = 0; dot < NUM_LEDS; dot++){
    leds[dot].fadeToBlackBy(120);  
  }
  leds[lead_led]=col; 
  FastLED.show();
  delay(del);
  }
}