Delay Problem. Can't understanding where to put delay in if else statements.

I'm making a simple code for a sawitch. If the switch is pressed the Led connected to pin 4 will lit up and wait for 2 seconds. But as soon as I release the button or switch the LED is getting off. delay is not working.

Any help regarding these will help me a lot. Thanks in advance.

int pushButton = 3;
int LedPin = 4;
int buttonState = 0;

// the setup routine runs once when you press reset:
void setup() {
  
  // make the pushbutton's pin an input:
  pinMode(pushButton, INPUT);
  pinMode(LedPin, OUTPUT);
}

// the loop routine runs over and over again forever:
void loop() {
  // read the input pin:
   buttonState = digitalRead(pushButton);
  
  if (buttonState == HIGH)
  {
    digitalWrite(LedPin,LOW);
    delay (2000);
    
  }
  else
  {
    digitalWrite(LedPin,HIGH);
    delay (2000);
  }
  
 
}

In that code the led will be on at least 2 seconds (and a multiple of 2 seconds). Isn't that what you want?

septillion:
In that code the led will be on at least 2 seconds (and a multiple of 2 seconds). Isn't that what you want?

if i pressed the switch, the led will be on for two seconds and then code goes to checking again whether switch is pressed or not.

Yes, it does that indeed. Don't you want that? If you don't, explain into more detail what you really want. As far as I can see you're code does what you tell us you want :wink:

ikelectro, you said: "But as soon as I release the button or switch the LED is getting off. delay is not working."

What septillion said is absolutely true. The LED will always be either on or off and the Arduino will always wait two seconds. The delay is working, unless some Major damage has been done to the Arduino base code (which just doesn't happen in real life).

I think it is possible that you just aren't expressing your Problem such that we understand it enough to help you.

To answer your question: "where to put delay in if else statements." Generally speaking, it is never a good idea to do the same Thing at the end of an if and an else. It just inflates the code and makes it use more Flash Memory. Move it to after the if

if (buttonState == HIGH)
digitalWrite(LedPin,LOW);
else
digitalWrite(LedPin,HIGH);

delay (2000);

Does the same Thing. Actually, in this simple case, you don't even Need the variable buttonState. You could simply write digitalWrite(LedPin, !digitalRead(pushButton));

ikelectro:
But as soon as I release the button or switch the LED is getting off. delay is not working

So is your LED wired through the switch?

Really good call boolrules!!! Funny how programmers often don't think about Hardware... Hats off!

One problem with your code is that it ALWAYS delays for 2 seconds.

If it checks the button and it is HIGH (not pressed) it switches the LED off and then waits for 2 seconds before it even looks at the button again. I don't think you want it to do that. I'd remove the delay(2000) after the digitalWrite(LedPin,LOW); and see if that's nearer to what you want.

Steve

You're expecting active low operation, judging by the narrative and the code:

ikelectro:
If the switch is pressed the Led connected to pin 4 will lit up

  if (buttonState == HIGH)
  {
    digitalWrite(LedPin,LOW);
    delay (2000);
    
  }
  else
  {
    digitalWrite(LedPin,HIGH);
    delay (2000);
  }
   
}

That means you need a pullup resistor on the button's pin, but you don't have INPUT_PULLUP in the pinMode. So do you have an external one fitted?

Thanks everyone for your reply. May be I'm not to express my actual problem. I'm trying now.

  1. A led will ON and OFF after pressing the switch. First time will ON and then it will OFF again and it goes like this forever.

  2. After the Switch is pressed, the Code will not take any input from the Switch for suppose 2 seconds or may be 5 seconds.

  3. The state of the LED will change only after the switch is pressed.
    That is actually I want to try.

wilfredmedlin:
You're expecting active low operation, judging by the narrative and the code:

  if (buttonState == HIGH)

{
    digitalWrite(LedPin,LOW);
    delay (2000);
   
  }
  else
  {
    digitalWrite(LedPin,HIGH);
    delay (2000);
  }
 
}





That means you need a pullup resistor on the button's pin, but you don't have INPUT_PULLUP in the pinMode. So do you have an external one fitted?

Yes, I added a 10K pull up resistor.

ikelectro:
Yes, I added a 10K pull up resistor.

Much easier just to use the internal one.

pinMode(somePinNumber, INPUT_PULLUP);

Still not clear what you want to happen or when you talk about what happens. Can you explain what you want to happen only?