make a loop run for a specified amount of time if a button is pushed

Hello all. I am making a controller to control a off road light bar. the bar has 4 sections of led clusters. i have a Mosfet based relay controller set up. so when switch one is flipped relay one closes and led cluster one illuminates. i have a switch set up for all 4 relays. there is a button included. when pressed i want it to run a loop for lets say 5 seconds. i am newish to this subject. anybody got some good advise for me? code is below.

#include <LiquidCrystal.h>
const int rs = 41, en = 40, d4 = 29, d5 =28, d6 = 27, d7 = 26;
const int sw1 = 5;
const int sw2 = 4;
const int sw3 = 7;
const int sw4 = 6;
const int bttn1 = 10;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

#define relay1 30
#define relay2 31
#define relay3 32
#define relay4 33

void setup(){
lcd.begin(16, 2);
lcd.setCursor(3,0);
lcd.print(“INITIALIZE”);
pinMode(relay1, OUTPUT);
pinMode(relay2, OUTPUT);
pinMode(relay3, OUTPUT);
pinMode(relay4, OUTPUT);
pinMode(sw1, INPUT_PULLUP);
pinMode(sw2, INPUT_PULLUP);
pinMode(sw3, INPUT_PULLUP);
pinMode(sw4, INPUT_PULLUP);
pinMode(bttn1, INPUT_PULLUP);

digitalWrite(relay1,HIGH);
digitalWrite(relay2,HIGH);
digitalWrite(relay3,HIGH);
digitalWrite(relay4,HIGH);
delay(500);
digitalWrite(relay1,LOW);
digitalWrite(relay2,LOW);
digitalWrite(relay3,LOW);
digitalWrite(relay4,LOW);
delay(500);
lcd.clear();

}

void loop(){
if (digitalRead(sw1)== LOW){
lcd.setCursor(0,0);
lcd.print(“LED1= 1”);
digitalWrite(relay1, HIGH);
}
else{
(digitalRead(sw1)== HIGH);
lcd.setCursor(0,0);
lcd.print(“LED1= 0”);
digitalWrite(relay1, LOW);
}

if (digitalRead(sw2)== LOW){
lcd.setCursor(8,0);
lcd.print(“LED2= 1”);
digitalWrite(relay2, HIGH);
}
else{
(digitalRead(sw2) == HIGH);
lcd.setCursor(8,0);
lcd.print(“LED2= 0”);
digitalWrite(relay2, LOW);
}

if (digitalRead(sw3) == LOW){
lcd.setCursor(0,1);
lcd.print(“LED3= 1”);
digitalWrite(relay3, HIGH);
}
else{
(digitalRead(sw3) == HIGH);
lcd.setCursor(0,1);
lcd.print(“LED3= 0”);
digitalWrite(relay3, LOW);
}

if (digitalRead(sw4) == LOW){
lcd.setCursor(8,1);
lcd.print(“LED4= 1”);
digitalWrite(relay4, HIGH);
}

else{
(digitalRead(sw4) == HIGH);
lcd.setCursor(8,1);
lcd.print(“LED4= 0”);
digitalWrite(relay4, LOW);

}

if (digitalRead(bttn1)== LOW){
lcd.clear();
lcd.setCursor(3,0);
lcd.print(“CODE 1”);
digitalWrite(relay1, HIGH);
delay(50);
digitalWrite(relay2, HIGH);
delay(50);
digitalWrite(relay3, HIGH);
delay(50);
digitalWrite(relay4, HIGH);
delay(50);
digitalWrite(relay1, LOW);
delay(50);
digitalWrite(relay2, LOW);
delay(50);
digitalWrite(relay3, LOW);
delay(50);
digitalWrite(relay4, LOW);
delay(50);
lcd.clear();
}

}

LIGHT_BAR_CONTROLLER.ino (2.51 KB)

When the button is pressed, you start a timer. You continually test the timer and power the light until the timer expires.

A timer looks something like this:

uint32_t ledTimer = 0 ; // global or static
. . .
if ( <button is pushed> ) ledTimer = millis() ;

to test:

if ( millis() - ledTimer < 5000 ) { <power led> } // assuming 5 seconds duration

I’ll give that a try. thank you.