Never mind. Got it sorted. Zeros out after 1 second of no pulses. If the interval is longer then the display will read closer to 0 RPM. Mine is set to zero out when the rpm reaches about 140 or slower.
I suspect numCount; would have an influence on this result.
#include <Arduino.h>
#include <U8g2lib.h>
#ifdef U8X8_HAVE_HW_SPI
#include <SPI.h>
#endif
U8G2_SH1106_128X64_NONAME_F_4W_HW_SPI u8g2(U8G2_R0, /* cs=*/ 10, /* dc=*/ 9, /* reset=*/ 8); /* CLK = 13; MOSI = 11; These 2 aren't defined but need to be connected. */
volatile byte count = 0;
byte numCount = 8; //number of pulse intervals to measure // I have 4 magnets but numCount=4; gives me 1 full rotation + 1 extra count??
// numCount=3; gives me an update every 1 full rotation
volatile unsigned long startTime;
volatile unsigned long endTime;
unsigned long copy_startTime;
unsigned long copy_endTime;
volatile boolean finishCount = false;
float period;
unsigned long rpm = 0;
unsigned long currentMillis;
unsigned long previousMillis;
unsigned long interval = 1000;
void setup(void) {
attachInterrupt(digitalPinToInterrupt(3), isrCount, FALLING);//interrupt on pin3
u8g2.begin();
u8g2.enableUTF8Print(); // enable UTF8 support for the Arduino print() function
u8g2.setFont(u8g2_font_inr16_mf);
delay (1000);
}
void loop(void) {
u8g2.clearBuffer();
u8g2.setCursor(2, 25);
u8g2.print("RPM:");
u8g2.setCursor(60, 25);
u8g2.print(rpm);
u8g2.sendBuffer();
if (finishCount == true)
{
finishCount = false;//reset flag
// disable interrupts, make protected copy of time values
noInterrupts();
copy_startTime = startTime;
copy_endTime = endTime;
count = 0;
interrupts();
period = (copy_endTime - copy_startTime) / 1000.0; //micros to millis
rpm = numCount * 15.0 * (1000.0 / period);
previousMillis = currentMillis;
}
else if (finishCount == false)
{
currentMillis = millis();
if(currentMillis - previousMillis >= interval)
{
rpm = 0;
}
}
}
void isrCount()
{
if (count == 0)//first entry to isr
{
startTime = micros();
}
if (count == numCount)
{
endTime = micros();
finishCount = true;
}
count++; //increment after test for numCount
}