User Tools

Site Tools


en-us_commands:reference:itimerset

ItimerSet()

ItimerSet() sets the interval and callback function, or the hardware timer to use.

ItimerSet command operation flow

bool ItimerSet(unsigned long intervalMs, void (*callback)())
bool ItimerSet(uint8_t timerNumber, unsigned long intervalMs, void (*callback)())

Commands

Command Arguments Return Value Operation
ItimerSet(intervalMs, callback) unsigned long intervalMs — Callback interval in ms. 0 is not allowed
void (*callback)() — Function in void functionName() form, called at each interval
bool Sets the interval and callback function, or the hardware timer to use.

Example

This uses the default callback timer (Timer2). The callback changes only a flag; serial output runs in loop().

volatile bool tick = false;
 
void onTimer() {
  tick = true;
}
 
void setup() {
  Serial.begin(115200);
  if (!ItimerSet(1000, onTimer)) return;  // Configure a 1 s callback on the default hardware timer.
  ItimerStart();  // Start the configured callback timer.
}
 
void loop() {
  if (tick) {
    noInterrupts();
    tick = false;
    interrupts();
    Serial.println(F("tick"));
  }
}

Precautions

  • The callback runs in an interrupt. Change only a flag there; do not call delay(), Serial, I2C or SPI in it.
  • The callback timer without a timer number uses Timer2. Do not use it together with tone() or PWM output that uses Timer2.
  • When specifying another hardware timer, check for conflicts with PWM, analog output and high-speed counter functions using it.
  • A zero interval or missing callback makes configuration fail and return false.

Built-in Commands in the Same Category

en-us_commands/reference/itimerset.txt · Last modified: by 127.0.0.1

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki