This variation does measure the time it takes to do one rotation and measures the RPM from the very last rotation. It is definitely a different approach
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2); //set the LCD address to 0x27 for a 16 chars and 2 line display
//TESTPIN
const int testpin = 3;
//MAIN INPUTS
const int DispSampleTime = 1000; //[millisec] //= 0.1sec
// variables:
volatile unsigned long lastTime;
volatile unsigned long thisTime;
volatile unsigned long duration;
unsigned long disp_timeold = 0UL;
unsigned long timetemp = 0L;
//SETUP RUN ONLY ONCE @ start
void setup()
{
//DEBUG
TCCR2B = TCCR2B & 0b11111000 | 0x07 ; // sets PWM to 30.517578125 Hz on pin 3 and 11 //timer 2
//testpin TESTESTESTEST----------------------<<<<<<<<<<<<>>>>>>>>>>>>>
pinMode(testpin, OUTPUT);
analogWrite(testpin, 1); //sample 30.51757 hz pulse 1/255 uptime.
lcd.init(); // initialize the lcd
lcd.backlight();
//set interrupt on pin 2 and call function rpm_interrupt when pin is falling
attachInterrupt(0, rpm_interrupt, FALLING);
}
void loop()
{
// DISPLAY RPM
timetemp = millis() - disp_timeold;
if( timetemp >= DispSampleTime)
{
disp_timeold += timetemp;
float RPM = 60e6/duration; // 60.000.000 micros / duration in micros
//disp stuff
lcd.clear();
lcd.print("Rpm: ");
lcd.print(RPM);
}
}
void rpm_interrupt()
{
lastTime = thisTime;
thisTime = micros();
duration = thisTime-lastTime;
}