help ditching delay

hello all, i have a code which works just fine with delay() but as i want to add some other functions the delay becomes a problem. I have looked tru some sketches using millis() but i have not found a sketch to learn how i can go around the delay in my sketch.

does anyone have some pointers to look at, or is my sketch not possible with the use of millis

#include "FastLED.h"

// Number of leds in 1 strip
#define NUM_LEDS 83

DEFINE_GRADIENT_PALETTE( Sunrise_gp ) 
{

   51,  0,  0, 51,  // Midnight blue
  101,   0,   0, 150,  // Deepsky blue
  151,   10,   10, 212,  // Medium blue  
  201, 25, 25, 205,  // Lightsky blue
  241, 50, 50, 205,  // Lightsky blue
  255,  100,  100, 200}; // Daylight

CRGBPalette16 myPal = Sunrise_gp;

    // Define the array of leds
    CRGB ledstrip_1[NUM_LEDS];
    CRGB ledstrip_2[NUM_LEDS];
    CRGB ledstrip_3[NUM_LEDS];
    CRGB ledstrip_4[NUM_LEDS];

void setup() 
{
    FastLED.addLeds<WS2812B, 4, GRB>(ledstrip_1, NUM_LEDS);
    FastLED.addLeds<WS2812B, 5, GRB>(ledstrip_2, NUM_LEDS);
    FastLED.addLeds<WS2812B, 6, GRB>(ledstrip_3, NUM_LEDS);
    FastLED.addLeds<WS2812B, 7, GRB>(ledstrip_4, NUM_LEDS);
}

void loop()
{ 
  sunrise();
  
    FastLED.show();
}

void sunrise() {
  
  // total sunrise length, in minutes
  static const uint8_t sunriseLength = 3;

  // how often (in seconds) should the heat color increase?
  // for the default of 60 minutes, this should be about every 14 seconds
  // 14 seconds x 256 gradient steps = 3,584 seconds = ~60 minutes
  static const float interval = ((float)(sunriseLength * 60) / 256)*1000;

  // current gradient palette color index
  static uint8_t heatIndex = 0; // start out at 0

    
    // current used color palette
    CRGB color = ColorFromPalette(myPal, heatIndex);

    // fill the entire strip with the current color palette starting on 1 side
  for ( byte i = 0; i < NUM_LEDS; i++ ) {
    ledstrip_1[i] = CRGB(color);
    ledstrip_2[i] = CRGB(color);
    ledstrip_3[i] = CRGB(color);
    ledstrip_4[i] = CRGB(color);
    FastLED.delay(1000);

  // slowly increase the heat
  EVERY_N_MILLISECONDS(interval ) {
    // stop incrementing at 255, we don't want to overflow back to 0
    if(heatIndex < 255) {
      heatIndex++;
    }   
  }
}

Have you read Using millis() for timing. A beginners guide, Several things at the same time and looked at the BlinkWithoutDelay example in the IDE ?

i did, and i have tried to implement it into my sketch but i only could light up all the leds at once or only the first led of each strip. i dont know how to update the leds to start lighting up from 1 side but this is the last sketch i was working on before i stopped.

#include "FastLED.h"

// Number of leds in 1 strip
#define NUM_LEDS 83

unsigned long previousMillis = 0;        // will store last time LED was updated

// constants won't change :
const long interval = 500;           // interval at which to blink (milliseconds)

DEFINE_GRADIENT_PALETTE( Sunrise_gp ) 
{

   51,  0,  0, 51,  // Midnight blue
  101,   0,   50, 150,  // Deepsky blue
  151,   10,   75, 212,  // Medium blue  
  201, 25, 150, 205,  // Lightsky blue
  241, 50, 50, 205,  // Lightsky blue
  255,  100,  180, 200}; // Daylight

CRGBPalette16 myPal = Sunrise_gp;

    // Define the array of leds
    CRGB ledstrip_1[NUM_LEDS];
    CRGB ledstrip_2[NUM_LEDS];
    CRGB ledstrip_3[NUM_LEDS];
    CRGB ledstrip_4[NUM_LEDS];

void setup() 
{
    FastLED.addLeds<WS2812B, 4, GRB>(ledstrip_1, NUM_LEDS);
    FastLED.addLeds<WS2812B, 5, GRB>(ledstrip_2, NUM_LEDS);
    FastLED.addLeds<WS2812B, 6, GRB>(ledstrip_3, NUM_LEDS);
    FastLED.addLeds<WS2812B, 7, GRB>(ledstrip_4, NUM_LEDS);
}

void loop()
{ 
  sunrise();
  
    FastLED.show();
}

void sunrise() {
  
  // total sunrise length, in minutes
  static const uint8_t sunriseLength = 3;

  // how often (in seconds) should the heat color increase?
  // for the default of 60 minutes, this should be about every 14 seconds
  // 14 seconds x 256 gradient steps = 3,584 seconds = ~60 minutes
  static const float interval = ((float)(sunriseLength * 60) / 256)*1000;

  // current gradient palette color index
  static uint8_t heatIndex = 0; // start out at 0

    
    // current used color palette
    CRGB color = ColorFromPalette(myPal, heatIndex);

    // fill the entire strip with the current color palette starting on 1 side
    unsigned long currentMillis = millis();

  
    
  for ( byte i = 0; i < NUM_LEDS; i++ ) {
    if (currentMillis - previousMillis >= interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;
    ledstrip_1[i] = CRGB(color);
    ledstrip_2[i] = CRGB(color);
    ledstrip_3[i] = CRGB(color);
    ledstrip_4[i] = CRGB(color);
    

  // slowly increase the heat
  EVERY_N_MILLISECONDS(interval ) {
    // stop incrementing at 255, we don't want to overflow back to 0
    if(heatIndex < 255) {
      heatIndex++;
    }   
  }
}
  }
  }

never your with FastLED library so not sure what " FastLED.show();" is supposed to do, I thins something like thing may work… possibly with " FastLED.show();" inside "sunrise routine.

#include "FastLED.h"

// Number of leds in 1 strip
#define NUM_LEDS 83

unsigned long previousMillis = 0;        // will store last time LED was updated
uint8_t i = 0;

// constants won't change :
const long interval = 500;           // interval at which to blink (milliseconds)

DEFINE_GRADIENT_PALETTE( Sunrise_gp )
{

  51,  0,  0, 51,  // Midnight blue
  101,   0,   50, 150,  // Deepsky blue
  151,   10,   75, 212,  // Medium blue
  201, 25, 150, 205,  // Lightsky blue
  241, 50, 50, 205,  // Lightsky blue
  255,  100,  180, 200
}; // Daylight

CRGBPalette16 myPal = Sunrise_gp;

// Define the array of leds
CRGB ledstrip_1[NUM_LEDS];
CRGB ledstrip_2[NUM_LEDS];
CRGB ledstrip_3[NUM_LEDS];
CRGB ledstrip_4[NUM_LEDS];

void setup()
{
  FastLED.addLeds<WS2812B, 4, GRB>(ledstrip_1, NUM_LEDS);
  FastLED.addLeds<WS2812B, 5, GRB>(ledstrip_2, NUM_LEDS);
  FastLED.addLeds<WS2812B, 6, GRB>(ledstrip_3, NUM_LEDS);
  FastLED.addLeds<WS2812B, 7, GRB>(ledstrip_4, NUM_LEDS);
}

void loop()
{
  sunrise();

  FastLED.show();
}

void sunrise() {

  // total sunrise length, in minutes
  static const uint8_t sunriseLength = 3;

  // how often (in seconds) should the heat color increase?
  // for the default of 60 minutes, this should be about every 14 seconds
  // 14 seconds x 256 gradient steps = 3,584 seconds = ~60 minutes
  static const float interval = ((float)(sunriseLength * 60) / 256) * 1000;

  // current gradient palette color index
  static uint8_t heatIndex = 0; // start out at 0


  // current used color palette
  CRGB color = ColorFromPalette(myPal, heatIndex);

  // fill the entire strip with the current color palette starting on 1 side
  unsigned long currentMillis = millis();

  if (currentMillis - previousMillis >= interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;
    ledstrip_1[i] = CRGB(color);
    ledstrip_2[i] = CRGB(color);
    ledstrip_3[i] = CRGB(color);
    ledstrip_4[i] = CRGB(color);


    // slowly increase the heat
    EVERY_N_MILLISECONDS(interval ) {
      // stop incrementing at 255, we don't want to overflow back to 0
      if (heatIndex < 255) {
        heatIndex++;
      }
    }

    i = (i + 1) % NUM_LEDS;
  }

}

Hope that helps

You have a couple of things going on… If you use millis() for timing, you don’t need to use the EVERY_N_MILLISECONDS() function from the FastLED library. I would also suggest moving all the timing stuff into the main body of loop() rather than buring it inside sunrise(). Here’s a cleaned up version:

#include "FastLED.h"

// Number of leds in 1 strip
#define NUM_LEDS 83

unsigned long previousMillis = 0;        // will store last time LED was updated

// constants won't change :
//const unsigned long interval = 500;           // interval at which to blink (milliseconds)

DEFINE_GRADIENT_PALETTE( Sunrise_gp )
{

  51,  0,  0, 51,  // Midnight blue
  101,   0,   50, 150,  // Deepsky blue
  151,   10,   75, 212,  // Medium blue
  201, 25, 150, 205,  // Lightsky blue
  241, 50, 50, 205,  // Lightsky blue
  255,  100,  180, 200
}; // Daylight

CRGBPalette16 myPal = Sunrise_gp;

// Define the array of leds
CRGB ledstrip_1[NUM_LEDS];
CRGB ledstrip_2[NUM_LEDS];
CRGB ledstrip_3[NUM_LEDS];
CRGB ledstrip_4[NUM_LEDS];

// total sunrise length, in minutes
const uint8_t sunriseLength = 3;

// how often (in seconds) should the heat color increase?
// for the default of 60 minutes, this should be about every 14 seconds
// 14 seconds x 256 gradient steps = 3,584 seconds = ~60 minutes
const unsigned long interval = sunriseLength * 60000UL / 256; // milliseconds

// current gradient palette color index
uint8_t heatIndex = 0; // start out at 0

void setup()
{
  FastLED.addLeds<WS2812B, 4, GRB>(ledstrip_1, NUM_LEDS);
  FastLED.addLeds<WS2812B, 5, GRB>(ledstrip_2, NUM_LEDS);
  FastLED.addLeds<WS2812B, 6, GRB>(ledstrip_3, NUM_LEDS);
  FastLED.addLeds<WS2812B, 7, GRB>(ledstrip_4, NUM_LEDS);
}

void loop()
{
  unsigned long currentMillis = millis();

  if (currentMillis - previousMillis >= interval) {
    // time to update leds
    // save the last time you blinked the LED
    previousMillis = currentMillis;

    // stop incrementing at 255, we don't want to overflow back to 0
    if (heatIndex < 255) {
      heatIndex++;
    }
    sunrise();
    FastLED.show();
  }
}

void sunrise() {

  // current used color palette
  CRGB color = ColorFromPalette(myPal, heatIndex);

  // fill the entire strip with the current color palette starting on 1 side

  for ( byte i = 0; i < NUM_LEDS; i++ ) {
    //ledstrip_1[i] = CRGB(color);
    //ledstrip_2[i] = CRGB(color);
    //ledstrip_3[i] = CRGB(color);
    //ledstrip_4[i] = CRGB(color);
    ledstrip_1[i] = color;
    ledstrip_2[i] = color;
    ledstrip_3[i] = color;
    ledstrip_4[i] = color;
  }
}

Also note that you where filling in the ledstrip arrays incorrectly. You don’t want to transform the variable ‘color’ into a CRGB type since it already is.

@ sherzaad
the FastLED.show command is used to clear the leds and start the new lightprogram

@blh64
Thnx for the clean up, i am still a newbie so i thought my code would be a little messy:P
mostly its due to the fact that i have a existing code that works and tru trial and error i try to make the code work better and also learn in the proces.

I have tried your cleaned up version and it works, now i can do a side by side comparison to improve my cleanup skills, but it also lights up all the led at once.
i am trying to find a way to use millis to light up the leds starting on 1 side lighting 1 led after the other.
it did work fine with delay but that gives me problems in my bigger project which include a lcd which freezes when the sunrise starts

inside your sunrise() function, you iterate over all the LEDs and set them to a color.

  for ( byte i = 0; i < NUM_LEDS; i++ ) {
    //ledstrip_1[i] = CRGB(color);
    //ledstrip_2[i] = CRGB(color);
    //ledstrip_3[i] = CRGB(color);
    //ledstrip_4[i] = CRGB(color);
    ledstrip_1[i] = color;
    ledstrip_2[i] = color;
    ledstrip_3[i] = color;
    ledstrip_4[i] = color;
  }

Why would you expect that NOT to turn on all your LEDs? If you want to slowly increase the number of LEDs that are lit up, you need to figure out how many you want and then just assign to those and leave the rest off (black). Rather than using the heatIndex variable, it sounds like you need another variable that gets incremented from 1…NUM_LEDS.

Give it a shot, if it doesn’t work, post the code and ask for help.

I would expected that they would light up one after the other cause it did work fine with delay function.
The heatindex function tells the arduino which colors to show not which led to light up.
But i will try to give it a go, if it does not work i will post the sketch again

changed the for loop to a static int and now it works like a charm