# Using the if statement with a time delay

**URL:** <https://forum.arduino.cc/t/using-the-if-statement-with-a-time-delay/169335>\
**Category:** Programming\
**Created:** [June 21, 2013, 7:02pm UTC](https://forum.arduino.cc/t/using-the-if-statement-with-a-time-delay/169335 "2013-06-21T19:02:41Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![Munch](https://avatars.discourse-cdn.com/v4/letter/m/a4c791/32.png) [@Munch](https://forum.arduino.cc/u/Munch)\
**Post date:** [June 21, 2013, 7:02pm UTC](https://forum.arduino.cc/t/using-the-if-statement-with-a-time-delay/169335/1 "2013-06-21T19:02:41Z")

</div>

Hi, my objective is to use the if statement so that the first statement, if it's true, activates the second statement and the LED only turns on if the second statement is true. The two push button presses have to happen within a two seconds delay. The code's syntax is correct according to the Arduino software but when I try the uploaded program on my Arduino Uno, the LED always stays off.

I connected the push button between pin 12 and and 5V and used the built-in LED.  
Any help is appreciated!

```cpp
int pushButton = 12;
int ledPin = 13;                 // LED connected to digital pin 13

void setup() {
  pinMode(ledPin, OUTPUT);
  // initialize serial communications:
  Serial.begin(9600);
}

void loop() 
{
  if (pushButton == HIGH) {
       delay(2000);
      if (pushButton == HIGH) {
      digitalWrite(ledPin, HIGH);
    }
      else {
        digitalWrite(ledPin, LOW);
         }
  }
   else {
     digitalWrite(ledPin, LOW);
  }
}

```

---

<div class="post-metadata">

**Author:** ![CrossRoads](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/crossroads/32/214033_2.png) [@CrossRoads](https://forum.arduino.cc/u/CrossRoads)\
**Post date:** [June 21, 2013, 7:07pm UTC](https://forum.arduino.cc/t/using-the-if-statement-with-a-time-delay/169335/2 "2013-06-21T19:07:17Z")

</div>

You need to set the led pin as an output it setup:  
pinMode (ledPin, OUTPUT);

Same for the pushbutton pin - set it as an input with the internal pullup resistors, wire the button so it connects the pin to Gnd (and not 5V):  
pinMode (pushButton, INPUT\_PULLUP);

Where do you read the state of the pushButton pin?  
Ex:  
if (digitalRead(pushButton) == LOW){ // see if button is pressed, connecting the pin to Gnd

---

<div class="post-metadata">

**Author:** ![Arrch](https://avatars.discourse-cdn.com/v4/letter/a/848f3c/32.png) [@Arrch](https://forum.arduino.cc/u/Arrch)\
**Post date:** [June 21, 2013, 7:10pm UTC](https://forum.arduino.cc/t/using-the-if-statement-with-a-time-delay/169335/3 "2013-06-21T19:10:54Z")

</div>

```cpp
int pushButton = 12;
...
  if (pushButton == HIGH) {
       delay(2000);
      if (pushButton == HIGH) {

```

Another good example of why variable names are important. Be explicit about what the variable is:

```cpp
const int pushButtonPin = 12; // pin # that our push button is attached to
int pushButtonState = digitalRead(pushButtonPin); // the current state of our push button

```

---

<div class="post-metadata">

**Author:** ![Munch](https://avatars.discourse-cdn.com/v4/letter/m/a4c791/32.png) [@Munch](https://forum.arduino.cc/u/Munch)\
**Post date:** [June 22, 2013, 2:57am UTC](https://forum.arduino.cc/t/using-the-if-statement-with-a-time-delay/169335/4 "2013-06-22T02:57:13Z")

</div>

Thank you both for your help but I modified my code and it still doesn't do what it should.  
Now the LED is always on until I press and hold the push button for two seconds, then it turns off and stays off for two seconds.

This is the new code:

```cpp
const int pushButton = 12;
const int ledPin = 13;                // LED connected to digital pin 13

void setup() {
  pinMode(ledPin, OUTPUT);
  // initialize serial communications:
  pinMode (pushButton, INPUT_PULLUP);
  int pushButtonState = digitalRead(pushButton);
  Serial.begin(9600);
}

void loop() 
{
  if (digitalRead(pushButton) == HIGH) {
      delay(2000);
      if (digitalRead(pushButton) == HIGH) {
      digitalWrite(ledPin, HIGH);
    }
      else {
        digitalWrite(ledPin, LOW);
        }
  }
  else {
    digitalWrite(ledPin, LOW);
  }
}

```

---

<div class="post-metadata">

**Author:** ![Henry\_Best](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/henry_best/32/1013044_2.png) [@Henry\_Best](https://forum.arduino.cc/u/Henry_Best)\
**Post date:** [June 22, 2013, 3:33am UTC](https://forum.arduino.cc/t/using-the-if-statement-with-a-time-delay/169335/5 "2013-06-22T03:33:03Z")

</div>

> Munch:  
> Thank you both for your help but I modified my code and it still doesn't do what it should.  
> Now the LED is always on until I press and hold the push button for two seconds, then it turns off and stays off for two seconds.

What SHOULD happen?

---

<div class="post-metadata">

**Author:** ![Arrch](https://avatars.discourse-cdn.com/v4/letter/a/848f3c/32.png) [@Arrch](https://forum.arduino.cc/u/Arrch)\
**Post date:** [June 22, 2013, 4:12am UTC](https://forum.arduino.cc/t/using-the-if-statement-with-a-time-delay/169335/6 "2013-06-22T04:12:14Z")

</div>

```cpp
  pinMode (pushButton, INPUT_PULLUP);
  ...
  if (digitalRead(pushButton) == HIGH) {

```

When you are using the internal pullups, HIGH mean not pushed, while LOW means pushed.

```cpp
  int pushButtonState = digitalRead(pushButton);

```

Pointless line of code

---

<div class="post-metadata">

**Author:** ![Munch](https://avatars.discourse-cdn.com/v4/letter/m/a4c791/32.png) [@Munch](https://forum.arduino.cc/u/Munch)\
**Post date:** [June 22, 2013, 2:12pm UTC](https://forum.arduino.cc/t/using-the-if-statement-with-a-time-delay/169335/7 "2013-06-22T14:12:14Z")

</div>

Thank you Arrch for that correction and Henry\_Best, my objective is to use the if statement so that the first statement, if it's true, activates the second statement and the LED only turns on if the second statement is true. The two push button presses have to happen within a two seconds delay.

With this new code, the LED is always off until I press and hold the push button for two seconds, then it turns on and stays on for two seconds.The same as before.

Here's the newest code:

```cpp
const int pushButton = 12;
const int ledPin = 13;                // LED connected to digital pin 13

void setup() {
  pinMode(ledPin, OUTPUT);
  // initialize serial communications:
  pinMode (pushButton, INPUT_PULLUP);
  Serial.begin(9600);
}

void loop() 
{
  if (digitalRead(pushButton) == LOW) {
      delay(2000);
      if (digitalRead(pushButton) == LOW) {
      digitalWrite(ledPin, HIGH);
    }
      else {
        digitalWrite(ledPin, LOW);
        }
  }
  else {
    digitalWrite(ledPin, LOW);
  }
}

```

---

<div class="post-metadata">

**Author:** ![Arrch](https://avatars.discourse-cdn.com/v4/letter/a/848f3c/32.png) [@Arrch](https://forum.arduino.cc/u/Arrch)\
**Post date:** [June 22, 2013, 2:25pm UTC](https://forum.arduino.cc/t/using-the-if-statement-with-a-time-delay/169335/8 "2013-06-22T14:25:43Z")

</div>

> Munch:  
> With this new code, the LED is always off until I press and hold the push button for two seconds, then it turns on and stays on for two seconds.The same as before.

That's exactly what the code is telling it to do. You haven't defined your requirements enough: how long is the LED supposed to be on for? What causes it to turn off? At the moment, the code is only telling it to turn on an LED if it was detected to be pressed down exactly two seconds apart; you've told it that you don't care what happens in between those 2 seconds. The button could be released and pushed a hundred times, or none. I would start by learning about state transitions. In other words, you don't care about the state of the input, just when it transitions from not-pushed to pushed. The StateChangeDetection example demonstrates this concept.

---

<div class="post-metadata">

**Author:** ![Henry\_Best](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/henry_best/32/1013044_2.png) [@Henry\_Best](https://forum.arduino.cc/u/Henry_Best)\
**Post date:** [June 23, 2013, 3:18am UTC](https://forum.arduino.cc/t/using-the-if-statement-with-a-time-delay/169335/9 "2013-06-23T03:18:58Z")

</div>

> Munch:  
> The two push button presses have to happen within a two seconds delay.

Try this:

```arduino
const int pushButton = 12;
const int ledPin = 13;                 // LED connected to digital pin 13
unsigned long startTime;  //variable to hold time button first pushed
int countPress = 0; //variable to know if its the 1st or 2nd press
void setup() {
  pinMode(ledPin, OUTPUT);
  // initialize serial communications:
  pinMode (pushButton, INPUT_PULLUP);
  Serial.begin(9600);
}

void loop() 
{
  if (digitalRead(pushButton) == LOW && countPress==0) { 
      // only for the first press
      startTime = millis(); //button pushed so record start time.
      while  (digitalRead(pushButton) == LOW);
      //do nothing until button is released
      countPress =1;
  }
  if (digitalRead(pushButton) == LOW && (millis() - startTime <= 2000) && countPress ==1) {
      // button pressed 2nd time within 2 seconds
      digitalWrite(ledPin, HIGH);
      countPress = 0;
    }
  if((millis() - startTime > 2000) && countPress ==1){
      // timed out
      countPress = 0;
      digitalWrite(ledPin, LOW);
     }
}

```

That will keep the LED continously on after 2 presses within 2 seconds and for up to 2 seconds after a third press.

---

<div class="post-metadata">

**Author:** ![Munch](https://avatars.discourse-cdn.com/v4/letter/m/a4c791/32.png) [@Munch](https://forum.arduino.cc/u/Munch)\
**Post date:** [June 26, 2013, 10:28pm UTC](https://forum.arduino.cc/t/using-the-if-statement-with-a-time-delay/169335/10 "2013-06-26T22:28:44Z")

</div>

Thank you both Henry and Arrch for your help however I feel like I should have better explained my problem. You see, I need to write a code in which if in the first if statement is high, and the second one is also high, to turn on a LED. If the first statement is high but the second is low, to turn on an other Led. I am am familiar with the StateChangeDetection but I do not think it applies to this case so I tried to do it with the if statement. I apologies for not having well explained the situation but I still haven't figured out what the problem is.

---

<div class="post-metadata">

**Author:** ![Arrch](https://avatars.discourse-cdn.com/v4/letter/a/848f3c/32.png) [@Arrch](https://forum.arduino.cc/u/Arrch)\
**Post date:** [June 26, 2013, 10:40pm UTC](https://forum.arduino.cc/t/using-the-if-statement-with-a-time-delay/169335/11 "2013-06-26T22:40:18Z")

</div>

> Munch:  
> I am am familiar with the StateChangeDetection but I do not think it.

And you would be incorrect. You don't care about the switches state, you just want to know when it is pressed (or released); that's what the StateChangeDetection example shows. Here is a simple example that demonstrates how it can be used to record the time between events:

```arduino
const int switchPin = 8;
int lastSwitchReading = HIGH;
unsigned long lastPressedTime = 0;

void setup()
{
  Serial.begin(115200);
  Serial.println("begin");

  pinMode(switchPin, INPUT_PULLUP);
}

void loop()
{

  int currentReading = digitalRead(switchPin);

  if (currentReading == LOW && lastSwitchReading == HIGH)
  {
    Serial.print("Time between presses: ");
    Serial.println(millis() - lastPressedTime);
    lastPressedTime = millis();
    lastSwitchReading = currentReading;
  }
}

```

---

<div class="post-metadata">

**Author:** ![Henry\_Best](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/henry_best/32/1013044_2.png) [@Henry\_Best](https://forum.arduino.cc/u/Henry_Best)\
**Post date:** [June 27, 2013, 1:29am UTC](https://forum.arduino.cc/t/using-the-if-statement-with-a-time-delay/169335/12 "2013-06-27T01:29:20Z")

</div>

> Munch:  
> Thank you both Henry and Arrch for your help however I feel like I should have better explained my problem. You see, I need to write a code in which if in the first if statement is high, and the second one is also high, to turn on a LED. If the first statement is high but the second is low, to turn on an other Led. I am am familiar with the StateChangeDetection but I do not think it applies to this case so I tried to do it with the if statement. I apologies for not having well explained the situation but I still haven't figured out what the problem is.

When you can explain precisely (to us and to yourself) what you want your sketch to do, you'll know what the problem is. Until then, we're all just guessing.

---

<div class="post-metadata">

**Author:** ![aeshaeret](https://avatars.discourse-cdn.com/v4/letter/a/ea666f/32.png) [@aeshaeret](https://forum.arduino.cc/u/aeshaeret)\
**Post date:** [July 30, 2020, 5:43pm UTC](https://forum.arduino.cc/t/using-the-if-statement-with-a-time-delay/169335/13 "2020-07-30T17:43:34Z")

</div>

I think that you should not use the delay function, and instead use the millis function because the delay function blocks the program, and cannot be used in a if statement. Correct me If I am wrong.

---

<div class="post-metadata">

**Author:** ![anon73444976](https://avatars.discourse-cdn.com/v4/letter/a/439d5e/32.png) [@anon73444976](https://forum.arduino.cc/u/anon73444976)\
**Post date:** [July 30, 2020, 6:05pm UTC](https://forum.arduino.cc/t/using-the-if-statement-with-a-time-delay/169335/14 "2020-07-30T18:05:11Z")

</div>

> aeshaeret:  
> I think that you should not use the delay function, and instead use the millis function because the delay function blocks the program, and cannot be used in a if statement. Correct me If I am wrong.

You're not wrong, but you're seven years too late

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 5, 2021, 8:15pm UTC](https://forum.arduino.cc/t/using-the-if-statement-with-a-time-delay/169335/15 "2021-05-05T20:15:12Z")

</div>

Closed on May 5, 2021, 8:15pm UTC


