hello, ive been working on converting a small engine into EFI. i have 95% of the circuits working. and they all work with the arduino on their own. im now trying to put them all together into 1 sketch.
the basic idea of the program, my crank position sensor is hooked up to an interrupt pin. the interrupt will record the run time (mils) and compare it to the last time the interrupt was triggered, there is also a threshold to reset the counter, because ill have a few teeth on the crank wheel filled in, providing a longer pulse to mark the end of each cycle. if the time is below the threshold, it will just increment the counter by 1. the interrupt should also flip 2 variable between true and false depending on the counter. these must be timed accurately, because they are for the ignition events and the fuel injection command. i was hoping someone could take a look at my code and either point out some mistakes or maybe ideas of a different way to approach this problem. all help is appreciated.
// **** ISR **** //
bool INJcmd = false;
bool IGNcmd = false;
volatile byte CPC = 0;
// **** INPUT **** //
byte TPS = A0;
byte CKP = 2;
// **** OUTPUT **** //
byte INJ = 6;
byte IGN = 5;
void setup(){
pinMode(INJ, OUTPUT);
pinMode(IGN, OUTPUT);
digitalWrite(IGN, HIGH);
pinMode(TPS, INPUT);
pinMode(CKP,INPUT);
attachInterrupt(digitalPinToInterrupt(CKP), CrankInterrupt, RISING);
long historyMills = 0;
long time = 0;
long compaireTime = 0;
long CThreshold = 0;
}
void loop(){
while (IGNcmd = true){ ignition();}
while (INJcmd = true){ injector();}
digitalWrite(IGN, HIGH);
analogWrite(INJ, 0);
}
void injector(){
digitalWrite(INJ, HIGH);
}
void ignition(){
digitalWrite(IGN, LOW);
}
void CrankInterrupt(){
long historyMills;
long currentMills = millis(); // records current time
long compaireTime = currentMills - historyMills; // compaires current time to history time
long CThreshold = compaireTime * 2.5;
if (compaireTime >= CThreshold){
CPC = 0; //RESET
}
else{
CPC++;
if (CPC = 13){IGNcmd = !IGNcmd;}
if (CPC >= 6 && CPC < 10){INJcmd = !INJcmd;}
}
historyMills = currentMills;
currentMills = 0;
compaireTime = 0;
CThreshold = 0;
}
thx in advanced !