# FastLED without delay()?

**URL:** <https://forum.arduino.cc/t/fastled-without-delay/516550>\
**Category:** Programming\
**Created:** [March 26, 2018, 5:17pm UTC](https://forum.arduino.cc/t/fastled-without-delay/516550 "2018-03-26T17:17:48Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![aminabudahab](https://avatars.discourse-cdn.com/v4/letter/a/f4b2a3/32.png) [@aminabudahab](https://forum.arduino.cc/u/aminabudahab)\
**Post date:** [March 26, 2018, 5:17pm UTC](https://forum.arduino.cc/t/fastled-without-delay/516550/1 "2018-03-26T17:17:48Z")

</div>

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.

```cpp
#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:

```cpp
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,

---

<div class="post-metadata">

**Author:** ![Robin2](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/robin2/32/216045_2.png) [@Robin2](https://forum.arduino.cc/u/Robin2)\
**Post date:** [March 26, 2018, 5:20pm UTC](https://forum.arduino.cc/t/fastled-without-delay/516550/2 "2018-03-26T17:20:32Z")

</div>

> 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](http://forum.arduino.cc/index.php?topic=223286.0) 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](http://forum.arduino.cc/index.php?topic=503368.0) if you need more explanation.

...R

---

<div class="post-metadata">

**Author:** ![Danois90](https://avatars.discourse-cdn.com/v4/letter/d/50afbb/32.png) [@Danois90](https://forum.arduino.cc/u/Danois90)\
**Post date:** [March 26, 2018, 5:24pm UTC](https://forum.arduino.cc/t/fastled-without-delay/516550/3 "2018-03-26T17:24:16Z")

</div>

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

```cpp
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..
}

```

---

<div class="post-metadata">

**Author:** ![aminabudahab](https://avatars.discourse-cdn.com/v4/letter/a/f4b2a3/32.png) [@aminabudahab](https://forum.arduino.cc/u/aminabudahab)\
**Post date:** [March 26, 2018, 5:31pm UTC](https://forum.arduino.cc/t/fastled-without-delay/516550/4 "2018-03-26T17:31:17Z")

</div>

> Danois90:  
> You cannot use a "for" loop. You should use a global instance of "x", something like this:
> 
> ```arduino
> int x = 0;
> 
> ```

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

> ```arduino
> 
> ```

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 😃

---

<div class="post-metadata">

**Author:** ![davepl](https://avatars.discourse-cdn.com/v4/letter/d/b77776/32.png) [@davepl](https://forum.arduino.cc/u/davepl)\
**Post date:** [April 30, 2019, 2:17pm UTC](https://forum.arduino.cc/t/fastled-without-delay/516550/5 "2019-04-30T14:17:32Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 5, 2021, 11:05pm UTC](https://forum.arduino.cc/t/fastled-without-delay/516550/6 "2021-05-05T23:05:45Z")

</div>

Closed on May 5, 2021, 11:05pm UTC


