Need help turning on a led for 5sec using anON button or OFF button during the5s

I tryed the whole afternoon with the code but it just down seem to work, I need to turn on a led using a ON button for 5seconds and wait those 5s for it to turn off or i can press the off button before those 5seconds end and turn it off.
please help me i need a code.
here the code i used
.
.
void setup()
{
Serial.begin(9600);
pinMode(A0, INPUT);
pinMode(A1, INPUT);
pinMode(13, OUTPUT);
}

void loop()
{
int etatBP0=digitalRead(A0);
int etatBP1=digitalRead(A1);
int ledState=0;
Serial.println(etatBP0);
Serial.println(etatBP1);

if (etatBP0==1) //il faut que le BP soit actionné
{digitalWrite(13, HIGH);ledState=1;}

while (etatBP1==0 && ledState==1)
{ delay(5000);
digitalWrite(13, LOW);ledState=0;}

if (etatBP1==1 && ledState==1)
{digitalWrite(13, LOW);ledState=0;}

}

dazzling_crift_wluff1.ino (521 Bytes)


How to post an image

Hello
Welcome to the Arduino fora.
Before you do anything else please take a moment to read General guidance and
How to use this forum
Especially item #7 on posting code.

We can't help with your code if we can't see your code!

Thank you.

Here the code I used

Please follow the instructions I gave you a link to and post your code properly so it looks like

Some properly posted code.
  while (etatBP1==0 && ledState==1)
  { delay(5000);
  digitalWrite(13, LOW);ledState=0;}

How do you think etatBP1 ever changes from 0 when there is nothing inside your while loop that changes it?

i think i need to implement the inturruption in the while loop,sorry for the late response.
Im very confussed, how am i suppose to change it to 1 and why ?

Question : is it possible to toggle the led off before the delay is over using the off button ?

youness_drr:
or i can press the off button before those 5seconds end and turn it off.

You cannot do this while you have delay() in the sketch. To do what you want you'll first have to get up to speed with:

several things at the same time

and

using millis() for timing

A graphical view:

Here are two solutions, one quick and dirty, the other more elegant.

The first one checks to see if the on button is pressed, and if it is it turns the led on and captures millis() as a start time. That means the start time is repeatedly zeroed as long as the button is pressed, and the consequence of that is that the 5s only starts when the on button is released.

The second version uses state change detection for the on button, so the timer starts as soon as the on button becomes pressed.


Note that in both cases my buttons are wired differently from yours: mine have no external resistors and the button is wired from pin to ground not to 5V.


// https://forum.arduino.cc/index.php?topic=678048.0
// this quick version V1 looks for the on button to *be* pressed, not *become* pressed
//     so, time starts on the release of the on button
//     if you need time to start on the press, see https://www.arduino.cc/en/Tutorial/StateChangeDetection
//     and V2 of this

// this constant won't change:
const int  on_button = A0;
const int  off_button = A1;
const int keepLedOnFor = 5000;

// Variables will change:
unsigned long weTurnedTheLedOnAt;

void setup()
{
  // initialize serial communication:
  Serial.begin(9600);
  Serial.println("V1.... https://forum.arduino.cc/index.php?topic=678048.0");
  pinMode(on_button, INPUT_PULLUP);
  pinMode(off_button, INPUT_PULLUP);

  //turn bulitin led off
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, LOW);

  Serial.println(" ");
  Serial.println("setup() done... press the on_button");
  Serial.println(" ");
}

void loop()
{
  if (digitalRead(on_button) == LOW)
  {
    digitalWrite(LED_BUILTIN, HIGH); //led goes on with the press
    weTurnedTheLedOnAt = millis(); //time starts on the release of the on button
    // if you need time to start on the press, see https://www.arduino.cc/en/Tutorial/StateChangeDetection
  }

  if (millis() - weTurnedTheLedOnAt >= keepLedOnFor || digitalRead(off_button) == LOW)
  {
    digitalWrite(LED_BUILTIN, LOW);
  }
} //loop
// https://forum.arduino.cc/index.php?topic=678048.0
// V2 with state change detect
/*
  BASED ON State change detection (edge detection) changed for INPUT PULLUP
               (on_button wired from pin to ground)
  https://www.arduino.cc/en/Tutorial/StateChangeDetection
*/

// this constant won't change:
const int  on_button = A0;
const int  off_button = A1;
const int keepLedOnFor = 5000;

// Variables will change:
bool on_buttonState;         // current state of the on_button
bool laston_buttonState;     // previous state of the on_button
unsigned long weTurnedTheLedOnAt;

void setup()
{
  // initialize serial communication:
  Serial.begin(9600);
  Serial.println("V2.... https://forum.arduino.cc/index.php?topic=678048.0");
  pinMode(on_button, INPUT_PULLUP);
  pinMode(off_button, INPUT_PULLUP);

  //initialize on_button states
  on_buttonState = digitalRead(on_button);
  laston_buttonState = on_buttonState;

  //turn bulitin led off
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, LOW);

  Serial.println(" ");
  Serial.println("setup() done... press the on_button");
  Serial.println(" ");
}

void loop()
{
  checkTheOnButton(); //turn led on with a new press
  
  if (millis() - weTurnedTheLedOnAt >= keepLedOnFor || digitalRead(off_button) == LOW)
  {
    digitalWrite(LED_BUILTIN, LOW);
  }

} //loop

void checkTheOnButton()
{
  // read the on_button:
  on_buttonState = digitalRead(on_button);

  // compare the on_buttonState to its previous state
  if (on_buttonState != laston_buttonState) // != means not equal, so it changed one way or the other
  {
    if (on_buttonState == LOW) //... and if it's now low, that's a press
    {
      //Serial.println("New on ");
      digitalWrite(LED_BUILTIN, HIGH);
      weTurnedTheLedOnAt = millis();
    }// change to low

    // Delay a little bit to avoid bouncing
    delay(50);
  }//change
  // save the current state as the last state, for next time through the loop
  laston_buttonState = on_buttonState;
}//on