#include <Arduino.h>
// ================= CONFIG =================
const int ledPin1 = 2; // Thread 1 LED
const int ledPin2 = 4; // Thread 2 LED
// ================= THREAD 1 =================
/*
function: threadOneTask
io: void* parameter (unused)
working: blinks ledPin1 every 2 seconds
*/
void threadOneTask(void *parameter) {
try {
pinMode(ledPin1, OUTPUT);
while (true) {
digitalWrite(ledPin1, HIGH);
vTaskDelay(1000 / portTICK_PERIOD_MS);
digitalWrite(ledPin1, LOW);
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
} catch (...) {
Serial.println("error | threadOneTask failed");
vTaskDelete(NULL);
}
}
// ================= THREAD 2 =================
/*
function: threadTwoTask
io: void* parameter (unused)
working: blinks ledPin2 every 6 seconds
*/
void threadTwoTask(void *parameter) {
try {
pinMode(ledPin2, OUTPUT);
while (true) {
digitalWrite(ledPin2, HIGH);
vTaskDelay(3000 / portTICK_PERIOD_MS);
digitalWrite(ledPin2, LOW);
vTaskDelay(3000 / portTICK_PERIOD_MS);
}
} catch (...) {
Serial.println("error | threadTwoTask failed");
vTaskDelete(NULL);
}
}
// ================= SETUP =================
void setup() {
Serial.begin(115200);
try {
xTaskCreate(
threadOneTask,
"threadOne",
1000,
NULL,
1,
NULL
);
xTaskCreate(
threadTwoTask,
"threadTwo",
1000,
NULL,
1,
NULL
);
} catch (...) {
Serial.println("error | task creation failed");
}
}
// ================= LOOP =================
void loop() {
// empty (FreeRTOS handles tasks)
}