#include <Arduino_FreeRTOS.h>
#include <LiquidCrystal_I2C.h>
// Configuración de pines
const int buzzerPin = 7;
const int switchPin = 8;
const int notes[] = {262, 294, 330, 349, 392, 440, 494}; // Do, Re, Mi, Fa, Sol, La, Si
const char* noteNames[] = {"Do", "Re", "Mi", "Fa", "Sol", "La", "Si"};
const int numNotes = 7;
LiquidCrystal_I2C lcd(0x27, 16, 2);
static void Task_1(void* pvParameters);
static void Task_2(void* pvParameters);
void setup() {
pinMode(switchPin, INPUT);
pinMode(buzzerPin, OUTPUT);
lcd.init();
lcd.backlight();
xTaskCreate(
Task_1, // Función de la tarea
"Task no. 1", // Nombre de la tarea
128, // Stack size
NULL, // Parámetros
1, // Prioridad
NULL // Handle
);
xTaskCreate(
Task_2, // Función de la tarea
"Task no. 2", // Nombre de la tarea
128, // Stack size
NULL, // Parámetros
2, // Prioridad
NULL // Handle
);
}
static void Task_1(void* pvParameters) {
while (1) {
if (digitalRead(switchPin) == HIGH) {
for (int i = 0; i < numNotes; i++) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(noteNames[i]);
vTaskDelay(pdMS_TO_TICKS(1000));
}
} else {
for (int i = numNotes - 1; i >= 0; i--) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(noteNames[i]);
vTaskDelay(pdMS_TO_TICKS(1000));
}
}
}
}
static void Task_2(void* pvParameters) {
while (1) {
if (digitalRead(switchPin) == HIGH) {
for (int i = 0; i < numNotes; i++) {
tone(buzzerPin, notes[i], 500);
vTaskDelay(pdMS_TO_TICKS(1000));
}
} else {
for (int i = numNotes - 1; i >= 0; i--) {
tone(buzzerPin, notes[i], 500);
vTaskDelay(pdMS_TO_TICKS(1000));
}
}
}
}
void loop() {
// Vacío porque usamos FreeRTOS
}