Pages: [1]   Go Down
Author Topic: Using the if statement with a time delay  (Read 733 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!


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

« Last Edit: June 21, 2013, 05:13:29 pm by Munch » Logged

Global Moderator
Boston area, metrowest
Online Online
Brattain Member
*****
Karma: 538
Posts: 27113
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

California
Offline Offline
Faraday Member
**
Karma: 88
Posts: 3382
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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:

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

Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:

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

London
Offline Offline
Edison Member
*
Karma: 47
Posts: 1437
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

California
Offline Offline
Faraday Member
**
Karma: 88
Posts: 3382
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
  pinMode (pushButton, INPUT_PULLUP);
  ...
  if (digitalRead(pushButton) == HIGH) {
  When you are using the internal pullups, HIGH mean not pushed, while LOW means pushed.

Code:
  int pushButtonState = digitalRead(pushButton);
Pointless line of code
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:

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

California
Offline Offline
Faraday Member
**
Karma: 88
Posts: 3382
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

London
Offline Offline
Edison Member
*
Karma: 47
Posts: 1437
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The two push button presses have to happen within a two seconds delay.
Try this:
Code:
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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

California
Offline Offline
Faraday Member
**
Karma: 88
Posts: 3382
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:

Code:
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;
  }
}
Logged

London
Offline Offline
Edison Member
*
Karma: 47
Posts: 1437
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Pages: [1]   Go Up
Jump to: