#include <Arduino.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h> // Required for task management
// Define LED pins according to your Wokwi circuit
const int RED_LED_PIN = 7; // High Priority
const int GREEN_LED_PIN = 6; // Medium Priority
const int YELLOW_LED_PIN = 5; // Low Priority
// Define blink intervals for each LED (in milliseconds)
// Each LED will be ON for half the interval and OFF for half the interval.
const TickType_t RED_BLINK_INTERVAL_MS = 100; // High priority: 100ms cycle
const TickType_t GREEN_BLINK_INTERVAL_MS = 500; // Medium priority: 500ms cycle
const TickType_t YELLOW_BLINK_INTERVAL_MS = 1000; // Low priority: 1000ms cycle
// --- Task Functions for Each LED ---
// Task for the High Priority (Red) LED
void redLedTask(void *pvParameters) {
// Initialize the pin inside the task, or in setup() if it's shared config
pinMode(RED_LED_PIN, OUTPUT);
Serial.println("Red LED Task started (High Priority)");
for (;;) { // Infinite loop for the task
digitalWrite(RED_LED_PIN, HIGH);
vTaskDelay(pdMS_TO_TICKS(RED_BLINK_INTERVAL_MS / 2)); // ON for half the interval
digitalWrite(RED_LED_PIN, LOW);
vTaskDelay(pdMS_TO_TICKS(RED_BLINK_INTERVAL_MS / 2)); // OFF for half the interval
}
}
// Task for the Medium Priority (Green) LED
void greenLedTask(void *pvParameters) {
pinMode(GREEN_LED_PIN, OUTPUT);
Serial.println("Green LED Task started (Medium Priority)");
for (;;) { // Infinite loop for the task
digitalWrite(GREEN_LED_PIN, HIGH);
vTaskDelay(pdMS_TO_TICKS(GREEN_BLINK_INTERVAL_MS / 2));
digitalWrite(GREEN_LED_PIN, LOW);
vTaskDelay(pdMS_TO_TICKS(GREEN_BLINK_INTERVAL_MS / 2));
}
}
// Task for the Low Priority (Yellow) LED
void yellowLedTask(void *pvParameters) {
pinMode(YELLOW_LED_PIN, OUTPUT);
Serial.println("Yellow LED Task started (Low Priority)");
for (;;) { // Infinite loop for the task
digitalWrite(YELLOW_LED_PIN, HIGH);
vTaskDelay(pdMS_TO_TICKS(YELLOW_BLINK_INTERVAL_MS / 2));
digitalWrite(YELLOW_LED_PIN, LOW);
vTaskDelay(pdMS_TO_TICKS(YELLOW_BLINK_INTERVAL_MS / 2));
}
}
// --- Arduino Setup Function ---
void setup() {
Serial.begin(115200); // Start serial communication for debugging
Serial.println("Starting FreeRTOS LED Blinker on ESP32-C3...");
// Create FreeRTOS tasks
// xTaskCreate( TaskFunction, TaskName, StackSize, Parameters, Priority, TaskHandle );
// High Priority Task: Higher priority value means higher priority.
// Standard ESP32 Arduino uses priorities 0-24, with 0 being lowest.
// We'll use 3 for High, 2 for Medium, 1 for Low for clear distinction.
xTaskCreate(
redLedTask, // Task function
"Red LED Task", // Name of the task (for debugging)
1024, // Stack size in words (not bytes) for the task.
// 1024 is usually sufficient for simple tasks.
NULL, // Parameter to pass to the task (none in this case)
3, // Priority of the task (High Priority)
NULL // Task handle (not used here)
);
// Medium Priority Task
xTaskCreate(
greenLedTask, // Task function
"Green LED Task", // Name of the task
1024, // Stack size
NULL, // Parameters
2, // Priority (Medium Priority)
NULL
);
// Low Priority Task
xTaskCreate(
yellowLedTask, // Task function
"Yellow LED Task", // Name of the task
1024, // Stack size
NULL, // Parameters
1, // Priority (Low Priority)
NULL
);
// The loop() function is typically left empty when using FreeRTOS tasks.
// The tasks run independently once created.
Serial.println("All tasks created. The scheduler will now manage execution.");
} // <--- MISSING CLOSING BRACE WAS HERE!
// --- Arduino Loop Function ---
void loop() {
// This loop runs on the main Arduino core (core 1 by default on ESP32-C3).
// In a FreeRTOS application, you generally avoid putting active code here
// unless it's a specific task that you want to run on this core.
// The tasks created in setup() are handled by the FreeRTOS scheduler.
// You can add a small delay here if you want to prevent it from consuming
// too much CPU if no other tasks are ready, but it's usually not necessary.
vTaskDelay(1); // Smallest possible delay to yield if no other tasks are ready
}