Remote vehicle ignition - code help.

Hi guys, if any of you have been on the project guidence section within the last week, you might have seen my remote vehicle ignition thread.

I'm now upto the codeing part of the project, and having never coded anything other than basic HTML, it's quite daunting at first. I'm quite stuck, basically this is how the project pans out.

arduino with cell shield (havn't actually got the cell shield yet, so i'm working off of serial for now) i want it working like this: Arduino recieves serial command> fires the ON relay at pin 12> wait's 5 secs for fuelpump/glow plugs> checks input pin 4's state, if low, fires igniton pin 11>once input pin 4's state goes high, it turns off ignition pin 11. >if brakeswitch pin 3 is low nothing happens until 15 minutes has past, then code restarts, when brakeswitch pin 3 goes high, it restarts the code thus the ON pin goes low and is now waiting for serial input to tell it to start again.

Basically, upon serial input the car starts, and waits 15 minutes or for the brake switch to be depressed, whichever comes first. so the user gets to the car before the 15 minutes is up, and puts the key in and switches to the on position, thus closing the cars normal relays. then he/she presses the brake pedal, thus the arduino code loops back to the start and is ready for another serial input.

Is this at all possible? this is what i've come up with so far, and it doesn't work properly :(

int ignitionPin = 12;
int onPin = 11;
int ledPin = 13;
int brakePin = 3;
int alternatorPin = 4;
int usbnumber = 0;
void setup() {
    pinMode(onPin, OUTPUT);
    pinMode(ignitionPin, OUTPUT);
    pinMode(ledPin, OUTPUT);
    pinMode(4,INPUT);
    pinMode(3,INPUT);    
    Serial.begin(9600);
}
void loop() {
             if (Serial.available() > 0) {
        usbnumber = Serial.read();
    }
    if (usbnumber > 0) {
      
        digitalWrite(onPin, HIGH);
        delay(5000); 
    if (digitalRead(alternatorPin)==LOW) 
       {digitalWrite(ignitionPin,HIGH);}
       else
       {digitalWrite(ignitionPin,LOW);}
       
    if (digitalRead(brakePin)==LOW)
       {digitalWrite(ledPin,HIGH);}
       else
       {digitalWrite(13,LOW);}
       delay(1000);

         
        usbnumber = 0;
    }
}

cheers, Matt.

and it doesn't work properly

Have you read this?

Yes, i have.

But to be honest, i don't want to spend months learning C++ for something that seems reasonably easy, Nor do i have the time. And i thought maybe someone here can help me through this.

Including this bit?

Describe what you expected to happen, and what actually happened. Not just “it doesn’t work”.

If you read my whole post properly, it explains what i want and what i expected the code to do from the knoledge i already have on codeing arduino. I havn't coming here asking for someone to code it for me, nor have i come here sounding like an idiot asking "plz hlp me, idno how to code my arduino"

I've asked for guidence. and i don't think that it would take much of a persons life to hep me out with some some of the code, than it would if i spent a few hours learning C++ for 6 months+ after a 12 to 14 hour day at work.

Regards, Matt.

it explains what i want and what i expected the code to do from the knoledge i already have on codeing arduino.

But it doesn't explain how the behaviour of the code you have written differs from that requirement. You're the one with the hardware - I'm not about to buy some crappy pickup just to try your sketch on.

I thought a user with over 12000 posts might've picked up on how it's wired, and judging on how i've written my first post, how it's wired is irrelevent as i've given my explanation on what i want the code to do.

But, like i mentioned in my post, i have a post under 'project guidence' if you care to take a look.

Basically, i have two relays, one for ON and one for IGNITION. i have a brake switch with a pull up resister (as if it was a push button) and another relay which when switched by the vehicles alternator, closes the contacts on another pull up resister switch.

Regards, Matt.

I thought a user with over 12000 posts might've picked up on how it's wired,

I'm good, but not psychic. (I managed to keep that deficiency quiet at the interview)

You still haven't explained what

doesn't work properly

means.

But, like i mentioned in my post, i have a post under 'project guidence'

Do you want me to merge the posts, so that people don't answer your questions here and there?

If you ever get around to answering the question of how what that code does differs from what you want it to do, and post more code, please don't post code like that again.

    if (digitalRead(alternatorPin)==LOW) 
       {digitalWrite(ignitionPin,HIGH);}
       else
       {digitalWrite(ignitionPin,LOW);}

is way too hard to read or modify.

    if (digitalRead(alternatorPin)==LOW) 
    {
       digitalWrite(ignitionPin,HIGH);
    }
    else
    {
       digitalWrite(ignitionPin,LOW);
    }

is the same code with judicious white space, which is much easier to read (and see the logic of, at a glance).

There is nothing in that initial code that knows anything about "so the user gets to the car before the 15 minutes is up". If that is the problem, why not say so?

@OP, To summarise, you have a project that you admit is

reasonably easy

but you're not willng to expend time or effort in learning C++, yet you expect others to invest their time and effort in guiding you (which, given the nature of the problem probably means writing the code) towards executing it.

Is that a reasonable synopsis?

Ugh.

When i run the code, and give a serial input, the ON relay fires < Good.
Then after 5 seconds, if the alternatorPin is low, it fires the IGNITION relay <Good.
Then when i push the button on alternatorPin (sending it high) the IGNITION relay doesn’t switch off. i thought it would as in the code it’s stated that if alternatorPin goes high, it makes ignitionPin low, then it should proceed to the next line of code.
But the arduino doesn’t wait around for this to happen, it just sees what the state is at exactly 5 seconds and makes it’s judgement, and moves onto the next lines of code.

Regards, Matt.

You are not using the internal pullup resistors. So, you need to have external pull-up or pull-down resistors.

How are the switches wired?

Your problem sounds like a floating pin condition.

Even if the switches are wired correctly, the only time that the pins are read is after the delay. That requires that you be holding the switch down when the delay ends. Are you?

I am using external pull up resisters. When i hold the switch down after the delay, the ignition relay doesn't close, which is fine. (basically the car is running, it doesn't need to fire the ingition relay) but when i release the switch, it doesn't fire the ignition relay, because the code has already moved on to the next lines.

The thing i want is, after the delay, i want it to check to see if the alternator pin is low (car is not running), if it is, then fire ignition relay until the alternator pin goes high, i.e, the car has started. i could just add a delay instead of having it sense that the car has started, but on cold mornings, it takes a little bit longer for the car to start.

Regards, Matt.

The thing i want is, after the delay, i want it to check to see if the alternator pin is low (car is not running), if it is, then fire ignition relay until the alternator pin goes high, i.e, the car has started. i could just add a delay instead of having it sense that the car has started, but on cold mornings, it takes a little bit longer for the car to start.

No, the thing is you must, MUST, ABSOLUTELY MUST get rid of the delay()s. Nothing else happens during a delay.

Te blink without delay example bears studying.

You need to create a state machine. The car is on one state at a time - starting, running, waiting for start command, etc. There are some external actions that cause a state change (the start command arrives, for instance) and some internal events that cause a state change (the alternator starts outputting current, for instance or 5 seconds has gone by).

There are actions to be performed when the state changes (open this relay, close that relay, send a response that the car started/failed to start, etc.)

You need to keep track of what state you are in, and determine, on each pass through loop, whether it is necessary to change states. If so, perform any associated actions, and update the state variable(s).

Thanks for the reply. Yeh, someone mentioned in my other post that i would need to create a statemachine. I guess this is way beyond my ability, and like i mentioned, i honestly don't have the time to start from scratch learning C++ for months.

Would the statemachine require alot of codeing? are there any hidden tutorials out there about creating one? i've done some searches but havn't come up with a whole lot.

Regards, Matt.

Isn't it possible to create a global rule etc, so once after the 5second delay, the rule is if the alternatorPin is low, the ignitionPin is set high until the alternatorPin goes high? Then so long as the alternatorPin is HIGH, it waits either 10 minutes or for a brakePin to go HIGH, then loops the code back to the beginning waiting for another serial input?

I've done a little reading on state machines. I guess the tables for this would be as follows. completly scrap the brakePin, and instead use a 10 minute delay, if the user isn't in the car with the key in and switched to ON, then bad luck, he/she has to start the vehicle again.

State0: onPin = off, ignitionPin = off, alternatorPin = LOW. Upon recival of serial command, go to state 1.

State1: onPin = on, ignitionPin = off, alternatorPin = LOW. after 5 seconds go to state 2.

State2: onPin = on, ignitionPin = on, alternatorPin = LOW. once alternatorPin becomes HIGH, go to state 3, if alternator pin is alrady HIGH, go to state0 (in case if someone floods the serial)

State3: onPin = on, ignitionPin = off, alternatorPin = HIGH. once 10 minutes has elapsed go to state 0

Does this seem right?

Does this seem right?

It looks pretty good to me. Make sure that you have considered all possibilities. To ensure that you have, find a 10 year old, and have them start the car, using only the instructions that you have written down. If the 10 year old can start the car, without damaging anything, then the Arduino should have no problems starting it.

Test each part of the code separately, before trying to combine all the code into one program.

For instance, make sure that you can detect each switch, and that the alternator is read correctly, after starting the car manually. Make sure that the relays open and close when they are supposed to, with nothing connected to them. Make sure that they still open and close correctly with a load on them.

find a 10 year old, and have them start the car, using only the instructions that you have written down.

Are you being sarcastic?

The way i'm wireing is is by having a seperate set of relays, along side the ones the car has. if the car is to be started manually, which will happen most of the time, the arduino has nothing to do with it. The arduino only controls it's own relays based on the serial command.

Well, like i said, i know very little about C++ or any programming for that matter. So a little help with the actual code would be nice, if it's not too much trouble?

And, the wireing side of this is completed, i've tested the relays by controlling them individually over wifi. i'm just in the middle of having a proper pcb for the arduino's power supply made. I'm just stuck on the code.