Using the library's tasks feature, there are three things to do first:
First: Reference Library
#include "SCoop.h"
"Library File Download Address"
Http://www.dfrobot.com.cn/community/forum.php?mod=attachment&aid=MTI4MzZ8MWFmMjk1MGF8MTQ1MDcwNTA2M3wwfD
Https://share.weiyun.com/45420032c079bbf55a42f32d6a023ca2
Second: In the Setup function, call the Myscoop.start () command
void Setup () {
Myscoop.start ();
}
Third: In the loop function, the yield () command is called
void Loop () {
Yield ();
}
Then you can use "Tasks". There are two ways to define it, so keep looking down:
1, complete definition: Define "task" of the setup and loop two functions.
Define a task named "Tasktest":
Definetask (tasktest); Define a task with Definetask, named Tasktest
void Tasktest::setup ()//define the Setup () function for this task, which is used to initialize the settings.
{
Here is the contents of the task's setup function
}
void Tasktest::loop ()//Defines the loop () function for this task, which is used to loop through code execution.
{
Here is the contents of the Loop function of the task
}
2. Quick definition: Define only the loop function of the task
If the action performed in this thread does not require the initialization of setup, only the loop loop can be used to quickly define the thread task directly using Definetaskloop (), as follows:
Definetaskloop (TaskTest1)
{
Here is the contents of the Loop function of the task
}
Note that after you use this library, use Sleep (ms) as the delay, not delay ().
Because sleep (MS) is only for the current thread. and delay (MS) is for global.
Finally, a finished demo, 2 LEDs flashing at 100 milliseconds and 1 seconds respectively. Connect the LEDs to the 12 and 13 pins, then run the code to see the results.
#include "SCoop.h"
//Full definition
definetask (tasktest);
void Tasktest::setup ()
{
pinmode (, OUTPUT);
}
void Tasktest::loop ()
{
digitalwrite (+, high);
Sleep (+);
Digitalwrite (, low);
Sleep (+);
}
Fast definition
Definetaskloop (TaskTest1)
{
digitalwrite (n, high);
Sleep (+);
Digitalwrite (n, low);
Sleep (+);
}
void Setup () {
pinmode (OUTPUT);
Myscoop.start ();
}
void Loop ()
{
yield ();
}