Ok guys, first thank you for having patience with me and helping out. (my only coding skills where with bash and at age 44 I am not that quick anymore
About adding a decimal to the int, I read somewhere that automatically triggers a float calculation or creates a workaround for rounding up values before calculation was finished so I changed them to float.
Also removed the calibration calculation for sake of speed.
Using a while or a for loop is not an option, onboard the puller life is pretty hard and I don't want to risk a complete engine failure because the software is waiting for a signal that is not gonna come due to a broken wire or other damage.
In the past I had an electronic device doing the same thing, an infrared transmitter/receiver combo triggered an NE555 which controlled the duration so that is why I want to use interrupts, intimidate response nomather what.
Aren't all digital ports usable as interrupts? Or do I need to add some extra function to accomplish that?
/*
Cuprum-2.0 simplified common rail injection system.
Use interrupts for timing injections cycle, a timing disc (alternator) is mounted to camshaft.
Attach the center pin of a potentiometer to pin A0, and the outside pins to +5V and ground, output will be 0-1023.
Potentiometer used is stereo, second line will be used to control injection pump pressure.
Attach timing signals: cyl1 pin 10, cyl2 pin 11, cyl3 pin 12 and cyl4 pin 13.
Attach injector hardware: cyl1 pin 4, cyl2 pin 5, cyl3 pin 6 and cyl4 pin 7.
*/
// setup pin for throttle potentiometer:
int ThrottlePin = A0;
float ThrottleValue = 0;
// define throttle calibration values:
// 9000rpm = 150rps, 1000000us/150 = 6666us per rotation.
// 6666/360*30 = 555us injection time, 30 crank degrees at 9000rpm.
// 1024/555 = 1.8
float ThrottleCalib = 1.85;
// setup timer pins for interrupts:
int cyl1 = 10;
int cyl2 = 11;
int cyl3 = 12;
int cyl4 = 13;
volatile int state = LOW;
// setup pins for injector drivers:
int inj1 = 4;
int inj2 = 5;
int inj3 = 6;
int inj4 = 7;
void setup()
{
// attach action when interrupt gets actived:
pinMode(cyl1, OUTPUT);
attachInterrupt(1, inject1, RISING);
pinMode(cyl2, OUTPUT);
attachInterrupt(2, inject2, RISING);
pinMode(cyl3, OUTPUT);
attachInterrupt(3, inject3, RISING);
pinMode(cyl4, OUTPUT);
attachInterrupt(4, inject4, RISING);
// initialize the digital injector pins as an output:
pinMode(inj1, OUTPUT);
pinMode(inj2, OUTPUT);
pinMode(inj3, OUTPUT);
pinMode(inj4, OUTPUT);
}
void loop()
{
// read the input on analog pin 0:
ThrottleValue = analogRead(ThrottlePin);
// recalculate throttle value to wanted microseconds:
ThrottleValue = ThrottleValue / ThrottleCalib;
// check the interrupts:
digitalWrite(cyl1, state);
digitalWrite(cyl2, state);
digitalWrite(cyl3, state);
digitalWrite(cyl4, state);
}
void inject1()
{
digitalWrite(inj1, HIGH);
delayMicroseconds(ThrottleValue);
digitalWrite(inj1, LOW);
}
void inject2()
{
digitalWrite(inj2, HIGH);
delayMicroseconds(ThrottleValue);
digitalWrite(inj2, LOW);
}
void inject3()
{
digitalWrite(inj3, HIGH);
delayMicroseconds(ThrottleValue);
digitalWrite(inj3, LOW);
}
void inject4()
{
digitalWrite(inj4, HIGH);
delayMicroseconds(ThrottleValue);
digitalWrite(inj4, LOW);
}