Offline
Newbie
Karma: 0
Posts: 7
|
 |
« on: January 17, 2013, 07:22:15 am » |
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(); } }
|
|
|
|
|
Logged
|
|
|
|
|
Poole, Dorset, UK
Offline
God Member
Karma: 8
Posts: 671
|
 |
« Reply #1 on: January 17, 2013, 07:26:59 am » |
How are you de-bouncing the button. Look at the examples.
Mark
|
|
|
|
|
Logged
|
|
|
|
|
NE PA
Offline
Full Member
Karma: 5
Posts: 149
|
 |
« Reply #2 on: January 17, 2013, 07:27:37 am » |
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.
|
|
|
|
« Last Edit: January 17, 2013, 07:31:09 am by Quick5pnt0 »
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 7
|
 |
« Reply #3 on: January 17, 2013, 07:36:51 am » |
Oops, posted the wrong copy... #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.
|
|
|
|
« Last Edit: January 17, 2013, 08:13:58 am by AWOL »
|
Logged
|
|
|
|
|
NE PA
Offline
Full Member
Karma: 5
Posts: 149
|
 |
« Reply #4 on: January 17, 2013, 07:39:01 am » |
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 7
|
 |
« Reply #5 on: January 17, 2013, 08:11:43 am » |
still cant get it to work ergh... need this to work by saturday!!!!
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Online
Brattain Member
Karma: 138
Posts: 19065
I don't think you connected the grounds, Dave.
|
 |
« Reply #6 on: January 17, 2013, 08:15:12 am » |
Have a look at the blink without delay tutorial to allow you to not use "delay()", and make your code more responsive.
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
Queens, New York
Offline
Edison Member
Karma: 29
Posts: 1565
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
|
 |
« Reply #7 on: January 17, 2013, 10:17:33 am » |
EDIT! Does not work properly Try this flagged = 0; //global variable at the top of code
void loop() { if (digitalRead(receiverIn) == HIGH) { if(!flagged) { digitalWrite(relayOut, HIGH); lcd.print("You may proceed"); flagged = 1; } else { lcd.print("Please fasten your seat belt!"); delayCarStart(); } } else flagged = 0; }
Not compiled or tested but, have use simular example in other working programs.
|
|
|
|
« Last Edit: January 17, 2013, 03:26:04 pm by HazardsMind »
|
Logged
|
UNO, MEGA, NANO, 4x4 keypad, micro servos, RF transceivers, bluetooth, ultrasonic sensor, 20x4 I2C LCD, 3.2 TFT touch screen, L298N Dual motor driver, Voice Recognition 15W, Gameduino
Arduino Tutorials, coming soon.
"If your doing nothing, it does not mean your lazy, it just means your open for anything that suits you" - Unknown
|
|
|
|
California
Online
Edison Member
Karma: 41
Posts: 1863
|
 |
« Reply #8 on: January 17, 2013, 10:24:38 am » |
What you're looking for is called signal edge detection. You don't care that the input is LOW, you're just looking for when it goes from HIGH to LOW. To do that, you need to keep track of the last reading. If the current reading is HIGH and the last reading is LOW, you have what's called a falling edge.
|
|
|
|
|
Logged
|
|
|
|
|
NE PA
Offline
Full Member
Karma: 5
Posts: 149
|
 |
« Reply #9 on: January 17, 2013, 02:49:17 pm » |
still cant get it to work ergh... need this to work by saturday!!!!
Post your new code so somebody can see what is wrong.
|
|
|
|
|
Logged
|
|
|
|
|
Queens, New York
Offline
Edison Member
Karma: 29
Posts: 1565
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
|
 |
« Reply #10 on: January 17, 2013, 03:04:16 pm » |
Here this example works. const int sensor = 2; int flag=0; int flag1=0; int last = 0;
void setup(){ Serial.begin(9600); Serial.println("Start"); } void loop(){ //Serial.println(digitalRead(sensor)); if(digitalRead(sensor) == HIGH) { if(!flag){ Serial.println("X"); flag = 1; } } else flag = 0;
if(digitalRead(sensor) == LOW) { if(!flag1){ Serial.println("Y"); flag1 = 1; } } else flag1 = 0; //delay(1000); }
|
|
|
|
|
Logged
|
UNO, MEGA, NANO, 4x4 keypad, micro servos, RF transceivers, bluetooth, ultrasonic sensor, 20x4 I2C LCD, 3.2 TFT touch screen, L298N Dual motor driver, Voice Recognition 15W, Gameduino
Arduino Tutorials, coming soon.
"If your doing nothing, it does not mean your lazy, it just means your open for anything that suits you" - Unknown
|
|
|
|
Offline
Jr. Member
Karma: 1
Posts: 67
|
 |
« Reply #11 on: January 17, 2013, 06:23:21 pm » |
Exactly as Arrch said, you need an edge trigger, google edge triggering arduino or edge triggering c++. It's a pretty easy thing to do.
|
|
|
|
|
Logged
|
|
|
|
|
|