IntervalTimer
IntervalTimer can be used to call a function via an interrupt at a precisely timed interval. The Quarto supports up to 4 IntervalTimers being active at the same time.
Instantiation
IntervalTimers are designed to be instantiated as global objects.
IntervalTimer someTimer;
IntervalTimers Functions
IntervalTimers have the following functions (methods):
begin
bool begin(void (*function)(), int microseconds);
bool begin(void (*function)(), unsigned int microseconds);
bool begin(void (*function)(), float microseconds);
Begin calling the function at the interval specified by the second argument in microseconds. The first argument is a void function (and function taking no arguments and returning nothing) that gets called at the timing interval. The second argument can be an signed or unsigned int, or a float and sets the timing interval in microseconds.
The timing interval is generated from a 66 MHz clock. Thus it is possible to generate an interval running every 4.5 µs (297 / 66e6 = 4.5 µs). However, an interval of 4.75 µs is not possible, as the options are either 4.7424 µs (313/66e6) or 4.7575 µs (314/66e6).
The function returns true if the timer is successfully activated and false otherwise. The function may return false if the microseconds argument is invalid (a negative number for example) or if there are already 4 active IntervalTimers and another one cannot be activated.
Example
IntervalTimer timer;
triggerMode(1,OUTPUT); // set Trigger #1 as output
void runOnInterval(void) {
triggerToggle(1); // toggle Trigger #1
}
bool res = timer.begin(runOnInterval, 4.5); // Run runOnInterval function every 4.5 µs.
if (res) {
Serial.println("Timer is running.");
}
update
void update(int microseconds);
void update(unsigned int microseconds);
void update(float microseconds);
Change the timing interval for a running timer. For limitations on the interval values, see begin.
Example
IntervalTimer timer;
triggerMode(1,OUTPUT); // set Trigger #1 as output
void runOnInterval(void) {
static uint8_t counts = 0;
triggerToggle(1); // toggle Trigger #1
counts++;
if (counts == 10){
timer.update(9); // change timing rate to every 9 µs.
}
if (counts == 20) {
timer.update(4.5); // change timing rate to every 4.5 µs
counts = 0;
}
}
timer.begin(runOnInterval, 4.5); // Run runOnInterval function every 4.5 µs.
end
void end(void);
Stops running the interval timer. Once stopped, the timer is no longer active and does not count towards the maximum of 4 timers that can be active at a time.