Seeking advice on code feasibility

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

The compiler said no such thing. All it reported was that it was able to compile the code. Whether it does what you want is a different thing altogether

What is the sketch meant to do, which Arduino board are you using, what hardware is connected to the Arduino and how is the project powered ?

Please post a schematic of your circuit, A photo of a hand drawn circuit is good enough

1 Like

Before you came up with this code, did you come up with any code like it but way simpler?

It is unusual to just write out a substantial sketch and then wonder if it will work.

What happens when you try it? Unless it is controlling a nuclear power plant or something, you ought to be able to test it without the possibility of much going wrong other than seeing that the code does not work.

If it would, in fact, be dangerous to run if you did not know it was perfect, you should work to separate the logic of the code from anything it can do that would be dangerous.

And no one here wants to say "looks good, engage".

Especially since you haven't said anything about what it is supposed to do.

a7

1 Like

The code has good comments and has everything labeled well.

The remote start function makes me nervous about the safety of the system. Accidental start up during maintenance will be dangerous to the system and the mechanic.

The startup procedure is unique and likely to fail to start the engine most of the time. The usual method runs the starter until either the engine starts running or the starter run times out. Limiting the starter run time to 5-10 seconds per start attempt with 30 seconds delay minimum before reengaging the starter would be closer to the expected manual start procedure. Reading the owners manual for your vehicle start process and changing your code to match would be much better.

The way the code determines if the engine is running does not allow for detecting when the engine starts running. Without this information, there is no way to correctly run the starter. Running the starter too long risks damaging the starter system. Observing the engine RPM directly is the best method.

Given the information provided in the code, the vehicle wiring and parts selection are very critical to the safety of the vehicle and nearby people. Please get confirmation from someone knowledgeable to ensure that the system will not cause electrical failure or fires.

Good luck with your project.

@xenon94alpharam because I have no life had a few minutes to waste, I started to read your code.

And now time is up. But a few questions.

You use analogRead() quite a bit, but always in a way that could be digitalRead(). Do you know that most analog pins can be used as digital pins?

Are the functions shutDown() and startUp() only run once every reset or power cycle of the device?

a7

1 Like

Lots of problems with that code, especially your use of analog inputs to read digital signals.

Then for example, this test will always be true, and is a grave error:

if (digitalRead(ignPin) < onVal) //If internal ignition output is curretnly off do this:

It is very important to understand that the electrical environment of an automobile is extremely harsh and noisy, especially during engine starts, and is most definitely not a place where complete beginners should start.

Arduinos are rapidly destroyed by voltage transients, and then can sometimes malfunction in rather spectacular ways.

I will. Guy two blocks away just put a brand new Ford F350 in the driveway :metal: :stuck_out_tongue:

Have you tried this with a simulated circuit, LEDs and the Serial Monitor to see if things are doing what you think they are doing when you think they are doing them?

I'd copy what you have, add lots of Serial monitor feedback to the copy and play around with the mocked up circuit to see what works as intended, what doesn't.

I'll bet nobody here is about to set all this up and test drive it for you, it's up to you to do everything and if you have a specific problem, ask how to fix that.

@xenon94alpharam Would you like input from the helpers here on the hardware feasibility as well as the software feasibility?

Also, I am curious about the significance of this vehicle to you. Is this an old project vehicle that has low resale value and low reliability expectations (not needed for transportation). Or is this a good vehicle that you need to maintain its value, reliability, and availability?

Looking forward to hearing back from you on this project.

hi there. thank you for the reply, i have done very simple code via the starter manual it came with. i will do some more research into this to make it better,
Thank for your concerns on saftey, i will now put a switch to cut power to the module if any work is to be done n the vehicle. i way it detects if the engine is running will be by determining the PWM voltage coming from one of my injector signal wires from my BCM Hence why im using analog read. also regarding the start procedure i only want the module to send a one second start signal as the ECU of my vehicle will continue to crank the engine until t starts or 10 seconds MAX.

also the startup and shutdown code is only to be run once if called for from the main loop

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.