Code: Select all
// RPM = 60 * (1/timediff in seconds)
RPM = 60*(1000000/(micros() - oldTime )) ; // i.e. RPM = 60 seconds divided by (1000000/(timediff uS))
Also watch out for millis() and micros() wrapping around, there is no check in your code for this..
Actually you could create an array of RPM samples (say 10), and average all those that are within an order of magnitude of each other, add your latest sample in to the sample_number mod 10 position each time you sample, so you overwrite the oldest.. when the millis()/micros() wraps around, the spurious sample should be automatically discarded, and will only last for 10 samples anyway.