ArduinoAsyncTasks is a simple and lightweight library for running asynchronous tasks on Arduino. It supports lambda functions and is easy to use. This library is particularly useful for managing multiple tasks or processes that need to run concurrently. Best for multicores boards like ESP32 or Arduino Due.
The library does not support AVR boards, such as Arduino Uno, Nano and Mega, because theses microcontrollers do not fully support the C++ Standard Library due to their limited resources.
- Run tasks asynchronously
- Use lambda functions as tasks
- Lightweight and easy to use
- Allows custom parameters for tasks
If you are using the Arduino board, you will have to install the FreeRTOS library before installing the ArduinoAsyncTasks library.
To install the FreeRTOS library, follow these steps:
- In the Arduino IDE, navigate to Tools > Manage Libraries.
- Search for
FreeRTOSand click Install.
If you are using the ESP32 board, you don't need to install the FreeRTOS library, because it is already included in the board's core.
To install the ArduinoAsyncTasks library, follow these steps:
- Download the latest release as a .ZIP file.
- In the Arduino IDE, navigate to Sketch > Include Library > Add .ZIP Library.
- At the top of your sketch, include the library with
#include <ArduinoAsyncTasks.h>. - You're ready to go!
To use the ArduinoAsyncTasks library, you need to create an instance of the AsyncTask class and pass a lambda function to its constructor. The lambda function will be the task that is run asynchronously.
Here's a basic example:
#include <ArduinoAsyncTasks.h>
void setup() {
// Start the serial monitor
Serial.begin(9600);
}
void loop() {
// Delay for 1 second
delay(1000);
for (int i = 0; i < 10; i++) {
// Create an asynchronous task with a lambda function and integer parameter
AsyncTask<int> task([](int i) {
Serial.println("Task " + String(i) + " is running asynchronously");
});
// Run the task asynchronously with the integer parameter
task(i);
}
}In this example, the AsyncTask runs the lambda function asynchronously, allowing the main loop() function to continue running without waiting for the task to finish.
For more information, see the source code or the examples in the GitHub repository.
The library uses the FreeRTOS, so if you want to play around with TaskParams or learn more about how tasks are managed, you should check out the FreeRTOS documentation.
"Error: Include FreeRTOS.h before including this file, for troubleshooting see the README file on Github."
If you see this error, it means that you need to install the FreeRTOS library (known as Arduino_FreeRTOS). You can do this by navigating to Tools > Manage Libraries and searching for FreeRTOS. Then, click Install.
Then, you need to include the Arduino_FreeRTOS.h file before including the ArduinoAsyncTasks.h file in your sketch.
#include <Arduino_FreeRTOS.h>
#include <ArduinoAsyncTasks.h>
Contributions are welcome! Please feel free to submit a pull request or open an issue on the GitHub repository.
This project is licensed under the MIT License - see the LICENSE