122Views7Replies

Author Options:

Help with arduino code Answered

Hi Peoples

I'm trying to code an Arduino, to use as an ignition sequencer, so I can get rid of the distributor on an engine and replace it with coil on plug system. Its a 4cylinder with fuel injection and has a 0-5 pulse to drive the coil. I will have to put a hall effect sensor on the cam to act as a reset.

I've set it up on the bench with 2 switches one to simulate the reset and one to simulate the ignition signal.

There are 4 LEDs to simulate each coil

So far I can push the ignition signal button each time and it will light up each LED in the correct sequence. The problem is the LED stays on when the button is released, It should only light up when the button is pressed.

The "delay" in the slows it down so I can see what's happening I would like to remove it or use a much smaller value when it is on the engine.

The next problem is the reset button I have no idea where to start. I need to be able to push the reset and the sequence start from the first LED

I can see having the delay in the code could potentially cause problems later.

Any help would be greatly apprecated

thanks

// SEQUENCER WITH BUTTON
int ledPin1 = 13;

int ledPin2 = 12;

int ledPin3 = 11;

int ledPin4 = 10;

int butPress1 = 9;

int countBP = 0;

void setup()

{

pinMode(ledPin1, OUTPUT);

pinMode(ledPin2, OUTPUT);

pinMode(ledPin3, OUTPUT);

pinMode(ledPin4, OUTPUT);

pinMode(butPress1, INPUT);

}

void loop()

{

digitalWrite(ledPin1, LOW);

digitalWrite(ledPin2, LOW);

digitalWrite(ledPin3, LOW);

digitalWrite(ledPin4, LOW);

countBP = digitalRead(butPress1);

while (countBP != 0)

{

switch (countBP)

{

if (countBP == 0)

{

case 1:

digitalWrite(ledPin1, HIGH);

digitalWrite(ledPin2, LOW);

digitalWrite(ledPin3, LOW);

digitalWrite(ledPin4, LOW); delay(100);

countBP = countBP + digitalRead(butPress1);

break;

}

if (countBP == 1)

{

case 2:

digitalWrite(ledPin1, LOW);

digitalWrite(ledPin2, HIGH);

digitalWrite(ledPin3, LOW);

digitalWrite(ledPin4, LOW); delay(100);

countBP = countBP + digitalRead(butPress1);

break;

}

if (countBP == 2)

{

case 3:

digitalWrite(ledPin1, LOW);

digitalWrite(ledPin2, LOW);

digitalWrite(ledPin3, HIGH);

digitalWrite(ledPin4, LOW); delay(100);

countBP = countBP + digitalRead(butPress1);

break;

}

if (countBP == 3)

{ case 4:

digitalWrite(ledPin1, LOW);

digitalWrite(ledPin2, LOW);

digitalWrite(ledPin3, LOW);

digitalWrite(ledPin4, HIGH); delay(100);

countBP = countBP + digitalRead(butPress1);

break;

}

default:

digitalWrite(ledPin1, LOW);

digitalWrite(ledPin2, LOW);

digitalWrite(ledPin3, LOW);

digitalWrite(ledPin4, LOW);

countBP = 0;

break;

}

}

}

Comments

The forums are retiring in 2021 and are now closed for new topics and comments.
0
Downunder35m
Downunder35m

11 months ago

Am I underthinking the problem when I think of a transistor ignition?
Back in my days of fiddling on the old Beetle I did not need a microcontroller...

The dizzy provides the contact and instead of firing the coil directly a beefy transistor was used to keep the timing steady and the pulses strong.
If I understand correctly (please correct me ;) ) then you want to use this single pulse that worked fine for the original setup to start a sequence with the correct timing to provide an ignition pulse for every single cylinder individually?
And you still need a microcontroller - I guess to provide timing adjustments based on the RPM and load?

If I got that correct so far there might be an alternative:
How about using 4 tiny metal blocks on the rotating shaft of the dizzy and ONE inductive sensor to get the intended signal?
Means you get a timing signal for every cylinder, using 4 blocks and sensors or even suitable hall sensors would give you individual inputs for each cylinder.
With that you would ony need to advance the timing based on the RPM signal you can generate from the input.
Like a Dyno tune so to say.
Normal from idle to 200RPM, 3 degree fast from 2000 to 4000 and 6 degrees fast above this.
Even with a reduced timing once you get to the red line to protect the engine.
Don't know if I over od underthinking it but somehow this approach seems to be easier and more reliable than a programmed timing sequence.

0
liquidhandwash
liquidhandwash

Reply 11 months ago

Thanks downunder. the reason I want to get rid of the distributor is I'm putting a front wheel drive engine into a rear wheel drive car. The distributor hangs out the back of the engine and wont fit in its new home. The advance will be taken care of by the ECU. The Arduino code is very close do doing what i need, it just has 2 issues. I cant figger out how to get it to restart with the second input. And the outputs need to go low when first input is low. With this code there is always one LED lit when there is no input, so if the engine is not running with the ignition on, a coil would be energized which is not good.

0
Downunder35m
Downunder35m

Reply 11 months ago

Wouldn't it be enough to add another If for the input ?
I mean: to keep the outputs off while the first input is low you just need to add this statement to the code - or did I miss it?
I tried twice but still unsure: Do you have a matching amount of { and } in the code?
Tried to copy the code into Notepad but it did not take the proper format as it should.


0
liquidhandwash
liquidhandwash

Reply 11 months ago

Thanks downunder, that is what ive been trying to do but so far its been a fail. the .ino file is at the top of the page, which can be put into the arduino editor

0
Downunder35m
Downunder35m

Reply 11 months ago

Ok, I did not go and test anything on a board as don't have the spares at hand but:
You add to the value of the "CountBp" but you don't seem to have included the case for going over the highest value allowed ;)
It would make sense to add a check within the loop for the counter value and once gone to high to set it back to the start value.
If you tie this together with the signal for the reset it should fix this problem.
Meaning either the counter going too high or the input from the reset will always set the value to zero.
You might have to do this outside the loop if it does not work as planned withthe variables inside the loop.

As for the light up time, if I understand correctly:
The delay is for you only.
But you want no delay for the real thing or something as short as possible?
For these ignition coils you should be able to find info on how long the digital ingnition impulse needs to be for them to still work properly.
Or calculate based on RPM ;)
The later will tell you for a max RPM how long the time between pulses is.
If you have a delay or computing time below about half this time you should be fine.
8000 revs per minutes equals to about 130 RPS - rounds per second.
So about 35 times a second you need an ignition pulse at 8000RPM.
An "on time" of 60ns should work here, much higher if have lower RPM ranges...

0
TinkerElectric
TinkerElectric

11 months ago

Hi! How you structured your 'switch statement' doesn't follow the syntax
https://www.arduino.cc/en/Tutorial/switchCase

instead of:
while (countBP != 0)
{
switch (countBP)
{
if (countBP == 0)
{
case 1:

it needs to be:
while (countBP != 0){
switch (countBP
case 0:

ie, you are testing for ==0, this happens by writing "case 0:" it basically is the 'if' statement.

This may resolve your problem.
0
liquidhandwash
liquidhandwash

Reply 11 months ago

Hi tinker i tried your idea, it didn't work as expected, the first led would not light up
thanks