//github.com/dibyasn
//Toggling LEDs using RTOS
#include <Arduino_FreeRTOS.h> // Include FreeRTOS library to use RTOS features like task management
// Pin definitions for LEDs
#define LED1 10 // Pin 10 controls LED1
#define LED2 5 // Pin 5 controls LED2
void setup() {
pinMode(LED1, OUTPUT); // Set pin 10 as an OUTPUT for LED1
pinMode(LED2, OUTPUT); // Set pin 5 as an OUTPUT for LED2
// Create two tasks, one for each LED, to run independently
// TaskLED1: Controls LED1, toggling it every 500ms
xTaskCreate(TaskLED1, "TaskLED1", 128, NULL, 1, NULL);
// TaskLED2: Controls LED2, toggling it every 500ms
xTaskCreate(TaskLED2, "TaskLED2", 128, NULL, 1, NULL);
}
void loop() {
// The loop function remains empty because the tasks are running independently.
// FreeRTOS handles the scheduling of tasks in the background.
}
// Task to toggle LED1
void TaskLED1(void *pvParameters) {
(void) pvParameters; // Avoid compiler warnings for unused parameter
for (;;) { // Infinite loop to keep the task running
digitalWrite(LED1, !digitalRead(LED1)); // Toggle the state of LED1
vTaskDelay(500 / portTICK_PERIOD_MS); // Wait for 500 ms before toggling again
// The vTaskDelay function allows other tasks to run while waiting
}
}
// Task to toggle LED2
void TaskLED2(void *pvParameters) {
(void) pvParameters; // Avoid compiler warnings for unused parameter
for (;;) { // Infinite loop to keep the task running
digitalWrite(LED2, !digitalRead(LED2)); // Toggle the state of LED2
vTaskDelay(500 / portTICK_PERIOD_MS); // Wait for 500 ms before toggling again
// vTaskDelay works similarly to delay() but allows FreeRTOS to schedule other tasks
}
}