advice needed for arduino as diesel injection brain...

Hello cyclegadget,

The parts for the injector and pump driver print are ordered so next week I can start with building the hardware.

Did some coding last week based on your idea, do have a question about it:
In the injection function you use done=0; and done=1; and at the end return done; why is that, for usage in messages when serial prints are added?

I did change things a bit, I moved time reading stuff directly into the interrupt function to make it more accurate.
Also I changed the way U used time based checks for start and stop injecting to add a "delay" before start injecting and to make it a bit more clear on when things are happening.

/*
  Cuprum-2.0 simplified 1-3-4-2 cylinder common rail injection system for Arduino Mega.
  
  Using interrupts for timing injections cycle, a timing disc (alternator) is mounted to camshaft.
  Throttle position is read from potentiometer.
  Pump pressure is bound to throttle position.
  
  Base rule for the system is that we want all fuel injected at 10 crank degree before TDP:
  - cylinder 2 gives pulse for cylinder 1
  - wait (180 degree at current rpm) - injection time (max 35 degree) - injection advance (10 degree at current rpm)
  - inject
  
  Attach the center pin of a potentiometer to pin A0, and the outside pins to +5V and ground, output will be 0-1023
  Attach timing signals to external interrupts: cyl1 pin 3, cyl2 pin 21, cyl3 pin 20 and cyl4 pin 19
  Attach injector drivers: cyl1 pin 2, cyl2 pin 4, cyl3 pin 6 and cyl4 pin 8
  Attach pump pressure to PWM pin 13
*/

// setup pin for throttle potentiometer:
int ThrottlePin = A0;
unsigned long ThrottleValue = 0;
// define throttle calibration values:
// we like to inject from -45 to -10 TDP making 35 degree injection duration max.
// crank duration in uS: (1000000/(rpm/60))/360*35
// at 10K rpm that is 583uS since the throttle has 1023 steps 1023/583=1.75
float ThrottleCalib = 1.75;
// base idle injectiontime is 50uS
unsigned long IdleDuration = 50;
// define microsecond timeframe between 2 injects = duration 180 degree crankshaft
unsigned long TimeFrame = 0;
unsigned long TimeOld = 0;
// define injection cycle times
unsigned long InjectionAdvance = 0;
unsigned long InjectionDuration = 0;
unsigned long currentmicros = 0;
// state of injectors
int InjectorState1 = LOW;
int InjectorState2 = LOW;
int InjectorState3 = LOW;
int InjectorState4 = LOW;
// injection start times
unsigned long start1micros = 0;
unsigned long start2micros = 0;
unsigned long start3micros = 0;
unsigned long start4micros = 0;
// setup timer pins for interrupts:
int cyl1 = 1;
int cyl2 = 2;
int cyl3 = 3;
int cyl4 = 4;
volatile int state = LOW;
// setup pins for injector drivers:
int inj1 = 2;
int inj2 = 4;
int inj3 = 6;
int inj4 = 8; 
// injection functions flags
volatile int state1 = LOW;
volatile int state2 = LOW;
volatile int state3 = LOW;
volatile int state4 = LOW;
volatile int Fuel1  = false;
volatile int Fuel2  = false;
volatile int Fuel3  = false;
volatile int Fuel4  = false;

void setup()
{
  // initialize the digital injector pins as an output:
  pinMode(inj1, OUTPUT);     
  pinMode(inj2, OUTPUT);     
  pinMode(inj3, OUTPUT);     
  pinMode(inj4, OUTPUT);
  // attach action when interrupt gets actived:
  pinMode(cyl1, INPUT);
  attachInterrupt(1, Fire1, RISING);
  pinMode(cyl2, INPUT);
  attachInterrupt(2, Fire2, RISING);
  pinMode(cyl3, INPUT);
  attachInterrupt(3, Fire3, RISING);
  pinMode(cyl4, INPUT);
  attachInterrupt(4, Fire4, RISING);
}

void loop()
{
  // check interupts
  digitalWrite(cyl1, state1);
  digitalWrite(cyl2, state2);
  digitalWrite(cyl3, state3);
  digitalWrite(cyl4, state4);
  // read throttle position
  throttle();
  // if the flag is true start the inject function 
  if (Fuel1 == true)
  {
    inject1();
  }
  if (Fuel2 == true)
  {
    inject2();
  }
  if (Fuel3 == true)
  {
    inject3();
  }
  if (Fuel4 == true)
  {
    inject4();
  }
}

// set flags    
void Fire1()
{
  // set flag
  Fuel1 = true;
  // set start time
  start1micros = micros();
  // calculate how many uS 180 degrees last
  TimeFrame = (micros() - TimeOld);
  TimeOld = TimeFrame;
}
void Fire2()
{
  // set flag
  Fuel2 = true;
  // set start time
  start2micros = micros();
  // calculate how many uS 180 degrees last
  TimeFrame = (micros() - TimeOld);
  TimeOld = TimeFrame;
}
void Fire3()
{
  // set flag
  Fuel3 = true;
  // set start time
  start3micros = micros();
  // calculate how many uS 180 degrees last
  TimeFrame = (micros() - TimeOld);
  TimeOld = TimeFrame;
}
void Fire4()
{
  // set flag
  Fuel4 = true;
  // set start time
  start4micros = micros();
  // calculate how many uS 180 degrees last
  TimeFrame = (micros() - TimeOld);
  TimeOld = TimeFrame;
}

// throttle reading
int throttle()
{
  // read the throttle input on analog pin 0:
  ThrottleValue = analogRead(ThrottlePin);
  // see if we are trying to have fun :-)
  if (ThrottleValue > 5)
    {
    // injection ends uS/360*350 to make sure all juice is injected 10 degree before TDP.
    InjectionAdvance = TimeFrame/360*10;
    // recalculate throttle value to wanted microseconds:
    InjectionDuration = ThrottleValue/ThrottleCalib;
    }
  // it seems we are idling :-(
  else
    {
    // make sure all juice is injected at TDP.
    InjectionAdvance = 0;
     // if we are running 900rpm (33333uS) we reduce injection time
    if (TimeFrame > 33333)
      {
      // reduce injection time
      IdleDuration=IdleDuration-5;
      InjectionDuration = IdleDuration;
      }
    // if we are running 800rpm (37500uS) we inject a bit longer
    else if (TimeFrame < 37500)
      {
      // expand injection time
      IdleDuration=IdleDuration+5;
      InjectionDuration = IdleDuration;
      }
    }
}

// injection functions
int inject1()
{
  // read time
  currentmicros = micros();
  // reset the flag that was caused by the interrupt
  Fuel1 = false;
  // this variable will show when the inject() function is complete. 0 = not complete, 1 = complete
  static int done = 0;
  // if the injector is closed see if it is time to open
  if(currentmicros - start1micros > (TimeFrame - (InjectionAdvance + InjectionDuration)) && InjectorState1 == LOW)
  {
    digitalWrite(inj1,HIGH);
    InjectorState1 = HIGH;
    done = 0;
  } 
  // If the injector is open and it is time to close the injector do so.
  if(currentmicros - start1micros > (TimeFrame - InjectionAdvance) && InjectorState1 == HIGH) 
  {
    digitalWrite(inj1, LOW);
    InjectorState1 = LOW;
    done = 1;
  }
  // return 0 or 1 to show if function has both opened and closed injector.
  return done;
}

int inject2()
{
}

int inject3()
{
}

int inject4()
{
}