Interrupt counter resetting to 0

Hi all, I made a thread about an interrupt counter yesterday and got some good advice. I'm making an RPM counter that reads a square wave from 0-5v. This code manages to count up the amount of rising edges using an interrupt to increment whenever the input goes up to 5v, however I'm not sure how to reset the count back to zero without causing the output to just be zero.

const byte interrupt_Pin = 2; //Sets the pin used for the interrupt
volatile int count; //Should increment when interrupt pin changes state
int last_time_millis_was_reset;
int RPM;

void setup() {
  pinMode(interrupt_Pin, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(interrupt_Pin), counter, RISING);

void counter() {

void loop() {
  if ((millis() - last_time_millis_was_reset) >= 1000);
    RPM = count * 60;
    last_time_millis_was_reset = millis();

Really not sure how to reset the count number every second without messing my count up all together

Silly! Print the RPM once a second, not every time through the loop!


Well my main problem is that the if statement causes my count to reset very fast, which shouldn’t be happening as far as I can tell. My serial monitor is just a spam of 0s with a 60 every now and then, which leads me to believe it’s the count getting reset over and over. I do think you might be on to something with the print every second though, so I moved it into my if statement

int last_time_millis_was_reset;
should be
unsigned long last_time_millis_was_reset;

Print inside the if() statement, just after the RPM calculation.

If 'count' is NEVER going to be higher than 255 (15,300 RPM), use an 'unsigned char' (a.k.a. 'byte') instead of 'int'.

If 'count' is EVER going to be higher than 255, use an 'unsigned int' and copy the values with interrupts disabled:

void loop() {
  unsigned localCount;
  localCount = count;  // Grab the volatile value while interrupts are off.
  if ((millis() - last_time_millis_was_reset) >= 1000);
    RPM = localCount * 60;

The problem this avoids is having the count roll from 255 (0x00FF) to 256 (0x0100) between the moment the code fetches the two bytes of the int. Depending on which byte is fetched first, you could read 0 (0x0000) or 511 (0x01FF). The sudden jump from 15,300 RPM to 0 RPM or 30,660 RPM could cause problems for whatever relies on the RPM signal.

  if ((millis() - last_time_millis_was_reset) >= 1000);

That semicolon shouldn't be there.