FastLED without delay()?

Hello all,

I’m trying to light my LED strip while running other stuff, unfortunately the example below uses the delay() function which is interrupting the motor movement.

#include <FastLED.h>
#define NUM_LEDS 6
#define DATA_PIN 5
CRGB leds[NUM_LEDS];

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

void loop()
{
  for(int x = 0; x < NUM_LEDS; x++)
  {
    leds[x].setRGB(255,69,0);
    FastLED.show();
    delay (50); //This is what I need to replace
    leds[x] = CRGB::Black;
  }
}

I can’t figure how to use millis() in my case:

unsigned long startMillis;
unsigned long currentMillis;
const unsigned long period = 1000;
const byte ledPin = 13;

void setup()
{
  pinMode(ledPin, OUTPUT);
  startMillis = millis();
}

void loop()
{
  currentMillis = millis();
  if (currentMillis - startMillis >= period)
  {
    digitalWrite(ledPin, !digitalRead(ledPin));
    startMillis = currentMillis;
  }
}

Any thoughts?

Thanks,

aminabudahab:
I can't figure how to use millis() in my case:

Your millis() code looks fine. What is the problem?

The demo Several Things at a Time illustrates the use of millis() to manage timing without blocking. It may help with understanding the technique.

Have a look at Using millis() for timing. A beginners guide if you need more explanation.

...R

You cannot use a "for" loop. You should use a global instance of "x", something like this:

int x = 0;

void loop() {
  currentMillis = millis();
  if (currentMillis - startMillis >= period) {
    startMillis = currentMillis;
    leds[x].setRGB(255,69,0);
    FastLED.show();
    leds[x] = CRGB::Black;
    if (++x >= NUM_LEDS) x = 0;
  }
  //Other stuff here..
}

Danois90:
You cannot use a "for" loop. You should use a global instance of "x", something like this:

int x = 0;

void loop() {
  currentMillis = millis();
  if (currentMillis - startMillis >= period) {
    startMillis = currentMillis;
    leds.setRGB(255,69,0);
    FastLED.show();
    leds = CRGB::Black;
    if (++x >= NUM_LEDS) x = 0;
  }
  //Other stuff here,,
}

That worked perfectly, been trying to wrap my head around it for 2 days now. I wouldn't have thought that I can't use for loops.

Thanks a lot :smiley:

Remember that FastLED replaces delay() with a busy wait...