#include <Arduino.h>
// Pin para la bocina
const int buzzerPin = 13;
// Pin para el botón
const int buttonPin = 2;
// Melodía del Atletic Club (ejemplo)
const int melody[] = {
41, 45, 49, 4, 100, 110, 120, 640
};
const int noteDurations[] = {
4, 8, 8, 4, 4, 4, 4, 4
};
void setup() {
pinMode(buzzerPin, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP);
}
void loop() {
if (digitalRead(buttonPin) == LOW) {
playMelody();
delay(1000); // Evitar repeticiones rápidas al mantener presionado el botón
}
}
void playMelody() {
for (int i = 0; i < sizeof(melody) / sizeof(melody[0]); i++) {
int noteDuration = 1000 / noteDurations[i];
tone(buzzerPin, melody[i], noteDuration);
int pauseBetweenNotes = noteDuration * 1.30;
delay(pauseBetweenNotes);
noTone(buzzerPin);
}
}