int buzzerPin = 8; // Replace with your buzzer pin number
void setup() {
pinMode(buzzerPin, OUTPUT);
Serial.begin(9600); // Initialize serial communication for debugging
}
void loop() {
// Define the melody using numerical values for the Mario theme song
int melody[] = {
2637, 2637, 0, 2637, 0, 2093, 2637, 0,
3136, 0, 0, 0, 2489, 0, 0, 0,
2093, 0, 0, 2489, 0, 0, 2349, 0,
1760, 0, 1976, 0, 1865, 1760, 0,
2489, 2637, 3136, 3520, 0, 2794, 3136, 0,
2637, 0, 2093, 2349, 1976, 0, 0,
2093, 0, 0, 2489, 0, 0, 2349, 0,
1760, 0, 1976, 0, 1865, 1760, 0,
2489, 2637, 3136, 3520, 0, 2794, 3136, 0,
2637, 0, 2093, 2349, 1976, 0, 0
};
int noteDurations[] = {
125, 125, 125, 125, 125, 125, 125, 125,
125, 125, 125, 125, 125, 125, 125, 125,
125, 125, 125, 125, 125, 125, 125, 125,
125, 125, 125, 125, 125, 125, 125,
125, 125, 125, 125, 125, 125, 125, 125,
125, 125, 125, 125, 125, 125, 125,
125, 125, 125, 125, 125, 125, 125, 125,
125, 125, 125, 125, 125, 125, 125, 125,
125, 125, 125, 125, 125, 125, 125, 125,
125, 125, 125, 125, 125, 125, 125
};
// Play the melody of the Mario theme song
for (int i = 0; i < sizeof(melody) / sizeof(melody[0]); i++) {
int noteDuration = 1000 / noteDurations[i];
if (melody[i] != 0) {
tone(buzzerPin, melody[i], noteDuration);
} else {
noTone(buzzerPin);
}
delay(noteDuration * 2.0); // Add a small delay to separate the notes
noTone(buzzerPin);
delay(100); // Add a short pause between the notes
}
}