Calling Timer1.start(); from ISR mess up its interval ??

I want to start the Timer1 when falling edge is detected on pin 2. I decided to map ISR on pin 2 and from that ISR i call Timer1.start();

It's kind of working but the Timer's interval is messed up. I have set Timer1.initialize(5000); but the pulse I'm getting on LED_BUILTIN is only about 10us long.

I suspect the problem lies in ISR but I don't know the solution to that. I use NANO and the pin 2 interrupt rate is around 100Hz.

#include "TimerOne.h"
#define SYNC_PIN    2
String buffer;                      // Serial buffer

void setup(){
    Serial.begin(115200);
    pinMode(LED_BUILTIN, OUTPUT);
    pinMode(SYNC_PIN, INPUT_PULLUP);
    Timer1.initialize(5000);
    Timer1.attachInterrupt(onTimer);
    Timer1.stop();
    attachInterrupt(digitalPinToInterrupt(SYNC_PIN), isrSync, FALLING);
}

void loop(){
    readSerial();                               // Read serial, nothing else
}

void onTimer(){
    digitalWrite(LED_BUILTIN, LOW);             // Turn off LED
    Timer1.stop();                              // Timer should disable itself 
}

void isrSync(){                                 // Falling edge detected
    digitalWrite(LED_BUILTIN, HIGH);            // Turn on LED
    Timer1.start();                             // Start the timer
}

void readSerial(){                              // Process incoming serial data
    if (Serial.available() > 0){
        char inChar = char(Serial.read());
        if (inChar == '\n'){
            // ENTER pressed: store the value
            unsigned long pulse;                // Pulse time in microseconds
            pulse = buffer.toInt();
            Timer1.setPeriod(pulse);            // Set Timer period
            Serial.println(pulse);              // Print value to serial
            buffer = "";
        } else {
            buffer += String(inChar);
        }
    }
}

I believe that you are running into an issue with the TimerOne library and the start() function. If you look inside the library you will see this note

void start() __attribute__((always_inline)) {
	TCCR1B = 0;
	TCNT1 = 0;		// TODO: does this cause an undesired interrupt?
	resume();
    }

My investigation leads me to believe that setting TCNT1 = 0 in this start() code does indeed trigger an interrupt. I have changed my library code as follows.

 void start() __attribute__((always_inline)) {
	TCCR1B = 0;
	//TCNT1 = 0;		// TODO: does this cause an undesired interrupt? Yes, I think so.
	TCNT1 = 1; //this change works to eliminate unwanted interrupt!
	resume();
    }

I believe that this is because of the Timer being in a PWM mode, but also using an overflow interrupt where the overflow interrupt is triggered at the bottom count.

@cattledog > You’re the man! Setting TCNT1 = 1 in Timer1.h start() has solved this issue.

But I think that modifying the library isn’t very good idea, as the future update will cause the same issue again.

What would be the recommended way?

initialize and setPeriod is in microseconds

But I think that modifying the library isn't very good idea, as the future update will cause the same issue again.

What would be the recommended way?

Don't use the Timer1 library. Set up the timer with direct register configurations. Do not use a pwm mode which has the overflow reset at BOTTOM. Manage the start, stop, and TCNT resest directly.

You will have to spend some time with the processor data sheet and some Timer tutorials. I think the best one on the Arduino Timers is Nick Gammon's here Gammon Forum : Electronics : Microprocessors : Timers and counters