TimerOne library gives an interrupt when Timer1.restart() is executed

Dear All,

I am fairly new at Arduino & am learning through online tutorials. I have been trying to use Timers & have come across TimerOne library. It is working well for me except the fact that every time I use Timer1.restart(), the timer restarts but also gives an interrupt.

I want it to simply start counting time from 0 when an input goes high & only execute the interrupt when the timer overflows, not when it restarts. Is there a way this can be done? My basic code is as shown -

#include <TimerOne.h>

int interrupt = 2;
volatile int count = 0;

void setup() {
Serial.begin(9600);
Timer1.initialize(4000000);
Timer1.attachInterrupt(Timer);
pinMode(interrupt, INPUT_PULLUP);
attachInterrupt(0, interrupt0, RISING);

}

void loop() {

}

void interrupt0()
{
  Serial.println("input");
  Timer1.restart();
}

void Timer()
{
  Serial.println("Time up");
  count++;
  Serial.println(count);
}

When I simulated the same on wokwi.com, using the same TimerOne library in the simulator, it did not execute the interrupt when the timer was restarted. It only happens when I am using physical hardware Arduino UNO. Thank you in advance.

This is an example code just for testing the issue.

restart() calls start() and look at the comment in the source code line 222

in practice given how long you want to wait you can probably do that in the loop with millis() or micros()

const byte interruptPin = 2;
volatile unsigned long count;
volatile unsigned long chrono;

void interrupt0() {
  Serial.println("input");
  chrono = micros();
}

void setup() {
  Serial.begin(115200);
  pinMode(interruptPin, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(interruptPin), interrupt0, RISING);
  Serial.println(F("Ready\n"));
}

void loop() {
  // critical section
  noInterrupts();
  unsigned long chronoCopy = chrono;
  interrupts();

  if (micros() - chronoCopy >= 4000000) {
    // critical section
    noInterrupts();
    count++;
    chrono = micros();
    interrupts();
    Serial.print(F("Time up -> "));
    Serial.println(count);
  }
}


PS: it's not a good practice to print inside an ISR

Thank you for the help! Much appreciated.