The AsyncTask library implements a task that runs after a certain period of microseconds. This allows for programming multiple tasks in a non-blocking manner, achieving a certain “asynchronous” behavior.
In the task constructor, we can specify the time in milliseconds, and the Callback function that will be executed once the time has elapsed. We can also specify the time with the SetIntervalMillis() and SetIntervalMicros() functions.
To update the AsyncTask, it is necessary to frequently call the Update() method, which checks the time elapsed since the start of the task, and executes the relevant actions if the time has expired.
Optionally, the Update function accepts another AsyncTask as a parameter. When the task expires, and after executing the Callback function, it will start the AsyncTask passed as a parameter. This allows for creating chains of tasks.
Additionally, AsyncTasks can be AutoReset, either through the constructor, or by activating the property. This causes the task to restart when it has expired.
Finally, we also have methods to start, stop, reset the task, determine if the task is active or stopped, if the task has expired, the time elapsed since the start, and the remaining time.
Usage Manual
The AsyncTask class can be instantiated through one of its constructors,
AsyncTask(unsigned long millisInterval);
AsyncTask(unsigned long millisInterval, Callback callback);
AsyncTask(unsigned long millisInterval, bool autoReset);
AsyncTask(unsigned long millisInterval, bool autoReset, Callback callback);
AsyncTask Usage
// Start, stop, or reset the AsyncTask
void Start();
void Stop();
void Reset();
// If it is AutoReset, the AsyncTask restarts when expired
bool AutoReset;
// Update the state of the AsyncTask
// This function must be called frequently from the main loop
bool Update();
void Update(AsyncTask &next);
// Set the interval
void SetIntervalMillis(unsigned long interval);
void SetIntervalMicros(unsigned long interval);
// AsyncTask state
unsigned long GetStartTime();
unsigned long GetElapsedTime();
unsigned long GetRemainingTime();
bool IsActive() const;
bool IsExpired() const;
unsigned long Interval;
// Callback function when the task expires
AsyncTaskCallback OnFinish;
Examples
The AsyncTask library includes the following examples to illustrate its usage.
- SimpleTask: Example that shows a simple timed function
#include "AsyncTaskLib.h"
#define DEBUG(a) Serial.print(millis()); Serial.print(": "); Serial.println(a);
AsyncTask asyncTask(2000, true, []() { DEBUG("Expired"); });
void setup()
{
Serial.begin(9600);
Serial.println("Starting");
asyncTask.Start();
}
void loop()
{
asyncTask.Update();
}
- BlinkWithoutDelay: Example that makes the board’s LED blink in a non-blocking manner, with Lambda functions
#include "AsyncTaskLib.h"
AsyncTask asyncTask1(1000, []() { digitalWrite(LED_BUILTIN, HIGH); });
AsyncTask asyncTask2(2000, []() { digitalWrite(LED_BUILTIN, LOW); });
void setup()
{
pinMode(LED_BUILTIN, OUTPUT);
asyncTask1.Start();
}
void loop()
{
asyncTask1.Update(asyncTask2);
asyncTask2.Update(asyncTask1);
}
Installation
- Download the latest version from GitHub
- Unzip the file
- Copy to your libraries folder (usually My Documents\Arduino\libraries)
- Restart the Arduino IDE