int zuendSignal = 2; //Standard auf 5V, geht auf 0V bei Signal
volatile byte isIgn = 0; //FLAG byte für Zündsignal
int isrAllowed = 0;
//internal Simulator of ignition Signal
int millisBefore = 0;
int ignFlag = 0;
//for RPM Calculation
int ignSigMillisBefore = 0;
float rpm;
//MAP
int mapSensor = A1;
//Injector Actuating
int injector = 3;
//ignition Actuating
int ignition = 4;
void setup() {
Serial.begin(2000000);
pinMode(zuendSignal, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(zuendSignal), zuendISR, FALLING);
pinMode(mapSensor, INPUT);
pinMode(injector, OUTPUT);
pinMode(ignition, OUTPUT);
}
void loop() {
//this is for cycletime calculation
int microsBeforeStartCycle = micros();
int timeNow = millis();
//internal Ignition Signal Simulator...
//Serial.println(timeNow);
//Serial.println(millisBefore);
if(timeNow>millisBefore+10) //10 ms makes real 11-12ms
{
int difference = timeNow-millisBefore;
//Serial.println(difference); //just for time testing
millisBefore = timeNow;
ignFlag = 1;
}
//this is the fuel table which is called on every loop
int fuelTable[5][15] = {
{500,500,500,500,500,500,500,500,500,500,500,500,500,500,500},
{500,500,500,500,500,500,500,500,500,500,500,500,500,500,500},
{500,500,500,500,500,500,500,500,500,500,500,500,500,500,500},
{500,500,500,500,500,500,500,500,500,500,500,500,500,500,500},
{500,500,500,500,500,500,500,500,500,500,500,500,500,500,500}
};
//this is the MAP Sensor Scaling in mbar per mV
float mapSensorScaling[2][2] = {
{50, 1000},
{100.0, 1000.0}
};
float mapSensorFunction = (mapSensorScaling[1][1]-mapSensorScaling[1][0])/(mapSensorScaling[0][1]-mapSensorScaling[0][0]);
//mapSensorFunction declares what pressure is applied at the ingoing voltage.
//this is ignitionTable
float ignitionTable[5][15] = {
{30,30,30,30,30,30,30,25,25,20,20,15,15,10,10},
{30,30,30,30,30,30,30,25,25,20,20,15,15,10,10},
{30,30,30,30,30,30,30,25,25,20,20,15,15,10,10},
{30,30,30,30,30,30,30,25,25,20,20,15,15,10,10},
{30,30,30,30,30,30,30,25,25,20,20,15,15,10,10}
};
//this is fixed Angle Ignition Delay (set by Ignition Stator)
float fixedDegree = 40.0;
//This is the routine that occurs on every ignition Signal
if(ignFlag == 1)
{
//this is for RPM Calculation
int ignSigMillisNow = millis();
int revTime = ignSigMillisNow-ignSigMillisBefore;
ignSigMillisBefore = ignSigMillisNow;
rpm = (1000.0/revTime)*60.0;
//now the MAP Value is read;
int mapValueVoltage = analogRead(mapSensor);
float mapValuePressure = mapValueVoltage*mapSensorFunction;
//now the correct Injector duration is picked!!!!!!!!
//first the parameters are declared in ranges
int rpmRange = rpm/1000;
int mapRange = mapValuePressure/100;
//These are the real ranges to make sure that they dont oversize the fuel table size
int realRpmRange = 0;
int realMapRange = 0;
if(rpmRange <= 14)
realRpmRange = rpmRange;
else realRpmRange = 14;
if(mapRange <= 4)
realMapRange = mapRange;
else realMapRange = 4;
//now the correct duration can be picked
int injectorDurationMs = fuelTable[realMapRange][realRpmRange];
int ignitionDelayDegree = fixedDegree - ignitionTable[realMapRange][realRpmRange];
int ignitionDelayTime = (revTime*1000 / 360)*ignitionDelayDegree;
//TIME: we are at PickUpSignal
delayMicroseconds(ignitionDelayTime);
//TIME: we are at Igniting
digitalWrite(ignition, LOW);
//delayMicroseconds(10);//this is ignition pulse time... please vary..
digitalWrite(ignition, HIGH);
delay((revTime/360)*ignitionTable[realMapRange][realRpmRange]); //waiting till TDC
//TIME: we are at TDC
delayMicroseconds(revTime*1000/2);
//TIME: we are at BTC. Now we can start to inject
//SOI
digitalWrite(injector, HIGH);
Serial.print("Injector open ");
//Serial.println(micros());
delayMicroseconds(injectorDurationMs);
digitalWrite(injector, LOW);
Serial.print("Injector closed ");
//Serial.println(micros());
//EOI
//This is for cycle time calculation.
int microsAfterCycle = micros();
int cycleTime = microsAfterCycle-microsBeforeStartCycle;
Serial.println(cycleTime);
}
Serial.println("Check");
ignFlag = 0;
}
void zuendISR(){
isIgn = 1;
}