Arduino RPM Detection

367 views Asked by At

Hello can someone explain me this code above. The arduino gets an rectangular signal (the rectangular signal comes from the spark plug from the engine) from an car engine which is attached to the "ignitionPin", and then it calculates the revs per minute, but i dont understand how it does that. please can someone explain me how the code with the interrupt works. thanks

const int ignitionPin = 2;
const int ignitionInterrupt = 0;
const unsigned int pulsesPerRev = 1;

unsigned long lastPulseTime = 0;
unsigned long rpm = 0;
int rpm_int;
int rpm_to_disp;

void setup() {  
  pinMode(ignitionPin, INPUT);
  attachInterrupt(ignitionInterrupt, &ignitionIsr, RISING);
}

void ignitionIsr()
{
  unsigned long now = micros();
  unsigned long interval = now - lastPulseTime;
  if (interval > 5000)
  {
     rpm = 60000000UL/(interval * pulsesPerRev);
     lastPulseTime = now;
  }   
}
0

There are 0 answers