Hi there, im looking to see if anyone can corroborate if this code would work. I ham very new to the world of Arduino and have came up with this code. the compiler states that it is good but I would be comfortable with a second opinion before putting it to use.
/* C++ code
//***************************************REMOTE STARTER FOR VHECILES "USING FACTORY REMOTE"*******************************************
This is my take on a Remote starter computer code using ELEGOO MEGA 2560, without the need for a secondary remote for my vehicle.
Its main function is too fire up and shutdown an engine using pre-determined input signals from the Unlock/Lock signal output from the
BCM of a vehicle with power locks.
The program will watch for unlock signal X4 to fire engine up upon command(if start failure blink an error LED) and watch for a run signal
to ensure it is still running (if not, turn off igintion power to car and blink an error led).
It is to then watch for a lock signal X3 to turn off engine upon command.
it is also to have the engine running for a set amout of time (15 Minutes Max)
and if the brake pedal is pushed shut down as well
In the future i have plans to add an alarm fuctionality to it, as well s control from a phone or other electronic device using a separte wireless signal. "TBD"
and to add code to honk the horn as notifiction to user that something has happened good or bad
Xenon's Custom Components
*/
//************************************************************************************************************************************
/*Variable and name Initailization Settings******************************************************************************************
All input signals from vehicle will be pulled down to ground Via an external resistor, 12 volt input signals will also be steped down //to 5 volts using an exteranl 5Volt power regulator
All Output signals will be controling a 5 volt octocoupler enabled relay to control 12 volt power out
*/
int lockBtn = A0; //Setting pin A0 as lock signal (From Vehicle BCM)
int unlkBtn = A1; //Setting pin A1 as unlock signal (FromVeehicle BCM)
int walkBtn = A3; //Setting pin A3 as a walk away button to leave car running for 15 minutes after user turn key off
int brakeCk = A4; //Setting pin A4 as Brake signal Check(User pushing brake pedal)
int runSgnl = A5; //Setting Pin A5 for engine on signal (either injector signal or tachometer signal "TBD")
int error = 13; //Setting pin 13 for error Led (For user to see there was an issue with remote start)
int ignPin = 12; //Setting Pin 12 for ignition control
int accPin = 11; //Setting Pin 11 for Accessory control
int strtPin = 10; //Setting Pin 10 for Starter control
int onVal = 820; //Setting Variable for checking signal in value
int strterrorVal = 0; //Setting Varible for Error State
int startVal = 0; //Setting Variable for Startup Check
int sdVal = 0; //Setting Varible for Shut Down Check
int runerrorVal = 0; //Setting Variable for engine run error state on(1) / off(0)
bool unlkOff = 0; //Setting Variable for unlock switch on state on(1) / off(0)
bool unlkOn = 0; //Setting Variable for unlock switch off state on(1) / off(0)
bool lockOff = 0; //Setting Variable for lock switch off state on(1) / off(0)
bool lockOn = 0; //Setting Variable for lock switch on state on(1) / off(0)
bool unlocktimerCheck = 0; //Setting Variable for lock timer on(1) / off(0)
bool locktimerCheck = 0; //Setting Variable for unlock timer on(1) / off(0)
bool enginetimerSw = 0; //Setting Variable for engine run time timer on(1) / off(0)
unsigned long strterrorTime; //Varible for storing Start error time
unsigned long runerrorTime; //Variable for storing Shutdown Error time (such as engine failure)
unsigned long engineTime; //Variable for storing engine run time for this Engine start
unsigned long lockTime; //Variable for storing time value for lock switch check
unsigned long unlkTime; //Variable for storing time value for unlock switch check
unsigned long unlkCheck; //Variable for storing time value for Starup check
unsigned long lockCheck; //Variable for storing time value for ShutDown check
//**************************************************************************************************************************************
//Setting pinmode code ************RUNS ONCE at board power****************************************************************************
void setup()
{
Serial.begin(9600); //Setting Serial Baud Rate for communication to computer for debugging
pinMode(lockBtn, INPUT); //Setting Lock Button Pin as an input
pinMode(unlkBtn, INPUT); //Setting unlock button Pin as an input
pinMode(runSgnl, INPUT); //Setting Run Signal Pin as a input
pinMode(brakeCk, INPUT); //Setting Break Check Pin as a input
pinMode(walkBtn, INPUT); //Setting Walk Button Pin as an input
pinMode(ignPin, OUTPUT); //Setting Ignition Pin as an output
pinMode(accPin, OUTPUT); //Setting Accessory Pin as an output
pinMode(strtPin, OUTPUT); //Setting Starter Pin as an output
pinMode(error, OUTPUT); //Setting Error LED Pin as an output
}
/*
//**************************************************************************************************************************************
//********************************************Main Program_____Runs Repeatedly**********************************************************
*/
void loop() //Main Loop, Run constantly and repeatedly
{
if (millis() - unlkTime > 10) //Is it time to check unlock switches?
{
unlkTime = millis(); //Restart timer
if (analogRead(unlkBtn) > onVal) //is Unlock button on? if so do this:
{
unlkOn = 1; //unlock on value set to 1
}
if (unlkOn == 1 && analogRead(unlkBtn) < onVal) //has unlock button been released after been pressed? if so do this:
{
unlkOff = 1; //unlock off value set to 1
}
if (unlkOn == 1 && unlkOff == 1) //if both unlock on and unlock off value is 1 do this:
{
startVal += 1; //compound add 1 to start value
unlkOn = 0; //reset unlock on value to 0
unlkOff = 0; //reset unlock off value to 0
unlocktimerCheck = 1; //start 10 second start up count, timer
}
}
if (startVal >= 4) //if Start Value is equal to or above a value of 4 do this:
{
startUp(); // Go to Startup one time code
} // Return from Startup one time code
if (millis() - unlkCheck > 10000 && unlocktimerCheck == 1) //is 10 second start up count timer done? if so do this:
{
startVal = 0; //reset start value to 0
unlkOn = 0; //reset unlock on value to 0
unlkOff = 0; //reset unlock off value to 0
unlkCheck = millis(); //reset unlock timer
unlocktimerCheck = 0; //Turn off unlock check Timer
}
if (millis() - lockTime > 10) //Is it time to check lock switches?
{
lockTime = millis(); //Restart timer
if (analogRead(lockBtn) > onVal) //is lock button on? if so do this:
{
lockOn = 1; //lock on value set to 1
lockCheck = millis(); //start 5 second shutdown count, timer
}
if (lockOn == 1 && analogRead(lockBtn) < onVal) //has lock button been released after been pressed? if so do this:
{
lockOff = 1; //lock off value set to 1
}
if (lockOn == 1 && lockOff == 1) //if both lock on and lock off value is 1 do this:
{
sdVal += 1; //compound add 1 to start value
lockOn = 0; //reset lock on value to 0
lockOff = 0; //reset lock off value o 0
locktimerCheck = 1; //Turn on lock check timer
}
if (sdVal >= 3) //if ShutDown Value is equal to or above a value of 3 do this:
{
shutDown(); //Go to DhutDown one time code
} //Return from ShutDown one time code
if (millis() - lockCheck > 5000 && locktimerCheck == 1) //is 5 second shutdown count timer done? if so do this:
{
startVal = 0; //reset shutdown value to 0
lockCheck = millis(); //reset lock check timer
lockOn = 0; //reset lock on value to 0
lockOff = 0; //reset lock off value to 0
locktimerCheck = 0; //Turn off lock check Timer, will start next time it is called for
}
}
if (millis() - strterrorTime >= 500) //Timer Error Code Begin (repeats and resets every half second)
{
strterrorTime = millis(); //sets start error time value to current time in milliseconds(for code loop)
if (strterrorVal == 1) //if start error is 1 do the following:
{
if (digitalRead(error) == LOW) //if Error LED is off do the following
{
digitalWrite(error, HIGH); //Turn Error LED on
}else{ //if above if statement condtion is not met do the following
digitalWrite(error, LOW); //Turn Error LED off
}
}
} //End of Starter Error Timer Code
if (millis() - runerrorTime >= 200) //simple timer to check engine run status, turn off ignition and flash on led if an error is
{
runerrorTime = millis(); //Sets run error time value to current time in milliseconds
if (digitalRead(ignPin) == HIGH && analogRead(runSgnl) <= onVal)//If ignition is on but engine is off do the following:
{
digitalWrite(accPin, LOW); //Turn Accessory Off
digitalWrite(ignPin, LOW); //Turn Igintion Off
runerrorVal = 1; //Set Run Error Value To 1
enginetimerSw = 0; //Turn engine run timer off
//insert future honk code for failed run
}
if (runerrorVal == 1) //If Run Error Value is 1 do the Followinig:
{
if (digitalRead(error) == LOW) //set of code for flashing error LED Fast for run error, if LED is OFF do the following:
{
digitalWrite(error, HIGH); //Turn error LED On
}else{ //If LED is on do this:
digitalWrite(error,LOW); //Turn error LED Off
}
}
}
if (analogRead(brakeCk) > onVal && analogRead(runSgnl) >= onVal && digitalRead(ignPin) == HIGH) //If the brake is pressed while
// remote start is on, do this:
{
digitalWrite(accPin, LOW); //Turn off Accessory Power
digitalWrite(ignPin, LOW); //Turn off Ignition Power
digitalWrite(error, HIGH); //***turn the error LED on***
delay(2000); //***for 2 seconds
digitalWrite(error,LOW); //***to show the user that the remote start system is now off***
enginetimerSw = 0; //Turn of engine run timer
//insert future honk code to notify user that engine run has been cancelled by brake push
}
if (enginetimerSw == 1 && millis() - engineTime > 900000) //condition for engine run timer
{
digitalWrite(accPin, LOW); //turn accessory power off
digitalWrite(ignPin, LOW); //turn ignition power off
enginetimerSw = 0; //Turn off engine run timer, resets next time called upon
//insert future honk code to notify user of engine run time END
}
if (analogRead(walkBtn) > onVal) //if walk away button is pressed do this:
{
if (analogRead(runSgnl) > onVal) //If run signal from engine is on do this:
{
if (digitalRead(ignPin) < onVal) //If internal ignition output is curretnly off do this:
{
digitalWrite(ignPin, HIGH); //Turn Ignition power on
digitalWrite(accPin, HIGH); //Turn Accessory Power on
enginetimerSw = 1; //Start enine run timer
//insert Future honk code for successful walk away to notify user engine run function is on
}
}
}
}
//***************************************END of Main void LOOP************************************************************************
//
//***************************************Begining of Startup Procedure********************************************************
void startUp() //one time code starting up vehicle
{
unlocktimerCheck = 0; //Turn off unlock check Timer
startVal = 0; //reset start value to zero so this code can be called for again
//insert future honk code to notify user that engine is being called to start
if (analogRead(brakeCk) < onVal && analogRead(runSgnl) < onVal && digitalRead(ignPin) < onVal) //check all inputs before proceding
{
digitalWrite(ignPin, HIGH); //Turn Ignition power on
digitalWrite(accPin, HIGH); //Turn Accessory Power on
delay(3000); //Wait three seconds for fuel pump to prime
digitalWrite(strtPin, HIGH); //Start Cranking engine
delay(1000); //Wait one second
digitalWrite(strtPin, LOW); //Stop Cranking engine
delay(6000); //Wait 6 seconds before checking for engine run Signal
if (analogRead(runSgnl) < onVal) //Check for engine run Signal, if not on do this:
{
digitalWrite(strtPin, HIGH); //Start Cranking engine
delay(1000); //Wait one second
digitalWrite(strtPin, LOW); //Stop Cranking Engine
delay(6000); //Wait 6 seconds before checking for engine run signal
}
}
if (analogRead(runSgnl) < onVal && digitalRead(ignPin) == HIGH) //if engine failed to start do this:
{
digitalWrite(accPin, LOW); //Turn Accessory Power off
digitalWrite(ignPin, LOW); //Turn Ignition Power off
enginetimerSw = 0; //Set engine run timer to 0, will reset next time it is called
strterrorVal = 1; //Set starter error value to 1 for error led flash
//Insert future Honk code for failed startup
}
if (analogRead(runSgnl) > onVal && digitalRead(ignPin) == HIGH) //if engine is running succesfully do this:
{
//insert future honk code for sucsesful startup
enginetimerSw = 1; //Set engine run timer on (time of run detirmined within engine run timer code)
}
}
//********************************************END of Startup**********************************************************************
//
//*****************************************Shut Down Code____RUNS ONCE when called for from main loop**********************************
//
void shutDown() //Shutdown Code
{
locktimerCheck = 0; //Turn off lock check Timer
sdVal = 0; //Set Shutdown count Value to 0
digitalWrite(accPin, LOW); //Turn off Accessory Power
digitalWrite(ignPin, LOW); //Turn off Ignition Power
enginetimerSw = 0; //Turn off engine run timer
//insert honk code for shutdown notification
} //END of Shutdown procedure