// https://wokwi.com/projects/390472636322088961
#include <Arduino_FreeRTOS.h>
#include <queue.h>
// Function prototypes for the tasks
void TaskBlink(void *pvParameters);
void TaskSerialRead(void *pvParameters);
// Declare a handle for the queue
QueueHandle_t blinkDelayQueue;
// The setup function runs once when you press reset or power the board
void setup()
{
// Initialize serial communication at 115200 bits per second:
Serial.begin(115200);
// Wait for serial port to connect. This is necessary for boards that use native USB
while (!Serial)
{
; // Wait for serial port to connect. Necessary for some boards like Leonardo, Micro, Yun, etc.
}
// Create a queue to hold int values. Queue length is 1, and each item size is the size of int
blinkDelayQueue = xQueueCreate(1, sizeof(int));
// Send an initial value to the queue to start blinking
int initialValue = 500; // Default delay value
xQueueSend(blinkDelayQueue, &initialValue, 0);
if (blinkDelayQueue == NULL)
{
Serial.println("Queue creation failed");
return; // Queue was not created and no memory was available
}
// Create two tasks to run independently.
xTaskCreate(
TaskBlink, // Function that implements the task.
"Blink", // Text name for the task. This is to facilitate debugging.
128, // Stack size in words, not bytes.
NULL, // Parameter passed into the task.
1, // Priority at which the task is created.
NULL); // Used to pass back a handle by which the created task can be referenced.
xTaskCreate(
TaskPotentRead, // Function that implements the task.
"PotentRead", // Text name for the task.
128, // Stack size in words.
NULL, // Parameter passed into the task.
3, // Priority of the task.
NULL); // Task handle.
xTaskCreate(
TaskSerialRead, // Function that implements the task.
"SerialRead", // Text name for the task.
128, // Stack size in words.
NULL, // Parameter passed into the task.
2, // Priority of the task.
NULL); // Task handle.
// The task scheduler, which manages the task execution, is automatically started.
}
void loop()
{
// Empty. All work is done in FreeRTOS tasks.
}
/*--------------------------------------------------*/
/*---------------------- Tasks ---------------------*/
/*--------------------------------------------------*/
// Task to blink an LED
void TaskBlink(void *pvParameters)
{
int delayValue = 500; // Default delay value, now just a fallback
int newDelayValue = 0;
pinMode(LED_BUILTIN, OUTPUT);
for (;;)
{
// Try to receive a new value from the queue, but do not wait indefinitely
if (xQueueReceive(blinkDelayQueue, &newDelayValue, 0) == pdPASS)
{
// A new value was received and delayValue is updated
delayValue = newDelayValue;
Serial.println("new value received");
}
else
{
// No new value, use the last known delayValue
}
digitalWrite(LED_BUILTIN, HIGH);
vTaskDelay(delayValue / portTICK_PERIOD_MS);
digitalWrite(LED_BUILTIN, LOW);
vTaskDelay(delayValue / portTICK_PERIOD_MS);
}
}
// Task to read from the serial port
void TaskPotentRead(void *pvParameters)
{
pinMode(A0, INPUT);
int sensorValue = 500;
int newSensorValue = 500;
for (;;)
{
// Read the analog input
newSensorValue = analogRead(A0);
if (newSensorValue != sensorValue)
{
sensorValue = newSensorValue;
Serial.print("Analog input: ");
Serial.println(sensorValue);
// Send the value to the queue
xQueueSend(blinkDelayQueue, &sensorValue, 0);
}
// Wait for a short time to avoid sending too many values to the queue
vTaskDelay(500 / portTICK_PERIOD_MS);
}
}
// Task to read from the serial port
void TaskSerialRead(void *pvParameters) {
Serial.println("Enter the number of ms delay");
for (;;) {
// Check if data is available to read from the serial port
if (Serial.available() > 0) {
int readValue = Serial.parseInt(); // Parse the incoming integer
// Validate the parsed integer
if (readValue > 0) {
// If valid, send the read value to the queue
if (xQueueSend(blinkDelayQueue, &readValue, portMAX_DELAY) == pdPASS) {
// Confirm the received value via serial output
Serial.print("Received value: ");
Serial.println(readValue);
}
} else {
// If no valid input is detected, notify the user
// Serial.println("No new input detected.");
}
}
// Delay a bit before the next read to avoid overwhelming the serial input
vTaskDelay(500 / portTICK_PERIOD_MS);
}
}