I'd try it a little differently?
#include <timeObj.h>
#include <mapper.h>
#include <runningAvg.h>
#define INT_PIN D2
#define READING_MS 500 // How long to let the counts add up.
#define MAX_COUNTS 1000 // <<- CHANGE THIS The max number of counts we should ever see during READING_MS
#define MAX_RPM 1000 // <<- CHANGE THIS Given this max amunt of counts, this is the max RPM that vallue gives.
timeObj readingTimer(READING_MS); // Set up a timer for grabbing the result.
mapper RPMmapper(0,MAX_COUNTS,0,MAX_RPM); // Set up a float mapper to do the math.
runningAvg smoother(10); // We'll avarage over 10 readings.
volatile long count;
long savedCount;
float RPM;
void isr() { count++; } //interrupt service routine
void setup(void) {
Serial.begin(115200);
count = 0;
pinMode(INT_PIN, INPUT);
attachInterrupt(digitalPinToInterrupt(INT_PIN),isr,FALLING); //attaching the interrupt
}
void loop (void) {
if (readingTimer.ding()) { // If the count timer has expired..
savedCount = count; // FIRST thing we do is save the counts. (Could change)
count = 0; // NEXT is to clear the original counter for the next set.
readingTimer.stepTime(); // Set up the timer for the next reading..
RPM = smoother.addData(RPMmapper.map(savedCount)); // WHEWH, now we have some time to play with. DO the math.
Serial.print("rpm: "); // Show the results to the user.
Serial.println(RPM);
}
}
I don't have hardware to test this, But at least, if it doesn't work out of the box you can see the approach. Maybe it'll help?
Before compiling you will need to calcualte a couple things..
I set it up for a reading refresh every 1/2 second..
What is the MAX number of counts you should ever see during that amount of time?
AND
What is the maximum RPM that this max number of counts would result it?
Plug in those two numbers, grab LC_baseTools from the IDE library manager and you should be good to go.
Fingers crossed!
Good luck!
-jim lee