Need Guide from expert

I need help from you guys, I will attach my coding below..there is two buttons that I used for this..first is to turn on the led..the second one is to turn off the led..but I want to make the second button when I keep pressing it and did not release it the led will continue to turn on..but when I released the button it will take around five seconds to turn off the led..can anyone help me with the coding..tq

int buttonPin1 = 2; //Start button
int buttonPin2 = 3; //Stop button
int ledPin = 8;
int buttonStatus1 = 0;
int buttonStatus2 = 0;

void setup() {
pinMode(ledPin, OUTPUT);
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
}

void loop() {
buttonStatus1 = digitalRead(buttonPin1);
buttonStatus2 = digitalRead(buttonPin2);

//Check(==) if the first button(START) is HIGH, AND(&&) the second button (STOP) is LOW, if yes turn the LED on.

if (buttonStatus1 == HIGH && buttonStatus2 == LOW)
{ digitalWrite(ledPin, HIGH); }

//Check(==) if the first button (START) is LOW, AND(&&) the second button (STOP is HIGH, if yes turn the LED off.

if (buttonStatus1 == LOW && buttonStatus2 == HIGH)
{digitalWrite (ledPin, LOW); } }

Please do format your code properly (use the autoformat function of the IDE to help out), and use code tags when posting code as described here. That makes it a lot more readable.

I don't really get what you're trying to do, that description may make sense to you but not to me.

1st, read How to Use This Forum
pay really close attention to #6 about subject lines.
pay attention to #7 about how to post code.

2nd when a controller does A thing, it does one thing, one, UNO, singular.
most people assume that when you flip a light switch. everything is one step.
to program, we have to break it down to each step.

in your case, you have 2 buttons.

press and release button #1, ( 2 actions and very different results) led comes on

press and release button #1, press and release #2 , after release, the timer starts and counts X time then turns off the LED.

as a note, if you press and hold, or press and release, the timer starts from the moment of the last time button #2 was released, if you programed it that way.


press #1, then #2, then #1..... button stays on indefinitely

#1, #2, #1, - ignores the second #1 if it is within the 5 second time-out

#1, #2, time-out, LED off, then #1, LED goes on indefinably

press and hold #1, no other actions matter, LED on indefinitely

Press and hold #1, then press and release #2, #2 superceeds #1 and turns off the LED after 5 seconds
Press and hold #1, then press and release #2, release #1 before the 5 seconds, LED goes off 5 seconds after #2 release.

as you can see, what you want can be done, but lots of other results can be achieved as well.

button1state = read #1
button2state = read #2

if button1state = HIGH, led1state = high

if button2state == low // button is not pressed
if button2state ( !=last_state ) // last state was pressed, so it was just released
then = millis() // start the timer

elapsed = millis(() - then // determines the elapsed time

if elapsed >= 5000 // 5 seconds
led1state = LOW

digitalWrite(led1, led1state )

// at the end of loop, add
last_state=button2state //

============

this is not code, but if you look at the idea
led1state is the flag you use during the loop, you only write the digialWrite after all the logic.

after button2 has been released, the timer starts to count
if button1 is held after the count, the LED will turn off for one scan of loop()
then turn back on and stay on.

as a note, it is good form to use unsigned long for any variables that hold times.

This link has code for different button click types. It may help.

...R