#define sensor_pin 17
int set_pulse=1; //set how many pulses in a round here
int max_rpm = 600; //set how many RPM you want to go to here
//Configuration for the Tachometer variables
volatile unsigned long lastPulseTime;
volatile unsigned long interval = 0;
long rpm=0;
void sensorIsr() {
unsigned long now = micros();
interval = now - lastPulseTime;
if (interval > 5000){
rpm = 61000000UL/(interval * set_pulse);
lastPulseTime = now;
}
}
void setup(){ // put your setup code here, to run once
Serial.begin(115200);
delay(500);
pinMode(sensor_pin, INPUT);
attachInterrupt(digitalPinToInterrupt(sensor_pin), &sensorIsr, FALLING);
lastPulseTime = 0;
}
void loop(){
if(rpm >= 0){//Remove the error readings of minus values
//Let's keep this RPM value under control, between 0 and 9999
rpm = constrain (rpm, 0, max_rpm);
//If the engine is not running, print 0
if((micros() - lastPulseTime) < 5e6) {rpm = rpm;}
else{rpm = 0;}
}
Serial.print("RPM:");
Serial.println(rpm);
delay(500);
}