/*.Crear un mini piano con ESP32 utilizando botones
y parlante. Al presionar un botón, se genera un
sonido diferente.*/
#define BOCINA_PIN 0
// Pines GPIO para los botones (asegúrate de que sean pines válidos en el ESP32)
const int botones[8] = {32, 33, 25, 26, 27, 14, 12, 13};
// Frecuencias de cada tecla (notas musicales)
const float notas[8] = {261.63, 293.66, 329.63, 349.23, 392.0, 440.0, 493.88, 523.25};
void setup() {
Serial.begin(115200);
pinMode(BOCINA_PIN, OUTPUT);
// Configurar cada pin de los botones como entrada con pull-up
for (int i = 0; i < 8; i++) {
pinMode(botones[i], INPUT_PULLUP);
}
}
void loop() {
bool botonPresionado = false;
// Recorrer los botones y reproducir el tono correspondiente si se presiona
for (int i = 0; i < 8; i++) {
if (digitalRead(botones[i]) == LOW) { // El botón se activa en LOW
tone(BOCINA_PIN, notas[i]); // Tocar la nota correspondiente
botonPresionado = true; // Indicar que hay un botón presionado
break; // Salir del bucle para evitar sonidos superpuestos
}
}
// Si ningún botón está presionado, apagar la bocina
if (!botonPresionado) {
noTone(BOCINA_PIN); // Detener el sonido
}
delay(50); // Pequeño retraso para evitar rebotes
}