Modifying Nick Gammon's spark plug ISR example

Hello,

Nick Gammon has written an example of an spark plug ISR interrupt about halfway down the page

http://www.gammon.com.au/interrupts

I would like to use this sketch on an Arduino Nano for an 1 cylinder 2-stroke engine.
In the example the timers are used to control the spark timing delay after the trigger signal, and the spark duration.

I am new to Arduino, so please excuse any “dumb” questions.

I tried to do it the “easy” way using micros(), the Timer1 Library and to use a static ignition angle (e.g 17°)

#define coilPin 7
#define triggerPin 2

#include <TimerOne.h>

volatile unsigned long currentMicros = 0;
volatile unsigned long previousMicros = 0;
unsigned long turnMicros = 0; // Time for one revolution
unsigned long sparkDelay = 0; 
int triggerOffset = 90; // Hallsensor mounted 90° before TDC

void setup()
{
  pinMode(coilPin,OUTPUT);  
  pinMode(triggerPin,INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(triggerPin),get_time,FALLING);
  Timer1.initialize(1000000);
}
  
void get_time()
{  
  currentMicros = micros();
}

void loop()
{
      if ((currentMicros - previousMicros) >= 0) //check for rollover
      {
      turnMicros = currentMicros - previousMicros;
      previousMicros = currentMicros;
      }
      sparkDelay = ((turnMicros/360)*(triggerOffset - 17)); // 17° static to keep it simple
      Timer1.attachInterrupt(fire,sparkDelay);
      Timer1.start();
}

void fire()
{
  digitalWrite(coilPin, HIGH);
  // delay time spark duration is missing
  Timer1.stop();
}

Nick’s example seems to me much more flexible, so i’d like to modify it.
For the use in my old vespa, it would be nice if the spark delay depends on current revs/min.
How do I implement the rpm measurement?

Next step would be the ignition angle in dependecy of the revs/min

revs/min ignition angle

<=500 10
1250 17
2000 24
3000 24
4000 24
5000 21
6000 19
7000 17
8000 17
9000 17
10000 17

Put it in an array and store in PROGMEM?
Is it possible to interpolate between the points?

Best Regards,
Dietmar

You have a pulse once per revolution. You have the micros() function to give you the time in microseconds. If you store the time when the pulse occurs and subtract the time of the previous pulse from the time of the current pulse you would have microseconds between pulses, a.k.a. microseconds per revolution. Divide that into microseconds per minute (60 million) to get revolutions per minute. Look that value up in your table. Better yet, look up the two entries on either side of that RPM value and interpolate.