Using the if statement with a time delay

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!

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);
  }
}

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

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:

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

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:

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);
  }
}

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?

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

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

  int pushButtonState = digitalRead(pushButton);

Pointless line of code

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:

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);
  }
}

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.

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

Try this:

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.

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.

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:

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;
  }
}

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.

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.

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