How to replace delay() with Millis()

Hi there I am very new when it comes to coding and such. I can sort of take bits and pieces and make it work how i like it too but I have run into a problem with how to implement millis() instead of delay(). The following sketch is part of a bigger sketch but it is this part I would like to figure out as the rest is working well.
The only thing I can find is the Blink without delay function but looks to only work with single LEDs.

Thanks in advance for any help

#include <FastLED.h>

#define DATA_PIN 8
#define NUM_LEDS 20

CRGB leds[NUM_LEDS];

const int buttonPin2 = 2;
int buttonState2 = 0;


void setup() {

  pinMode(buttonPin2, INPUT);
  FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, NUM_LEDS);

}

void fadeall_1() {
  for (int i = 0; i < NUM_LEDS; i++) {
    leds[i].nscale8(250);
  }
}


void loop() {


  buttonState2 = digitalRead(buttonPin2);

  if (buttonState2 == HIGH) {


    for (int i = 0; i < NUM_LEDS; i++) {

      leds[i] = CRGB(255, 85, 0);
      FastLED.setBrightness (255);
      FastLED.show();
      fadeall_1();
      delay(20);
    }

    for (int i = 0; i < NUM_LEDS; i++) {

      leds[i] = CRGB(0, 0, 0);
      FastLED.show();
      fadeall_1();
      delay(20);
    }
  }
}

Demonstration code for several things at the same time Blinky. Blinky more then one led at different times.

And your code does not show any attempt and using millis(), show your best attempt and what went wrong or you are looking for someone just to write the code for you?

Here is what I got together. The best I can get is for it to flash a single pixel across the strip. Which is what I am half after. Tho I want the whole strip to light up and then wipe clear. Kinda like colour wipe but turning pixels off instead of another colour. Ive tried moving the show command and adding/subtracting curly braces but still no luck. So many different examples around but they dont fit to what I require. I have also removed the fadeall function as it is not needed now.

#include <FastLED.h>

#define DATA_PIN 8
#define NUM_LEDS 20

CRGB leds[NUM_LEDS];

const int buttonPin2 = 2;
int buttonState2 = 0;

const int interval = 20;
int currentMillis = 0;
void setup() {

  pinMode(buttonPin2, INPUT);
  FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, NUM_LEDS);

}

void loop() {

  buttonState2 = digitalRead(buttonPin2);

  if (buttonState2 == HIGH) {

      for (int i = 0; i < NUM_LEDS; i++) {

      leds[i] = CRGB(255, 85, 0);
      FastLED.setBrightness (255);
      FastLED.show();
      
   if(millis() - currentMillis >= interval) 
      currentMillis = millis();
      
      
      
      for (int i = 0; i < NUM_LEDS; i++) {

      leds[i] = CRGB(0, 0, 0);
      FastLED.show();
      
   if(millis() - currentMillis >= interval) 
      currentMillis = millis(); 
      
      
    
  }
}
}
}
int currentMillis = 0;

Change to.

uint32_t currentMillis = millis();

If using a Uno or Mega the millis needs unsigned longs to operate properly.

I use the Adafruit library, after setting all the pixels to 0,0,0 I call the show function to get the LEDS to turn off. Not sure what you'd do for fastled thingies.

millis() and for-loops are not the best of friends.
2)
you have a for-loop inside a for-loop; I doubt that that is the intention.

     if (millis() - currentMillis >= interval)
        currentMillis = millis();

Nothing wrong with that code but you need to do more inside the if

     if (millis() - currentMillis >= interval)
      {
        currentMillis = millis();

        // do something here; e.g. switch the next led on
        ...
        ...
      }

To switch the next led on, you need to get rid of the for-loop; you use a counter i, in below I’ve changed the name of the counter to index. The code will every interval switch the next led on on the strip; if all leds are on, it will switch them all off and start again. It does not use for-loops but lets loop() do the looping.
The static variable is used to keep track of the next led to switch on.

It’s meant as demonstration code to give you the idea of the use of millis(); not meant as a full solution.

void loop()
{
  static int16_t index = 0;

  if (millis() - currentMillis >= interval)
  {
    // 'reset' millis
    currentMillis = millis();

    if (index < NUM_LEDS)
    {
      leds[index] = CRGB(255, 85, 0);
      index++;
    }
    else
    {
      FastLED.clear();
      index = 0;
    }
    FastLED.show();
  }
}

Idahowalker:
Change to.

uint32_t currentMillis = millis();

And "interval" is also a long :grinning:

Thanks for the input so far. I will have a go at it and update if I am successful.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.