//ESP32 and Arduino IDE C langauge
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#define LED_PIN 2
#define BAUD_RATE 115200
TaskHandle_t uartTaskHandle = NULL;
TaskHandle_t ledTaskHandle = NULL;
volatile char receivedChar = 0;
volatile bool commandReceived = false;
void setup()
{
Serial.begin(BAUD_RATE);
pinMode(LED_PIN, OUTPUT);
xTaskCreate(
uartTask,
"UART Task",
1000,
NULL,
1,
&uartTaskHandle
);
xTaskCreate(
ledTask,
"LED Task",
1000,
NULL,
1,
&ledTaskHandle
);
Serial.println("Setup completed.");
}
void loop()
{
}
void uartTask(void * parameter)
{
while (1)
{
if (Serial.available())
{
receivedChar = Serial.read();
Serial.print("UART Task: Received character: ");
Serial.println(receivedChar);
commandReceived = true;
}
vTaskDelay(pdMS_TO_TICKS(10));
}
}
void ledTask(void * parameter)
{
while (1)
{
if (commandReceived)
{
if (receivedChar == '1')
{
digitalWrite(LED_PIN, HIGH);
vTaskDelay(pdMS_TO_TICKS(500)); // LED on for 500 ms
digitalWrite(LED_PIN, LOW);
vTaskDelay(pdMS_TO_TICKS(500)); // LED off for 500 ms
}
else if (receivedChar == '2')
{
digitalWrite(LED_PIN, HIGH);
vTaskDelay(pdMS_TO_TICKS(100)); // LED on for 100 ms
digitalWrite(LED_PIN, LOW);
vTaskDelay(pdMS_TO_TICKS(100)); // LED off for 100 ms
}
}
else
{
digitalWrite(LED_PIN, LOW); // Turn off LED if no command received
}
vTaskDelay(pdMS_TO_TICKS(10));
}
}