#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
// Configuración de pines
const int buzzerPin = 14; // Pin del buzzer
const int switchPin = 4; // Pin del switch
// Configuración de la pantalla OLED
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
#define SCREEN_ADDRESS 0x3C
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
// Notas musicales (frecuencias en Hz)
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;
// Variable para determinar el orden
volatile bool reverseOrder = false;
// Tarea 1: Reproduce las notas en el altavoz
void playNotes(void* pvParameters) {
while (1) {
for (int i = 0; i < numNotes; i++) {
int index = reverseOrder ? (numNotes - 1 - i) : i;
tone(buzzerPin, notes[index], 500);
vTaskDelay(pdMS_TO_TICKS(1000));
}
vTaskDelay(pdMS_TO_TICKS(500));
}
}
// Tarea 2: Representa las notas en la pantalla OLED
void displayNotes(void* pvParameters) {
while (1) {
for (int i = 0; i < numNotes; i++) {
int index = reverseOrder ? (numNotes - 1 - i) : i;
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(SSD1306_WHITE);
display.setCursor(10, 20);
display.println(noteNames[index]);
display.display();
vTaskDelay(pdMS_TO_TICKS(1000));
}
vTaskDelay(pdMS_TO_TICKS(500));
}
}
void IRAM_ATTR switchInterrupt() {
reverseOrder = !reverseOrder;
}
void setup() {
Serial.begin(115200);
// Inicialización de Wire (I2C)
Wire.begin(22, 21); // SDA = 21, SCL = 22
// Inicialización de pines
pinMode(buzzerPin, OUTPUT);
pinMode(switchPin, INPUT_PULLUP);
// Inicialización de la pantalla OLED
if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
display.clearDisplay();
display.display();
// Configurar la interrupción
attachInterrupt(digitalPinToInterrupt(switchPin), switchInterrupt, CHANGE);
// Crear las tareas
xTaskCreate(
playNotes, // Función de la tarea
"Play Notes", // Nombre de la tarea
2048, // Tamaño del stack
NULL, // Parámetros de la tarea
1, // Prioridad
NULL // Handle de la tarea
);
xTaskCreate(
displayNotes, // Función de la tarea
"Display Notes", // Nombre de la tarea
2048, // Tamaño del stack
NULL, // Parámetros de la tarea
1, // Prioridad
NULL // Handle de la tarea
);
}
void loop() {
// Vacío porque usamos FreeRTOS
vTaskDelay(pdMS_TO_TICKS(1000));
}