First off, wow! i dont think i've ever used a forum with so many useful answers after a single question! thanks guys!
so i guess to put this in perspective, ill fill you in a bit more. this is my first almost completely original sketch. it's function is to emulate a smart key system like toyota uses. so if the rf key is detected and the brake is held while the button is pushed the car starts right up. if the brake is not held and the button is pushed it goes to accessory, push again, ignition. once at ignition you can hold the brake and push again to start the vehicle or you can just push again and the car turns off. right now im not worried about the rf key since this vehicle is still in development and i can code that in a bit further down the line.
the safety feature is making sure the NSS (neutral safety switch from the transmission that shows ground in park or neutral) is grounded so the vehicle cannot start in gear.
the lastSw and currentSw are variables from a debounce code i borrowed from another sketch. honestly, i dont know if i'm even using it correctly.
my assumption is that by making sure the last switch state and current switch state does not match in each possible situation it will prevent crazy things from happening if the user holds the button down for too long since it is always checking to make sure the last button state was not high (pressed) before running another line of code.
here is my sketch if you care to look it over. if you see anywhere i can make it more efficient please let me know! i appreciate your time and helpfulness!
/*
Push button Ignition Switch/Starter (Like Toyota SmartKey)
TEAM WIKISPEED
Created by Nick Franklin - Franklin Automotive Electronics
206.551.5705
January 14, 2013
*/
//set pin numbers:
const int brakePin = 7; // brake petal +input
const int engRun = 6; // from alternator running +input
const int swPin = 5; // ignition push button closed = HIGH
const int stPin = 4; // +output to starter relay
const int igPin = 3; // +output to ignition relay
const int led = 13; // i synchronize this led with other outputs for diagnostic purposes
const int nssPin = 8; // -input from transmission in park or neutral so you can not start in gear
boolean lastSw = LOW;
boolean currentSw = LOW;
void setup()
{
pinMode(stPin, OUTPUT);
pinMode(igPin, OUTPUT);
pinMode(led, OUTPUT);
pinMode(swPin, INPUT);
pinMode(engRun, INPUT);
pinMode(brakePin, INPUT);
pinMode(nssPin, INPUT);
}
// Debounce code to prevent button misread
boolean debounce(boolean last)
{
boolean current = digitalRead(swPin);
if (last != current);
{
delay(5);
current = digitalRead(swPin);
}
return current;
}
void loop()
{
currentSw = debounce(lastSw);
//if the ignition is off but the brake is held and the ignition button is pushed
//program will turn the ignition on, wait 3sec for fuel pump to prime, then activate the starter relay.
if (lastSw == LOW && currentSw == HIGH && igPin == LOW && nssPin == LOW && brakePin == HIGH);
{
digitalWrite(igPin, HIGH);
delay(3000);
digitalWrite(stPin, HIGH);
//once the engine running signal is recieved from the alternator the starter relay is deactivated.
if (engRun == HIGH);
digitalWrite(stPin, LOW);
}
// if the ignition is off and the button is pressed once ignition will turn on
if (lastSw == LOW && currentSw == HIGH && igPin == LOW);
{
digitalWrite(igPin, HIGH);
}
// if ignition is on, brake is pressed, engine is not running, and the button is pushed
// then activate the starter relay.
if (lastSw == LOW && currentSw == HIGH && igPin == HIGH && nssPin == LOW && brakePin == HIGH && engRun == LOW);
{
digitalWrite(stPin, HIGH);
//once the engine running signal is recieved from the alternator the starter relay is deactivated.
if (engRun, HIGH);
digitalWrite(stPin, LOW);
}
// if the engine is running and the button is pushed the ignition relay is deactivated and the vehicle is turned off
if (lastSw == LOW && currentSw == HIGH && igPin == HIGH && engRun == HIGH);
{
digitalWrite(igPin, LOW);
}
}