Using millis () to turn off LEDs in FastLED

I am new to coding, and have written my first bit of code. It works, but there are issues I am having which I cannot conceive how to overcome.

My main issue is this: I am turning on groups of LED's on a WS2812b LED strip, triggered by readings from an ultrasonic sensor, but I don't know how to use millis () properly to turn them off in groups. I would like the LED's to stay on for 15 seconds after being activated and then turn off, however my use of millis () only turns off all the LED's every 15 seconds, with any LED's currently being triggered staying on.

It 'works' but not in the way I envisioned the project. If anyone could shed some light on how to create multiple millis loops in the FastLED library, or an alternative structure that might achieve the same result, I would be very grateful. Apologies for any incorrect formatting, this is my first post/code. The section in bold is the part that I have having trouble with adapting.

Any general coding advice is also very welcome.

/*  Ultrasonic LED strip project

    This sketch uses an ultrasonic sensor to activate an LED on a 1m 60-LED strip.
    The LEDS light up depending on the distance of the object detected by the
    ultrasonic seonsor. As the object moves, the illuminated LED changes
*/

//  LED LIGHTS SECTION
#include <FastLED.h>  
#define NUM_LEDS 60  
#define DATA_PIN 5    
#define COLOR_ORDER GRB    
#define LED_TYPE WS2812B   
#define BRIGHTNESS 30 

CRGB leds[NUM_LEDS];  

const int cascade = 15;  // Regulates the speed that the lights turn on when arranged in a run
const int setColour = CRGB::Blue;   // Sets the colour of the LEDs

unsigned long currentMillis = 0;    // Stores the value of millis() in each iteration of the loop()

unsigned long previousMillis = 0;          // Stores the last time the LED was triggered

const long interval = 15000;   // Interval at which to delay turning off LEDs

// ULTRASONIC SENSOR SECTION
const int trigPin = 7;   
const int echoPin = 8;   

long duration;  //  create 'duration' int to store ping duration
int distance;   //  create 'distance' int to trigger LED

void setup() {

  // ULTRASONIC SENSOR SECTION
  pinMode(trigPin, OUTPUT);  
  pinMode(echoPin, INPUT);  

  //  LED LIGHTS SECTION
  delay(2000);    
  FastLED.addLeds<WS2812B, DATA_PIN, RGB>(leds, NUM_LEDS);
  FastLED.addLeds<LED_TYPE, DATA_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
  FastLED.setBrightness(BRIGHTNESS);     

  Serial.begin(9600);   
}

void loop() {

  //  ULTRASONIC SENSOR SECTION
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);   
  digitalWrite(trigPin, LOW);

  duration = pulseIn(echoPin, HIGH);
  distance = duration / 2.915 / 2;     // convert the time into distance (mm)

  Serial.println(distance); 

  // LED LIGHTS SECTION  
[b]  // Attempt to introduce millis() function
  // DOESNT WORK PROPERLY... THIS REPEATEDLY TURNS OFF LEDS EVERY 15 SECONDS (int interval duration)
  unsigned long currentMillis = millis();
  
  if (currentMillis - previousMillis >= interval)
  {
    previousMillis = currentMillis;
  
  FastLED.clear();            // clear the LEDs[/b]
  }

  if (distance > 570)       // when an object is detected over 57cm away
  {
    leds[35] = setColour;  // set the led to the colour selected at the top
    FastLED.show();         // turn it on
  }

  if ((distance <= 580) && (distance > 450))
  {
    leds[34] = setColour;
    FastLED.show();
    delay(cascade);
      leds[33] = setColour;
      FastLED.show();
      delay(cascade);
        leds[32] = setColour;
        FastLED.show();
        delay(cascade);
          leds[31] = setColour;
          FastLED.show();
          delay(cascade);
            leds[30] = setColour;
            FastLED.show();
            delay(cascade);
              leds[29] = setColour;
              FastLED.show();
              delay(cascade);
                leds[28] = setColour;
                FastLED.show();
                delay(cascade);
                  leds[27] = setColour;
                  FastLED.show();
                  delay(cascade);
                    leds[26] = setColour;
                    FastLED.show();
                    delay(cascade);
                      leds[25] = setColour;
                      FastLED.show();
                      delay(cascade);
                        leds[24] = setColour;
                        FastLED.show();
                        delay(cascade);
                          leds[23] = setColour;
                          FastLED.show();
                          delay(cascade);
                            leds[22] = setColour;
                            FastLED.show();
                            delay(cascade);
                              leds[21] = setColour;
                              FastLED.show();
                              delay(cascade);
                                leds[20] = setColour;
                                FastLED.show();
                                delay(cascade);
                                  leds[19] = setColour;
                                  FastLED.show();
                                  delay(cascade);
                                    leds[18] = setColour;
                                    FastLED.show();
  }

  if ((distance <= 480) && (distance > 400))
  {
    leds[35] = setColour;
    leds[34] = setColour;
    leds[33] = setColour;
    leds[32] = setColour;
    leds[31] = setColour;
    leds[30] = setColour;
    FastLED.show();
  }
  
  if ((distance <= 500) && (distance > 300))
  {
    leds[29] = setColour;
    leds[28] = setColour;
    leds[27] = setColour;
    leds[26] = setColour;
    leds[25] = setColour;
    leds[24] = setColour;
    FastLED.show();
  }

  if ((distance <= 400) && (distance > 200))
  {
    leds[23] = setColour;
    leds[22] = setColour;
    leds[21] = setColour;
    leds[20] = setColour;
    leds[19] = setColour;
    leds[18] = setColour;
    FastLED.show();
  }
  
  if ((distance <= 430) && (distance > 150))
  {
    leds[17] = setColour;
    leds[16] = setColour;
    leds[15] = setColour;
    leds[14] = setColour;
    leds[13] = setColour;
    leds[12] = setColour;
    FastLED.show();
  }

  if ((distance <= 300) && (distance > 150))
  {
    leds[11] = setColour;
    leds[10] = setColour;
    leds[9] = setColour;
    leds[8] = setColour;
    leds[7] = setColour;
    leds[6] = setColour;
    FastLED.show();
  }

  if ((distance <= 200) && (distance > 120))
  {
    leds[5] = setColour;
    leds[4] = setColour;
    leds[3] = setColour;
    leds[2] = setColour;
    leds[1] = setColour;
    leds[0] = setColour;
    FastLED.show();
  }

  if ((distance <= 100) && (distance > 0))
  {
    leds[0] = setColour;
    FastLED.show();
    delay(cascade);
      leds[1] = setColour;
      FastLED.show();
      delay(cascade);
        leds[2] = setColour;
        FastLED.show();
        delay(cascade);
          leds[3] = setColour;
          FastLED.show();
          delay(cascade);
            leds[4] = setColour;
            FastLED.show();
            delay(cascade);
              leds[5] = setColour;
              FastLED.show();
              delay(cascade);
                leds[6] = setColour;
                FastLED.show();
                delay(cascade);
                  leds[7] = setColour;
                  FastLED.show();
                  delay(cascade);
                    leds[8] = setColour;
                    FastLED.show();
                    delay(cascade);
                      leds[9] = setColour;
                      FastLED.show();
                      delay(cascade);
                        leds[10] = setColour;
                        FastLED.show();
                        delay(cascade);
                          leds[11] = setColour;
                          FastLED.show();
  }
}

Thanks for your time,

Andy

You need to declare your LED's in groups where each group consists of index, led-count, distance to trigger, state and the time (millis) they where triggered. When this is done, you can iterate the groups much easier - consider something like:

struct LED_GROUP {
  byte index, count;
  int triggerNear, triggerFar;
  bool on;
  unsigned long triggerMillis;
};

Declare a const array of something like the above and you're on your way :slight_smile:

Thanks for the tip! I will give it a go, it will take me a while but I will let you know how it goes... thanks again