Notifications
Clear all

Fuel injectors controlled from Arduino

33 Posts
4 Users
1 Likes
8,609 Views
(@zeferby)
Member
Joined: 5 years ago
Posts: 355
Topic starter  

@bmann

If I correctly understood your question here, you want to vary the 25ms between injectors but keep each injector on a 5ms delay ?

Eric


   
Bmann reacted
Quote
frogandtoad
(@frogandtoad)
Member
Joined: 5 years ago
Posts: 1458
 
Posted by: @zeferby

@bmann

If I correctly understood your question here, you want to vary the 25ms between injectors but keep each injector on a 5ms delay ?

The picture painted to me sounds to me like a request for a PWM duty cycle type scenario?, which could easily be set up with a for loop and the introduction of a couple of delays.

I'm not usually a good guesser, so let's see what Brian comes back with πŸ˜€

Β 


   
ReplyQuote
(@bmann)
Member
Joined: 4 years ago
Posts: 10
 

Yes, Eric that sounds correct.Β  Maintain the 5ms on time for each injector while varying the delay between each event.

Β  I would like to be able to increase the time between injector firing up to 400ms ( like a engine cranking at 150 RPM) down to a 1ms delay between injectors firing (5,000 RPM). Note that this is for a 4 cylinder 4 stroke ICEΒ  so the total amount of delay is divided by 2 to get theΒ  REV / ms, then divide that by 60,000 to get the RPM.Β  Any help greatly appreciated.

Β  Β This is what I have right now:

// the setup function runs once when you press reset or power the board
void setup() {
// initialize digital pin LED_BUILTIN as an output.
pinMode(8,OUTPUT);
pinMode(7,OUTPUT);
pinMode(4,OUTPUT);
pinMode(2,OUTPUT);
}

void loop() {
digitalWrite(8, HIGH);
delay(5);
digitalWrite(8, LOW);
delay(25 );
digitalWrite(7, HIGH);
delay(5);
digitalWrite(7,LOW);
delay(25);
digitalWrite(4, HIGH);
delay(5);
digitalWrite(4,LOW);
delay(25);
digitalWrite(2, HIGH);
delay(5);
digitalWrite(2, LOW);
delay(25);
}


   
ReplyQuote
(@zeferby)
Member
Joined: 5 years ago
Posts: 355
Topic starter  

@bmann

Ok Brian so your loop() needs to run very quickly when you need a 1 ms timing...

I tried to create something that should work; it compiles ok but not having the hardware, I could not test it.

It does not "freeze" the MCU with longish delay() calls and could also be adapted to more/less cylinders.

Feel free to report bugs or ask questions if anything is unclear.

Eric


   
ReplyQuote
(@pugwash)
Sorcerers' Apprentice
Joined: 5 years ago
Posts: 923
 

   
ReplyQuote
(@zeferby)
Member
Joined: 5 years ago
Posts: 355
Topic starter  
Posted by: @pugwash

@zeferby

First, my compliments on a bit of monumental coding!

But don't you think it is a bit too complex for a beginner?

Thanks, and yes you're probably right...between 1am and 2am I've got carried away... ?Β 

Β 

I'll try to find some time today to go a bit more "step-by-step" on this.

Eric


   
ReplyQuote
(@pugwash)
Sorcerers' Apprentice
Joined: 5 years ago
Posts: 923
 

   
ReplyQuote
(@zeferby)
Member
Joined: 5 years ago
Posts: 355
Topic starter  
Posted by: @pugwash

This makes me think that for some noobs, the loop() function is a bit of a mystery or black magic.

We could say that there is a "hidden" main function - which is the program itself, using the ino sketch setup() and loop() - looking like :

void main() {

Β  setup();

while (true) {

Β  Β  loop();
}

}


Eric


   
ReplyQuote
(@bmann)
Member
Joined: 4 years ago
Posts: 10
 

Thank you every one for the information that you are sharing. I have so much to learn on so many levels.Β  Β I am hoping to have a chance to try (@zeferby) code first and then the others.Β  Β It so so cool that you all are willing to help out.Β  I have been reading a few basic electronics books in the past few monthsΒ  and getting the chance to apply principles is awesome.Β Β 


   
ReplyQuote
(@zeferby)
Member
Joined: 5 years ago
Posts: 355
Topic starter  

@bmann @pugwash

So I'll try to go over successive refactoring steps starting with Brian's initial code here, to explain the reasoning behind the code I uploaded yesterday.

Please @pugwash put your code reviewer sharp eye to work ! ? ? ?Β 

Β 

Also : while writing this first iteration, I came upon a ? bug in my EFI00.ino code here with the variable declaration of injectTime : it needs to support values up to 400, so a byte is too small !

Β 

Step 01 is compiling ok for an Arduino UNO...at least the syntax is correct. ?Β 

Sketch uses 1100 bytes (3%) of program storage space. Maximum is 32256 bytes.
Global variables use 9 bytes (0%) of dynamic memory, leaving 2039 bytes for local variables. Maximum is 2048 bytes.

Here is the code (contents of the attached file) :

//BrianEFI-v01

/*
Refactoring Step 01 :
=====================
Let's to avoid manipulating "hard numbers" all over the code, make the code easier to understand
and facilitate search/replace operations.

We'll #define some symbols for constants
- pins for injectors
- injection time (=5ms, fixed)
and we'll create a variable to hold the delay between injectors, since we want it to vary
- injectionDelay (=25ms but will vary later on)

...and we'll replace all the "hard numbers" in the code with the above without changing it's structure.

Finally let's add the "opening statement" of { } blocks as a comment after their closing bracket,
so that we know who/where/what we are when the code gets more complex :-)

And after all that, for code readability, we add some blank kines here and there,
and we select the full source code (Ctrl+A) then apply the right-click menu option "Auto-Format".

!!! NOTE !!!
============
The injectDelay is declared as an unsigned int ( https://www.arduino.cc/reference/en/language/variables/data-types/unsignedint/),
which can hold values between 0 and 65535 on the UNO.
If we count milliseconds, that gives 65 seconds, which is more than enough for our needs.
BUT if we have to count microseconds insted of milliseconds, we would be limited to about 65ms => we'll probably need to change that
to unsigned long, which can hold much bigger numbers ( https://www.arduino.cc/reference/en/language/variables/data-types/unsignedlong/).
*/

//injection time in ms
#define INJECT_TIME_MS 5

//injector pin numbers
#define PIN_INJECT1 8
#define PIN_INJECT2 7
#define PIN_INJECT3 4
#define PIN_INJECT4 2

//injection delay between injectors, in ms
unsigned int injectDelay = 25;


// the setup() function runs once when you press reset or power the board
void setup() {

// initialize digital pins as output for injectors.
pinMode(PIN_INJECT1, OUTPUT);
pinMode(PIN_INJECT2, OUTPUT);
pinMode(PIN_INJECT3, OUTPUT);
pinMode(PIN_INJECT4, OUTPUT);

}//void setup()


// the loop() function runs continuously
void loop() {

digitalWrite(PIN_INJECT1, HIGH);
delay(INJECT_TIME_MS);
digitalWrite(PIN_INJECT1, LOW);

delay(injectDelay);

digitalWrite(PIN_INJECT2, HIGH);
delay(INJECT_TIME_MS);
digitalWrite(PIN_INJECT2, LOW);

delay(injectDelay);

digitalWrite(PIN_INJECT3, HIGH);
delay(INJECT_TIME_MS);
digitalWrite(PIN_INJECT3, LOW);

delay(injectDelay);

digitalWrite(PIN_INJECT4, HIGH);
delay(INJECT_TIME_MS);
digitalWrite(PIN_INJECT4, LOW);

delay(injectDelay);

}//void loop()


Β 

Β 

Eric


   
ReplyQuote
(@pugwash)
Sorcerers' Apprentice
Joined: 5 years ago
Posts: 923
 

@zeferby

Eric,

now all you need is:

void loop(){
int tDelay = AnalogRead(A0);
int injectDelay= map(tDelay, 0, 1023, 25, 400);

Brian can tweak these mapped values to suit.Β 

and I wouldΒ  change

delay(injectDelay);

to

delay(injectDelay - INJECT_TIME_MS);

Otherwise you are adding a further 5ms to the cycle time.

Agreed??


   
ReplyQuote
(@zeferby)
Member
Joined: 5 years ago
Posts: 355
Topic starter  

@pugwash

Hum, Steve actually I read the initial code like : 4 x [5ms ON + 25ms OFF]

and Brian wrote he would like to have the 25ms delay vary between 1ms and 400ms , so the full cycle would be between 4x [5ms ON + 1ms OFF] for maximum speed/low delay and 4x [5ms ON + 400ms OFF] for minimum speed/high delay -> Remember the delay is reversed to the "throttle"

Β 

Anyway we have one time duration which is variable, meaning that we can :

  • either manage the OFF delay before the next cylinder (currently 25ms),
  • or manage the full ON+OFF cycle time for a cylinder (currently 5+25=30ms) with your substraction above.

Eric


   
ReplyQuote
(@pugwash)
Sorcerers' Apprentice
Joined: 5 years ago
Posts: 923
 

@zeferby

I read the following

If I correctly understood your questionΒ here, you want to vary the 25ms between injectors but keep each injector on a 5ms delay ?

as 25ms between injections, and injection duration 5ms, then further down the thread, he changed this to 25 to 400ms with 5ms injection duration depending on crankshaft rotation speed or RPM.

But I also think that his maths are wrong here:

I would like to be able to increase the time between injector firing up to 400ms ( like a engine cranking at 150 RPM) down to a 1ms delay between injectors firing (5,000 RPM). Note that this is for a 4 cylinder 4 stroke ICEΒ  so the total amount of delay is divided by 2 to get theΒ  REV / ms, then divide that by 60,000 to get the RPM.Β  Any help greatly appreciated.

But now he can alter the sketch himself to suit his purposes.


   
ReplyQuote
(@zeferby)
Member
Joined: 5 years ago
Posts: 355
Topic starter  

@pugwash

Lol I had to go back to this video to understand Brian's math : in 4-stroke, we inject once per cylinder for every 2 revolutions of the engine.

So the total 4-cylinder 4-stroke cycle of 4x[inject(5ms) + delay] corresponds to 2 engine revolutions.

So 1 revolution <=> 4x[inject+delay] /2

thus : RPMs = 60000 / (4x[inject+delay] /2) = 30000 / [inject+delay]

and : delay (ms) = 30000/RPMs - inject(ms)

Eric


   
ReplyQuote
(@bmann)
Member
Joined: 4 years ago
Posts: 10
 

@zeferby

I am trying to take a closer look to see what I am missing (something for sure!) uploaded this sketch and I have PIN 7 high all the time.Β  I started to get that slight smell that lets me know something not quite right.Β  Β I reloaded my original sketch and all 4 injectors fire normally.Β  Β I have a variable sensor type resistor that I am using as a pot (more regular 10k pots on order). I do not think that would cause a problem,Β  I was also trying a few fixed resistors 10k, 22k PINΒ  A0 to 5vΒ  also tried A0 to ground no change.Β  Let me know if you have any ideas ,Β  I appreciate your help.


   
ReplyQuote
Page 1 / 3