Go Down

Topic: Run an if-else once (Read 666 times) previous topic - next topic

guns4guitars

Hello people. I have a project involving an RC car and a "seat belt". Basically the car cannot move unless the seat belt is fastened. I am using an Arduino UNO to control whether the car moves. It checks for a logic 1 or 0 at pin 9, and controls a relay at pin 8. The program works and the car does move only when the seat belt is fastened, but when the seat belt is unfastened the car immediately slows to a halt. I needed to add a delay when the seat belt is unfastened. So I added the delay() function and tried uploading. Here's the problem. I added a delay of 60 seconds. When the seat belt is unfastened, the Arduino delays for 60 seconds before writing a LOW to the relay. This is perfect, but when I refasten the seat belt after 60 seconds, the Arduino waits another 60 seconds before re-activating the relay. So I need help. Is it possible to just run the if-else once? Like what ever is in the "else" just once?

*ignore LCD commands.

#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int relayOut = 8;
int receiverIn = 9;
int x = 60;

void setup()
{
  pinMode(relayOut,OUTPUT);
  pinMode(receiverIn,INPUT);
  lcd.begin(16, 2);
  lcd.print("Welcome. Please fasten your seat belt.");


void delayCarStart()
{
  delay(3000);
  digitalWrite(relayOut, LOW);
}
 
void loop()
{
  if (digitalRead(receiverIn) == HIGH)
     {
     digitalWrite(relayOut, HIGH);
     lcd.print("You may proceed");
     }
  else
     {
     lcd.print("Please fasten your seat belt!");
     delayCarStart();
     }     
}


holmes4

How are you de-bouncing the button. Look at the examples.

Mark

Quick5pnt0

#2
Jan 17, 2013, 01:27 pm Last Edit: Jan 17, 2013, 01:31 pm by Quick5pnt0 Reason: 1
Is this the code you tested because there is nothing there that delays for 60 seconds. I agree wih Mark, it does sound like a debounce problem. Your button is "bouncing" which cycles from high to low a few times before settling. During that time the code catches a low state and goes to the else portion of your code.

guns4guitars

#3
Jan 17, 2013, 01:36 pm Last Edit: Jan 17, 2013, 02:13 pm by AWOL Reason: 1
Oops, posted the wrong copy...
Code: [Select]

#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int relayOut = 8;
int receiverIn = 9;
int x = 60;

void setup()
{
 pinMode(relayOut,OUTPUT);
 pinMode(receiverIn,INPUT);
 lcd.begin(16, 2);
 lcd.print("Welcome. Please fasten your seat belt.");
}  

 
void loop()
{
 if (digitalRead(receiverIn) == HIGH)
    {  
    digitalWrite(relayOut, HIGH);
    lcd.print("You may proceed");
    }
 else
    {
    lcd.print("Please fasten your seat belt!");
    delay(60000);
    digitalWrite(relayOut, LOW);
    }    
}


Moderator edit: CODE TAGS, CODE TAGS.

Quick5pnt0


Go Up