-
Notifications
You must be signed in to change notification settings - Fork 5
Hooking into yield
Sometimes your have code that needs to be called by the
user as often as possible. Prominent examples are AccelStepper
where you are required to call run()
at high speed. Using the debounce library, you need to call update()
or the PID library which requires a frequent call to Compute()
.
Usually, the user of these libraries just calls these functions in loop which works fine for simple code. As soon as there is longer running code or some delays in loop the call rate of these functions can get unacceptably low and the libraries don't work as expected.
Teensyduino provides a yield()
function which is called before each call to loop()
. delay()
and probably other long running core functions also call loop while they spin.
Yield
is defined as weak function and can be overridden. Here an example how to do this.
void yield() // override the built in yield function
{
digitalToggleFast(0);
}
void setup()
{
pinMomde(0,OUTPUT);
pinMode(LED_BUILTIN, OUTPUT);
}
void loop()
{
digitalToggleFast(LED_BUILTIN);
delay(250);
}
A much better approach would be to have those functions in the background. To achieve this you can replace the stoc
Teensy is a PJRC trademark. Notes here are for reference and will typically refer to the ARM variants unless noted.