I'm trying to change the period for TimerOne, but the interval after start() is half the specified period.
In the following code, I ask for a period of 1000 microseconds, but get pulses every 500 microseconds. The problem goes away if I remove the stop/setPeriod/start from the handler, and exists if I just have the start.
Expected behavior: the start() call in timerInterrupt should start the interval at the beginning.
Do I need to do something different to start the timer from the beginning of the interval?
(My ultimate goal is to read serial data from an input, using timer interrupts to sample the data in the middle of a pulse, and restarting the timing on a signal edge. But the time intervals are half of what I specify. The code above is simplified to show the problem, so it's not actually useful.)
In the following code, I ask for a period of 1000 microseconds, but get pulses every 500 microseconds. The problem goes away if I remove the stop/setPeriod/start from the handler, and exists if I just have the start.
Expected behavior: the start() call in timerInterrupt should start the interval at the beginning.
Do I need to do something different to start the timer from the beginning of the interval?
Code:
#include <TimerOne.h>
#define PIN_DEBUG 6
void timerInterrupt() {
Timer1.stop();
Timer1.setPeriod(1000);
Timer1.start();
digitalWrite(PIN_DEBUG, HIGH);
digitalWrite(PIN_DEBUG, LOW);
}
void setup() {
pinMode(PIN_DEBUG, OUTPUT);
Timer1.initialize(1000);
Timer1.attachInterrupt(timerInterrupt);
Timer1.start();
}
void loop() {
}
(My ultimate goal is to read serial data from an input, using timer interrupts to sample the data in the middle of a pulse, and restarting the timing on a signal edge. But the time intervals are half of what I specify. The code above is simplified to show the problem, so it's not actually useful.)