#include <Arduino.h> // Standard Arduino header
#include <freertos/FreeRTOS.h> // Required for FreeRTOS definitions
#include <freertos/task.h> // Required for xTaskCreate and vTaskDelay
// Define task functions (these are like separate "programs" running concurrently)
// Task 1: Prints a message every 1000 milliseconds (1 second)
void task1_function(void *pvParameters) {
for (;;) { // An RTOS task typically runs in an infinite loop
Serial.println("Task 1 is running: Hello from Task 1!");
// vTaskDelay pauses the task for a specified number of RTOS ticks.
// pdMS_TO_TICKS(ms) converts milliseconds to ticks.
vTaskDelay(pdMS_TO_TICKS(1000));
}
}
// Task 2: Prints a message every 500 milliseconds (0.5 seconds)
void task2_function(void *pvParameters) {
for (;;) { // An RTOS task typically runs in an infinite loop
Serial.println(" Task 2 is running: Greetings from Task 2!"); // Indent for easier distinction
vTaskDelay(pdMS_TO_TICKS(500));
}
}
// The standard Arduino setup() function, used to initialize and create tasks
void setup() {
Serial.begin(115200); // Initialize serial communication at 115200 baud
Serial.println("--- Starting RTOS Multitasking Demo ---");
// Create Task 1:
// xTaskCreate(TaskFunction, TaskName, StackSize, Parameters, Priority, TaskHandle)
xTaskCreate(
task1_function, // Pointer to the task function
"Task1Name", // A descriptive name for the task (useful for debugging)
2048, // Stack size allocated for the task (in bytes for ESP32)
NULL, // Parameters to pass to the task (NULL if none)
1, // Task priority (higher number = higher priority). Here, both have same.
NULL // Handle to the created task (NULL if not needed)
);
// Create Task 2:
xTaskCreate(
task2_function,
"Task2Name",
2048,
NULL,
1,
NULL
);
// In RTOS applications, the Arduino loop() function is typically left empty.
// All ongoing operations are handled by the FreeRTOS scheduler managing the tasks.
}
// The standard Arduino loop() function - not used in this RTOS example.
void loop() {
// This loop will effectively do nothing as tasks handle execution.
}