Hi I found this sketch for my project making an electric gear motor RPM tachometer, rpm in between 0-150 only. My issue is for this sketch to be pretty accurate I have to take readings at 1000ms intervals but I need my reading to be accurate at maybe 100ms?
Is there any way to make this sketch more accurate at 100ms? Maybe somehow save last 5 readings and take an average of the last 5?
volatile int interruptCounter; //counter use to detect hall sensor in fan
int RPM; //variable used to store computed value of fan RPM
unsigned long previousmills;
//NODEMCU ESP8266
#define tachInputPIN D5
#define pwmOutputPin D6
#define pwmDuty 1024
#define calculationPeriod 1000 //Number of milliseconds over which to interrupts
void ICACHE_RAM_ATTR handleInterrupt() { //This is the function called by the interrupt
interruptCounter++;
}
void setup()
{
Serial.begin(115200);
previousmills = 0;
interruptCounter = 0;
RPM = 0;
//NODEMCU esp8266
pinMode(pwmOutputPin, OUTPUT);
pinMode(tachInputPIN, INPUT_PULLUP);
analogWriteFreq(25000);
analogWrite(pwmOutputPin, pwmDuty);
attachInterrupt(digitalPinToInterrupt(tachInputPIN), handleInterrupt, FALLING);
}
void loop()
{
// NODEMCU esp8266
// analogWrite(pwmOutputPin, pwmDuty);
if ((millis() - previousmills) > calculationPeriod) { // Process counters once every second
previousmills = millis();
int count = interruptCounter;
interruptCounter = 0;
computeFanSpeed(count);
displayFanSpeed();
}
yield();
}
void computeFanSpeed(int count) {
//interruptCounter counts 1 pulses per revolution of the fan over a one second period
RPM = count / 1 ;
}
void displayFanSpeed() {
Serial.print(RPM); //Prints the computed fan speed to the serial monitor
Serial.print(" RPM\r\n"); //Prints " RPM" and a new line to the serial monitor
}