Skip to content
luni64 edited this page Jun 28, 2020 · 8 revisions

Why

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.

Yield

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.

Overriding Yield

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

Clone this wiki locally