arduinoledarduino-c++fastled

FastLED blinking - chaning color using millis() without delay


I am trying to blink my led strip without using a delay()

So far i have somethin like this:

#include <FastLED.h>

#define NUM_LEDS 60
#define BRIGHTNESS  32
#define LED_TYPE    WS2811
#define DATA_PIN 6

CRGB leds[NUM_LEDS];
unsigned long actTime = 0;
unsigned long remTime = 0;
const unsigned long period = 1000;


void setup() { 
      FastLED.setMaxPowerInVoltsAndMilliamps(5,1000);
      FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
      FastLED.setBrightness(BRIGHTNESS);
}

void loop() {
  actTime = millis();
  if (actTime - remTime >= period){
    remTime = actTime;
    fill_solid(leds, NUM_LEDS, CRGB::Red);
    FastLED.show();
    fill_solid(leds, NUM_LEDS, CRGB::Black);
    FastLED.show();
  }
}

But it is not working like i want it to. For example if i change fill_solid(leds, NUM_LEDS, CRGB::Black); to CRGB::Green i will only see green color and hard to see red blinking. I want to make it look like this for example: green for 1s -> red for 1s -> green etc.

How my loop should look like?


Solution

  • Your timing code is correct, but it does the wrong thing every second--it sets the color to red, shows it, and then immediately sets the color to black and shows it. Add an additional state variable to track the current color:

    bool is_red = false;
    
    void loop() {
      actTime = millis();
      if (actTime - remTime >= period){
        remTime = actTime;
        if(!is_red) {
            is_red = true;
            fill_solid(leds, NUM_LEDS, CRGB::Red);
            FastLED.show();
        } else {
            is_red = false;
            fill_solid(leds, NUM_LEDS, CRGB::Black);
            FastLED.show();
        }
      }
    }