arduinofastled

Arduino FastLED turn on leds in stages then flash the last segment


I would like to turn on segments of LEDs, first 4 followed by a delay then the next 4 LEDs followed by a delay then turn on the last 4 but have them flash at a smaller delay. when I tried this I get it to flash but the differences in delays does not make the flashing uniformed. I am new to aurdino and FastLEDs. here is the code that I was playing with.

first 4 turn on green

Delay 4000

next 4 turn on orange

Delay 4000

last 4 turn on red

Delay 400

turn off last 4

then it delays 4000 before turning on red again.

I would like the last 4 to turn on red then flash on and off at a delay of 400.

#include <FastLED.h>
#define LED_PIN 2
#define NUM_LEDS 15

CRGB leds[NUM_LEDS];


void setup() {
  FastLED.addLeds<WS2812, LED_PIN, GBR>(leds, NUM_LEDS);
  FastLED.setMaxPowerInVoltsAndMilliamps(5, 500);
  FastLED.clear();
  FastLED.show();

}
void loop() {

// GREEN


for (int i=0; i<4; i++){
  leds[i] = CRGB(0, 255, 0);
  FastLED.setBrightness(50+i);
  FastLED.show();
}

delay(4000);
// Orange
for (int i=4; i<8; i++){
  leds[i] = CRGB(0, 128, 255);
  
  FastLED.setBrightness(50+i);
  FastLED.show();
}

// RED 
delay(4000);
for (int i=8; i<12; i++){
    leds[i] = CRGB(0, 0, 255);
    FastLED.setBrightness(50+i);
    FastLED.show();

}
delay(400);
for (int i=8; i<12; i++){
    leds[i] = CRGB::Black;
    FastLED.setBrightness(50+i);
    FastLED.show();

}

}

Solution

  • It's a good practice to split code into separates functions to make it more readable. Then to flash your LEDs you have to turn them off and on in a dedicated loop. I also added a complete extinction at the end of the cycle. Try this code:

    #include <FastLED.h>
    #define LED_PIN 2
    #define NUM_LEDS 15
    
    CRGB leds[NUM_LEDS];
    
    
    void setup() {
      FastLED.addLeds<WS2812, LED_PIN, GBR>(leds, NUM_LEDS);
      FastLED.setMaxPowerInVoltsAndMilliamps(5, 500);
      FastLED.clear();
      FastLED.show();
    }
    
    void loop() {
    
      green();
      delay(4000);
      orange();
      delay(4000);
      red();
      delay(4000);
    
      for (int k = 0; k < 4; k++) { //flash 4 times
        redOff();
        delay(400);
        red();
        delay(400);
      }
    
      allOff(); //switch off all leds and loop
    }
    
    
    void green() {
      for (int i = 0; i < 4; i++) {
        leds[i] = CRGB(0, 255, 0);
        FastLED.setBrightness(50 + i);
        FastLED.show();
      }
    }
    
    void orange() {
      for (int i = 4; i < 8; i++) {
        leds[i] = CRGB(0, 128, 255);
        FastLED.setBrightness(50 + i);
        FastLED.show();
      }
    }
    
    void red() {
      delay(4000);
      for (int i = 8; i < 12; i++) {
        leds[i] = CRGB(0, 0, 255);
        FastLED.setBrightness(50 + i);
        FastLED.show();
      }
    }
    
    void redOff() {
      for (int i = 8; i < 12; i++) {
        leds[i] = CRGB::Black;
        FastLED.setBrightness(50 + i);
        FastLED.show();
      }
    }
    
    
    void allOff() {
      for (int i = 0; i < 12; i++) {
        leds[i] = CRGB::Black;
        FastLED.setBrightness(50 + i);
        FastLED.show();
      }
    }