Button delay

Hi, I am working on a project, in which, a small part of the project is, Press button---> LED turns ON-------->Button left------>keep LED ON for 5secs and then Turn OFF.

I tired working with while loop and delay. But no use.

void setup() {
  // put your setup code here, to run once:
pinMode(12, OUTPUT);
pinMode(2, INPUT_PULLUP);
Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:

int stat = digitalRead(2);
Serial.println(stat);
  while (stat == LOW){
  digitalWrite(12, HIGH);
  delay(5000);
  stat = digitalRead(2);  
  
} 

  digitalWrite(12,LOW);
}

Try moving the 'delay(5000)' outside the 'while' loop, to just before "stat = digitalRead(2);"

And when posting code, you should first hit Ctrl-T or ">Tools >Auto Format" in the IDE to format it correctly. Makes it much easier to follow.

Edit: And usually, you need to 'debounce' the button, because at the moment you press or release it the contacts 'bounce', causing a short series of high and low states. For this simple example, just a short delay at the beginning of the 'while' loop would suffice, maybe 100mS, but for more advanced programs, you need to learn about 'millis()'-based timing.

Check out the "BlinkWithoutDelay' example for the very basics.

You code is not correct…

Have a go like this:

byte inpin = 2;
byte led = 12;


void setup() {
  pinMode(led, OUTPUT);
  pinMode(inpin, INPUT_PULLUP);   // Pullup (HIGH when button is not pressed. LOW when pressed.
  Serial.begin(9600);
}

void loop() {

  boolean button = false; //a boolean state variable for the button

  // This while loop keeps running untill the value 'button' goes true. Once it is true...itll then stop looping.

  while (!button) {   //while the boolean variable button is false...
    button = !digitalRead(inpin); // The ! mark "flips" the state, so it is true when the button is pressed...
  }

  digitalWrite(led, 1);
  delay(5000);
  digitalWrite(led, 0);

}

Hey Shanta,

Here is what I see in your code:

  1.   check an on/off switch
  1a.  show the status of the switch in the Serial moitor
  2.      if the switch is pressed { 
  2a.        turn on pin 12
  2b.        just wait. 5 seconds. Don't do nothing else. Wait.
  2c.        check the switch again
  3.      turn pin 12 off
  4.   repeat from step 1.

Step 2c is redundant. Rethink step 2b. Everything else looks good to me. Oh, and always check for syntax errors. But it you miss them the compiler messages are explicit and exact.

I understood him differently. I thought he wanted the LED to stay on for the whole time the button was held, then for a further 5 seconds after it was released. That's what I see in this:-

Press button---> LED turns ON-------->Button left------>keep LED ON for 5secs and then Turn OFF.

(That's what my comments were aiming for, anyway. :) )

Perhaps he can clarify.

OldSteve: I understood him differently. I thought he wanted the LED to stay on for the whole time the button was held, then for a further 5 seconds after it was released. That's what I see in this:-

Press button---> LED turns ON-------->Button left------>keep LED ON for 5secs and then Turn OFF.

(That's what my comments were aiming for, anyway. :) )

Perhaps he can clarify.

I assumed he meat he just wanted it on for 5 seconds... Clarification would be useful.

Push to turn on for 5 seconds just needs a simple if, not a while.

INTP: Push to turn on for 5 seconds just needs a simple if, not a while.

Unless it has to stay on for the duration of the button press, plus 5 seconds.

Which would indeed be a compelling argument for your interpretation of the aim.

Or it's a misused while.

The crowd eagerly awaits clarification.

INTP: Which would indeed be a compelling argument for your interpretation of the aim.

Or it's a misused while.

The crowd eagerly awaits clarification.

Meantime, we're all right. :D

OldSteve: Meantime, we're all right. :D

Schrödinger's button.

ChrisTenone: Schrödinger's button.

:D

I wanted it on all the while the button is pressed. and Once the button is left, DO NOT turn off instantly. Rather, stay ON for 5 secs and then turn off!

That's what I wanted!!!

Okay, stop yelling.

You don't need while.

If button pressed blah blah digitalWrite(12,HIGH);

If button released blah blah delay(5000); digitalWrite(12,LOW);

Done

hahahha!! I was not yelling!

Just few extra exclamations marks!! lol!!

Anyways…Thank You all for your help and advises. Really Appreciate it!!