#include <Arduino_FreeRTOS.h> /
#include <queue.h>
QueueHandle_t xQueue; // Declara una variable global para la cola
const int ledPin1 = 4;
const int ledPin2 = 7;
void Task1(void *params); // Prototipo de función para la tarea 1
void Task2(void *params); // Prototipo de función para la tarea 2
void setup() {
Serial.begin(9600); // Inicia la comunicación serial
xQueue = xQueueCreate(10, sizeof(int)); // Crea una cola con capacidad para 10 elementos enteros
xTaskCreate(Task1, "Task1", configMINIMAL_STACK_SIZE, NULL, 1, NULL); // Crea la tarea 1
xTaskCreate(Task2, "Task2", configMINIMAL_STACK_SIZE, NULL, 1, NULL); // Crea la tarea 2
pinMode(ledPin1, OUTPUT); // Configura el pin del primer LED como salida
pinMode(ledPin2, OUTPUT); // Configura el pin del segundo LED como salida
}
void loop() {
// Nada que hacer aquí ya que todo el trabajo se realiza en las tareas
}
void Task1(void *params) {
while (1) {
int dataToSend = random(2); // Genera un número aleatorio (0 o 1)
xQueueSend(xQueue, &dataToSend, portMAX_DELAY); // Envía el número generado a la cola
vTaskDelay(pdMS_TO_TICKS(1000)); // Espera 1000 milisegundos antes de generar otro número
}
}
void Task2(void *params) {
int previousState = -1; // Estado inicial del LED
while (1) {
int receivedData;
if (xQueueReceive(xQueue, &receivedData, portMAX_DELAY) == pdPASS) { // Espera a recibir datos de la cola
if (receivedData == 0) {
digitalWrite(ledPin1, HIGH); // Enciende el primer LED
digitalWrite(ledPin2, LOW); // Apaga el segundo LED
if (previousState != 0) {
Serial.println("Estado del LED cambiado: LED 1 ENCENDIDO, LED 2 APAGADO"); // Imprime un mensaje si el estado ha cambiado
previousState = 0;
}
} else {
digitalWrite(ledPin1, LOW); // Apaga el primer LED
digitalWrite(ledPin2, HIGH); // Enciende el segundo LED
if (previousState != 1) {
Serial.println("Estado del LED cambiado: LED 1 APAGADO, LED 2 ENCENDIDO"); // Imprime un mensaje si el estado ha cambiado
previousState = 1;
}
}
}
}
}