# Timer Entity¶

The Timer entity provides simple scheduling for events in the reflex system. BrainStem modules generally contain between 4 and 8 timers depending on the module. The most common usage is to write a timer reflex and load and enable it on the BrainStem module, then an expiration can be set for the timer, and this reflex code will be executed when the timer expires.

Timers have two modes, single which executes just once and repeat which executes until the expiration is set to zero or the mode is changed to single.

## Expiration (Get/Set)¶

timer [ index ] . getExpiration <= (unsigned int) microseconds
timer [ index ] . setExpiration => (unsigned int) microseconds


Gets or sets the next expiration for this timer in microseconds. If zero, the timer is not currently set to expire in the future.

## Mode (Get/Set)¶

timer [ index ] . getMode <= (unsigned char) mode
timer [ index ] . setMode => (unsigned char) mode


Gets or sets the current timer mode. 1 for repeat mode and 0 for single mode.

When in repeat mode an expiration will occur every n microseconds when n it the expiration setting of the timer. To stop a repeat timer, set its expiration to 0.

When in single mode (The default) setting a non-zero expiration will cause the timer to trigger a single time after the expiration setting in microseconds. If a timer is set, resetting its expiration to zero will clear the timer, and no reflex code will be triggered.

Reflex example

The following reflex code would need to be compiled with arc, loaded onto the BrainStem module and enabled to be executed. See the Reflex Language reference for more information about working with reflex files.

reflex timer[0].expiration(void) {
stem.system.setLED(on);
}


### Code Examples¶

#### C++¶

// All commands return aErr values when errors are encountered and aErrNone on
// success. Get commands fill the variable with the returned value.

stem.timer[0].getExpiration(uSecs);
stem.timer[0].setExpiration(1000000); // Sets the timer for 1 second in the future.
stem.timer[0].getMode(mode);
stem.timer[0].setMode(timerModeRepeat) // timerModeRepeat is a convenience define.


#### Reflex¶

// Get commands fill the variable with the returned value.

stem.timer[0].getExpiration(uSecs);
stem.timer[0].setExpiration(1000000); // Sets the timer for 1 second in the future.
stem.timer[0].getMode(mode);
stem.timer[0].setMode(timerModeRepeat) // timerModeRepeat is a convenience define.


#### Python¶

uSecs = stem.timer[3].getExpiration()
stem.timer[3].setExpiration(1000000) # Sets the timer for 1 second in the future.
mode = stem.timer[3].getMode()
stem.timer[3].setMode(timerModeRepeat) // timerModeRepeat is a convenience define.