#include <Arduino.h>
// Define LED pins
const int greenLedPin = 7;
const int yellowLedPin = 6;
const int redLedPin = 5;
// Task handles (optional, but good practice for managing tasks)
TaskHandle_t greenLedTaskHandle = NULL;
TaskHandle_t yellowLedTaskHandle = NULL;
TaskHandle_t redLedTaskHandle = NULL;
// --- Task function for Green LED ---
void greenLedBlinkTask(void *parameter) {
const long blinkInterval = 200; // 0.2 seconds
pinMode(greenLedPin, OUTPUT);
for (;;) { // Infinite loop
digitalWrite(greenLedPin, HIGH);
Serial.println("Green LED ON (Task)");
vTaskDelay(pdMS_TO_TICKS(blinkInterval)); // Non-blocking delay
digitalWrite(greenLedPin, LOW);
Serial.println("Green LED OFF (Task)");
vTaskDelay(pdMS_TO_TICKS(blinkInterval)); // Non-blocking delay
}
}
// --- Task function for Yellow LED ---
void yellowLedBlinkTask(void *parameter) {
const long blinkInterval = 500; // 0.5 seconds
pinMode(yellowLedPin, OUTPUT);
for (;;) { // Infinite loop
digitalWrite(yellowLedPin, HIGH);
Serial.println("Yellow LED ON (Task)");
vTaskDelay(pdMS_TO_TICKS(blinkInterval)); // Non-blocking delay
digitalWrite(yellowLedPin, LOW);
Serial.println("Yellow LED OFF (Task)");
vTaskDelay(pdMS_TO_TICKS(blinkInterval)); // Non-blocking delay
}
}
// --- Task function for Red LED ---
void redLedBlinkTask(void *parameter) {
const long blinkInterval = 1000; // 1 second
pinMode(redLedPin, OUTPUT);
for (;;) { // Infinite loop
digitalWrite(redLedPin, HIGH);
Serial.println("Red LED ON (Task)");
vTaskDelay(pdMS_TO_TICKS(blinkInterval)); // Non-blocking delay
digitalWrite(redLedPin, LOW);
Serial.println("Red LED OFF (Task)");
vTaskDelay(pdMS_TO_TICKS(blinkInterval)); // Non-blocking delay
}
}
void setup() {
Serial.begin(115200);
Serial.println("Multitask LED Blink Demo (FreeRTOS)");
// Create tasks
// xTaskCreate(taskFunction, taskName, stackSize, parameters, priority, taskHandle)
xTaskCreate(
greenLedBlinkTask, // Task function
"Green LED Task", // Name of task
1024, // Stack size (bytes)
NULL, // Parameter to pass to the task
1, // Task priority (0 is lowest, configMAX_PRIORITIES-1 is highest)
&greenLedTaskHandle // Task handle
);
xTaskCreate(
yellowLedBlinkTask,
"Yellow LED Task",
1024,
NULL,
1,
&yellowLedTaskHandle
);
xTaskCreate(
redLedBlinkTask,
"Red LED Task",
1024,
NULL,
1,
&redLedTaskHandle
);
// The setup() function will exit, and the FreeRTOS scheduler will take over.
// The loop() function is generally not used when you're primarily
// managing functionality with FreeRTOS tasks.
}
void loop() {
// This loop will effectively do nothing as tasks are running independently
// in the FreeRTOS scheduler. You could put low-priority background
// tasks here if needed, but it's often left empty when using xTaskCreate.
}