Hello, I'm a newbie and this is actually my first Arduino project. I'm trying to make an ignition module for my 300cc bike, single cylinder four stroke. The crankshaft sensor is an inductive sensor. The trigger wheel has 18 teeth but the first tooth (TDC) extends towards the direction of rotation and covers almost half the gap between the last tooth and first tooth. So the gap between the last and first tooth is almost half the normal gap between other teeth. I'm a bit confused on how do I get the angle of rotation using Arduino. This is the oscilloscope reading from the sensor
And here is a code I tried writing to blink an LED when TDC passes and the LED blinks but very poorly. If I put a delay in the code it blinks better but I suppose that's the wrong way to do it.
const byte inputCrank = 3;
const byte ignitionOutput = 12;
void setup() {
pinMode(ignitionOutput, OUTPUT);
pinMode(inputCrank, INPUT_PULLUP);
attachinterrupt(digitalPinToInterrupt(inputCrank), TDC, RISING);
}
void loop(){
digitalWrite(ignitionOutput, LOW);
}
static volatile unsigned long longPulse =0;
static volatile unsigned long lastPulse = 0;
void TDC(){
unsigned long now = micros ();
unsigned lon nowPulse = now - lastPulse;
lastPulse = now;
if((nowPulse >>1) > longPulse) {
digitalWrite(ignitionOutput, HIGH);
//delayMicrosends(200);
// with this delay it blinks better
longPulse = nowPulse;
}
else{
longPulse = nowPulse
}
}
