I'm looking building a push button start for my bike with single button. I want the following input but am having trouble. please help me.
first push = digitalWrite(relayIGN, HIGH)
digitalWrite(relayStart, HIGH)
delay(2000)
digitalWrite(relayStart, LOW)
second push = digitalWrite(relayIGN, LOW)
this is the code which i tried:
int pinButton = 8;
int relayStart = 2;
int relayIGN = 3;
int relayStartState;
int relayIGNState;
int LED = 13;
int buttonState;
void setup() {
// put your setup code here, to run once:
pinMode(pinButton, INPUT);
pinMode(relayStart, OUTPUT);
pinMode(relayIGN, OUTPUT);
pinMode(LED, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
digitalWrite(LED, HIGH);
buttonState = digitalRead(pinButton);
relayIGNState = digitalRead(relayIGN);
relayStartState = digitalRead(relayStart);
if (buttonState == HIGH && relayIGNState == LOW){
digitalWrite(relayIGN, HIGH);
digitalWrite(relayStart, HIGH);
delay(2000);
digitalWrite(relayStart, LOW);
}else if (buttonState == HIGH && relayIGNState == HIGH){
digitalWrite(relayIGN, LOW);
digitalWrite(relayStart, LOW);
}
}
first push = digitalWrite(relayIGN, HIGH)
digitalWrite(relayStart, HIGH)
delay(2000)
digitalWrite(relayStart, LOW)
second push = digitalWrite(relayIGN, HIGH)
Will a second button push have any effect, bearing in mind that relayIGN will already be HIGH ?
Sorry, that was a typing mistake.
Second push= digitalWrite(relayIGN, LOW)
Can you please post your code in code-format? What problem do you have with using the current code?
//this is the code which i tried:
int pinButton = 8;
int relayStart = 2;
int relayIGN = 3;
int relayStartState;
int relayIGNState;
int LED = 13;
int buttonState;
void setup() {
// put your setup code here, to run once:
pinMode(pinButton, INPUT);
pinMode(relayStart, OUTPUT);
pinMode(relayIGN, OUTPUT);
pinMode(LED, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
digitalWrite(LED, HIGH);
buttonState = digitalRead(pinButton);
relayIGNState = digitalRead(relayIGN);
relayStartState = digitalRead(relayStart);
if (buttonState == HIGH && relayIGNState == LOW){
digitalWrite(relayIGN, HIGH);
digitalWrite(relayStart, HIGH);
delay(2000);
digitalWrite(relayStart, LOW);
}else if (buttonState == HIGH && relayIGNState == HIGH){
digitalWrite(relayIGN, LOW);
digitalWrite(relayStart, LOW);
}
}
When i press the button for the first time relayIGN and relayStart is going HIGH and after given delay the relayStart is going LOW as i wish. But when i press the button for second time relayStart is going HIGH again and relayIGN is not going LOW.
The problem is that after the code in the 'else' braces is executed, on the next iteration of 'loop()', (while the button is still pressed), the first comparison is true, so both 'relayIGN' and 'relayStart' are made high again.
There are better ways of doing this, using state change detection, but a short delay after making both low will work. Probably about 200mS to 300mS would do.
Look at the "Debounce" and "StateChangeDetection" examples included with the IDE for more information on the better way.
Edit: Oops, almost forgot - in the "Debounce" example, a 'long' variable type is used for storing 'millis()':-
long lastDebounceTime = 0;
That should be an 'unsigned long':-
unsigned long lastDebounceTime = 0;
This error has now been fixed for future releases of the IDE, but still appears right up to IDE V1.6.11, and possibly even the early releases of V1.6.12.
Edit2: A potential fault in your system is that if the engine starts immediately, the starter will still continue trying to crank it for the remainder of the 2 seconds. In a similar system I made years ago, I took feedback from the alternator, (using an appropriate voltage divider), so the starter would be stopped as soon as the engine started. Of course, to do that you'd need to get rid of the 'delay()' and use 'millis()'-based timing instead, to read the alternator input.