Hardware Reference
In-Depth Information
{
Serial.println("loop2()");
delay(1000);
}
This sketch is simple; it will import the Scheduler library and run two func-
tions: loop1() and loop2() . Remember, loop() is always called. The two addi-
tional loop functions will simply print a line of text to the serial port and then
wait for a second.
Remember when I said that delay() was blocking? With the Scheduler library,
it isn't; it allows functions to sleep for a set time but gives control back to other
functions. In this case, one loop is called, and when it reaches delay() , it gives
control to the other loop function. When that one reaches delay() , it will once
again return control to the i rst function, and this will happen until delay()
ends, after 1 second.
The output of the function on the serial port is a list, alternating between
"loop1()" and "loop2()" .
Scheduled functions can also use global variables. Change the sketch to add
the following:
#include <Scheduler.h>
int i;
void setup()
{
Serial.begin(9600);
// Add "loop1" and "loop2" to scheduling.
Scheduler.startLoop(loop1);
Scheduler.startLoop(loop2);
i = 0;
}
void loop()
{
delay(1000);
}
void loop1()
{
i++;
Serial.print("loop1(): ");
Serial.println(i, DEC);
delay(1000);
}
Search WWH ::




Custom Search