FreeRTOS Tasks example sketch Runs FreeRTOS tasks side by side. Needs Tools > Operating System > FreeRTOS (SMP).
setup() starts two tasks: one blinks the LED, the other prints a count. loop() is a task too; every five seconds it prints the list of tasks with vTaskListTasks(). Each task waits with vTaskDelay() (delay() does the same with FreeRTOS), which leaves the cores free for the others. See FreeRTOS.
- Output
- Serial Monitor at 115200 baud: a "countTask on core 0: n" or "countTask on
core 1: n" line every second (the count task may run on either core), and every five seconds one line per task: loop, blink, count, the two idle tasks IDLE0 and IDLE1, and the FreeRTOS timer task Tmr Svc. The columns:
| Column | Meaning |
| Name | Task name |
| State | X running, R ready, B blocked (waiting), S suspended, D deleted |
| Prio | Priority, 0 (idle) to 55 |
| Stack | The least free stack the task has had, in 4-byte words: near 0 means too small |
| Num | Task number, in order of creation |
| Cores | Cores the task may run on: 0x1 core 0, 0x2 core 1, 0xffffffff either |
#include <FreeRTOS.h>
#include <task.h>
void blinkTask(void*) {
for (;;) {
vTaskDelay(pdMS_TO_TICKS(250));
}
}
void countTask(void*) {
unsigned long count = 0;
for (;;) {
Serial.print(
"countTask on core ");
vTaskDelay(pdMS_TO_TICKS(1000));
}
}
xTaskCreate(blinkTask, "blink", 512, nullptr, 1, nullptr);
xTaskCreate(countTask, "count", 1024, nullptr, 1, nullptr);
}
static char list[640];
vTaskListTasks(list, sizeof(list));
Serial.println(
"Name \tState\tPrio\tStack\tNum\tCores");
}
@ OUTPUT
Definition Common.h:40
SerialUSBClass Serial
USB serial port, the one the Serial Monitor uses.
void digitalWrite(pin_size_t pinNumber, PinStatus status)
Sets an output pin HIGH (3.3 V) or LOW (0 V).
PinStatus digitalRead(pin_size_t pinNumber)
Reads the level on a pin.
void pinMode(pin_size_t pinNumber, PinMode pinMode)
Sets a pin as an input or an output.
void loop(void)
Written by the sketch: runs over and over after setup(), on core 0.
void setup(void)
Written by the sketch: runs once after reset, on core 0.
void delay(unsigned long)
Waits a number of milliseconds.
#define LED_BUILTIN
On-board LED.
Definition pins_arduino.h:63