#include <Arduino_FreeRTOS.h>
#include <queue.h>
// Define task handles
TaskHandle_t TaskC_Handle;
TaskHandle_t TaskD_Handle;
// Define queues
QueueHandle_t Queue3;
QueueHandle_t Queue4;
// Define LED pin
const int ledPin = 13;
// Define struct to encapsulate different blink information
struct DifferentBlinkInfo {
String message;
int blinkCount;
int delayMultiplier; // Additional parameter for different behavior
};
void setup() {
Serial.begin(9600);
// Create queues
Queue3 = xQueueCreate(5, sizeof(int));
Queue4 = xQueueCreate(5, sizeof(DifferentBlinkInfo));
// Create tasks
xTaskCreate(TaskC, "TaskC", 100, NULL, 2, &TaskC_Handle);
xTaskCreate(TaskD, "TaskD", 100, NULL, 1, &TaskD_Handle);
// Start the scheduler
vTaskStartScheduler();
}
void loop() {
// Code should not enter here
}
void TaskC(void *pvParameters) {
(void)pvParameters;
while (1) {
// Read Serial input from the user
if (Serial.available() > 0) {
String input = Serial.readStringUntil('\n');
// Echo back the input
Serial.println("You entered: " + input);
// Check if input contains "customDelay" followed by a space and a number
if (input.startsWith("customDelay ")) {
int customDelayValue = input.substring(12).toInt();
// Send the custom delay value to Queue3
xQueueSend(Queue3, &customDelayValue, portMAX_DELAY);
}
}
// Task delay
vTaskDelay(100 / portTICK_PERIOD_MS);
}
}
void TaskD(void *pvParameters) {
(void)pvParameters;
int customDelayValue = 1500; // Default custom delay value
while (1) {
// Check for new custom delay value in Queue3
if (xQueueReceive(Queue3, &customDelayValue, 0) == pdPASS) {
// Update LED blink rate
vTaskDelay(100 / portTICK_PERIOD_MS); // Ensure TaskC has enough time to read Serial input
}
// Different LED blink pattern using custom delay multiplier
digitalWrite(ledPin, HIGH);
vTaskDelay(customDelayValue / 3 / portTICK_PERIOD_MS);
digitalWrite(ledPin, LOW);
vTaskDelay(customDelayValue / 3 / portTICK_PERIOD_MS);
digitalWrite(ledPin, HIGH);
vTaskDelay(customDelayValue / 3 / portTICK_PERIOD_MS);
digitalWrite(ledPin, LOW);
// Increment blink count
static int differentBlinkCount = 0;
differentBlinkCount++;
// Every 50 blinks, send "Different Blinked" message to Queue4
if (differentBlinkCount % 50 == 0) {
DifferentBlinkInfo differentBlinkInfo;
differentBlinkInfo.message = "Different Blinked";
differentBlinkInfo.blinkCount = differentBlinkCount;
differentBlinkInfo.delayMultiplier = 2; // Modify the multiplier for different behavior
// Send struct to Queue4
xQueueSend(Queue4, &differentBlinkInfo, portMAX_DELAY);
}
}
}