Resetting interaction after set amount of time

Hello,

I'm looking for some help with a project I'm working on. With the help of a user on this forum I was able to come up with code at the bottom of this message.

Basically, every time a motion is sensed by the sensor, one of five led strips lights up. The ones that were already on will stay on, until all 5 led strips are on. This is exactly how I want it.

The thing I'd like to implement is some kind of reset after the fifth LED strip has been switched on. Is it possible to turn all led strips off, 2 minutes after they've all been switched on and restart the interaction at "case 0"?

If so, how would this be implemented in the code? I'd really appreciate it if someone could help me.

#include <Adafruit_NeoPixel.h>

#define ECHOPIN 13        // Pin to receive echo pulse 
#define TRIGPIN 12        // Pin to send trigger pulse

#define LED_STRIP_1 3
#define LED_STRIP_2 4
#define LED_STRIP_3 5
#define LED_STRIP_4 6
#define LED_STRIP_5 7

#define NUMPIXELS 34

#define RESET_ALL 60000 //60s
#define DELAY 100 //150ms between each LED operation

// Parameter 1 = number of pixels in strip
// Parameter 2 = pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
//   NEO_KHZ800  800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
//   NEO_KHZ400  400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
//   NEO_GRB     Pixels are wired for GRB bitstream (most NeoPixel products)
//   NEO_RGB     Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
Adafruit_NeoPixel led_strip_1 = Adafruit_NeoPixel(NUMPIXELS, LED_STRIP_1, NEO_GRB + NEO_KHZ800);
Adafruit_NeoPixel led_strip_2 = Adafruit_NeoPixel(NUMPIXELS, LED_STRIP_2, NEO_GRB + NEO_KHZ800);
Adafruit_NeoPixel led_strip_3 = Adafruit_NeoPixel(NUMPIXELS, LED_STRIP_3, NEO_GRB + NEO_KHZ800);
Adafruit_NeoPixel led_strip_4 = Adafruit_NeoPixel(NUMPIXELS, LED_STRIP_4, NEO_GRB + NEO_KHZ800);
Adafruit_NeoPixel led_strip_5 = Adafruit_NeoPixel(NUMPIXELS, LED_STRIP_5, NEO_GRB + NEO_KHZ800);

 
float distance=0;
uint8_t leds[] = {3,4,5,6,7};
uint8_t a = 0, i=0, j=0, k=0, l=0, m=0;
unsigned long oldtime, delay_val;

void setup() 
{ 
  Serial.begin(9600); 
  pinMode(ECHOPIN, INPUT); 
  pinMode(TRIGPIN, OUTPUT); 

  
  led_strip_1.begin();
  led_strip_1.show(); // Initialize all pixels to 'off'
  
  led_strip_2.begin();
  led_strip_2.show(); // Initialize all pixels to 'off'
  
  led_strip_3.begin();
  led_strip_3.show(); // Initialize all pixels to 'off'

  led_strip_4.begin();
  led_strip_4.show(); // Initialize all pixels to 'off'

  led_strip_5.begin();
  led_strip_5.show(); // Initialize all pixels to 'off'
  
  oldtime = millis();
  delay_val = millis();
}
void loop() 
{  
  // Start Ranging -Generating a trigger of 10us burst 
  digitalWrite(TRIGPIN, LOW); 
  delayMicroseconds(2); 
  digitalWrite(TRIGPIN, HIGH); 
  delayMicroseconds(10); 
  digitalWrite(TRIGPIN, LOW);
  // Distance Calculation
  
  distance = pulseIn(ECHOPIN, HIGH); 
   /* The speed of sound is 340 m/s or 29 us per cm.The Ultrasonic burst travels out & back.So to 
find the distance of object we divide by 58  */
  distance = distance/58; 
  
   if (distance <= 18) {
    oldtime = millis();
    delay(1000);
    Serial.print(distance);
    Serial.println(" cm");
 
    if(a < 5) a++; ;
  }
  
  if(millis() - oldtime > RESET_ALL) a = 0;
  
  if(millis() - delay_val > DELAY){
    switch(a){
    //switch all LED strips off
    case 0:
      led_strip_1.setPixelColor(i, led_strip_1.Color(0,0,0));
      led_strip_1.show(); 
      i = (i+0)%NUMPIXELS;
      led_strip_2.setPixelColor(j, led_strip_2.Color(0,0,0));
      led_strip_2.show(); 
      j = (j+0)%NUMPIXELS;
      led_strip_3.setPixelColor(k, led_strip_3.Color(0,0,0));
      led_strip_3.show(); 
      k = (k+0)%NUMPIXELS;
      led_strip_4.setPixelColor(l, led_strip_4.Color(0,0,0));
      led_strip_4.show(); 
      l = (l+0)%NUMPIXELS;
      led_strip_5.setPixelColor(m, led_strip_5.Color(0,0,0));
      led_strip_5.show(); 
      m = (m+0)%NUMPIXELS;
    break;
    
    //turn 1st strip on
    case 1:
      led_strip_1.setPixelColor(i, led_strip_1.Color(0,140,255)); // Moderately bright blue color.
      led_strip_1.show(); 
      i = (i+1)%NUMPIXELS;   
      delay_val=millis();
    break;
    
    //turn 1st and second strip on
    case 2:
      led_strip_1.setPixelColor(i, led_strip_1.Color(0,140,255)); // Moderately bright blue color.
      led_strip_1.show(); 
      i = (i+1)%NUMPIXELS;  
      led_strip_2.setPixelColor(j, led_strip_2.Color(255,150,0)); // Moderately bright orange color.
      led_strip_2.show(); 
      j = (j+1)%NUMPIXELS;   
      delay_val=millis();
    break;
    
    //turn 1st,2ND AND 3RD strip on
    case 3:
      led_strip_1.setPixelColor(i, led_strip_1.Color(0,140,255)); // Moderately bright blue color.
      led_strip_1.show(); 
      i = (i+1)%NUMPIXELS;  
      led_strip_2.setPixelColor(j, led_strip_2.Color(255,150,0)); // Moderately bright orange color.
      led_strip_2.show(); 
      j = (j+1)%NUMPIXELS;  
      led_strip_3.setPixelColor(k, led_strip_3.Color(0,255,0)); // Moderately bright green color.
      led_strip_3.show(); 
      k = (k+1)%NUMPIXELS;      
      delay_val=millis();
    break;

      case 4:
        led_strip_1.setPixelColor(i, led_strip_1.Color(0,140,255)); // Moderately bright green color.
      led_strip_1.show(); 
      i = (i+1)%NUMPIXELS;  
      led_strip_2.setPixelColor(j, led_strip_2.Color(255,150,0)); // Moderately bright orange color.
      led_strip_2.show(); 
      j = (j+1)%NUMPIXELS;  
      led_strip_3.setPixelColor(k, led_strip_3.Color(0,255,0)); // Moderately bright green color.
      led_strip_3.show(); 
      k = (k+1)%NUMPIXELS;
      led_strip_4.setPixelColor(l, led_strip_4.Color(0,255,0)); // Moderately bright orange color.
      led_strip_4.show(); 
      l = (l+1)%NUMPIXELS;      
      delay_val=millis();
    break;

      case 5:
      led_strip_1.setPixelColor(i, led_strip_1.Color(0,140,255)); // Moderately bright green color.
      led_strip_1.show(); 
      i = (i+1)%NUMPIXELS;  
      led_strip_2.setPixelColor(j, led_strip_2.Color(255,150,0)); // Moderately bright green color.
      led_strip_2.show(); 
      j = (j+1)%NUMPIXELS;  
      led_strip_3.setPixelColor(k, led_strip_3.Color(0,255,0)); // Moderately bright green color.
      led_strip_3.show(); 
      k = (k+1)%NUMPIXELS;
      led_strip_4.setPixelColor(l, led_strip_4.Color(0,255,0)); // Moderately bright green color.
      led_strip_4.show(); 
      l = (l+1)%NUMPIXELS;   
      led_strip_5.setPixelColor(m, led_strip_5.Color(0,255,0)); // Moderately bright green color.
      led_strip_5.show(); 
      m = (m+1)%NUMPIXELS;      
      delay_val=millis();
    break;
    }
  }
  
}

Is it possible to turn all led strips off, 2 minutes after they've all been switched on and restart the interaction at "case 0"?

Yes

Save the millis() value at the time that they are all turned on. Then, each time through loop(), check whether the required wait period has elapsed by subtracting the start time from the millis() value now. If the period has elapsed then go back to case 0 If not, then go round loop() again but you must not have blocking code in loop()

For extra information look at Using millis() for timing. A beginners guide, Several things at the same time and look at the BlinkWithoutDelay example in the IDE.

What @UKHelibob said, but you need to flag the fact that millis was captured the very first time case5 was run, and not re-"zeroed" on subsequent spins through loop().

Something like this maybe?

At the top:

unsigned long case5StartedAt;
bool case5isRunning = false;

Then in case 5:

case 5:
if (!case5isRunning) // ! means not, so read as "if case5 is not running"
{
case5StartedAt=millis();
case5isRunning = true;
}
else
{
   if (millis() - case5StartedAt >= 120000)
      {
         a =0;
         case5isRunning=false;
       }
}

//as before
led_strip_1.setPixelColor(i, led_strip_1.Color(0,140,255)); // Moderately bright green color.
      led_strip_1.show();