#include "pitches.h"
#define SPEAKER_PIN 8
#define BUTTON_PIN 2 // Change this to the desired button pin
const int melody[] = {
// Super Mario Bros. theme
NOTE_E7,-4, NOTE_E7,-4, 0,-4, NOTE_E7,-4, 0,-4, NOTE_C7,-4,
NOTE_E7,-4, 0,-4, NOTE_G7,-4, 0,-4, 0,-4, NOTE_G6,-4, 0,-4,
0,-4, NOTE_C7,-4, 0,-4, NOTE_G6,-4, 0,-4, 0,-4, NOTE_E6,-4,
0,-4, 0,-4, NOTE_A6,-4, 0,-4, NOTE_B6,-4, 0,-4, NOTE_AS6,-4,
NOTE_A6,-4, NOTE_G6,-4, 0,-4, NOTE_E7,-4, 0,-4, NOTE_G7,-4,
NOTE_A7,-4, 0,-4, NOTE_F7,-4, NOTE_G7,-4, 0,-4, NOTE_E7,-4,
0,-4, NOTE_C7,-4, NOTE_D7,-4, NOTE_B6,-4, 0,-4, 0,-4, 0,-4,
NOTE_G6,-4, 0,-4, 0,-4, NOTE_E6,-4, 0,-4, 0,-4, NOTE_A6,-4,
0,-4, NOTE_B6,-4, 0,-4, NOTE_AS6,-4, NOTE_A6,-4, NOTE_G6,-4,
0,-4, NOTE_E7,-4, 0,-4, NOTE_G7,-4, NOTE_A7,-4, 0,-4, NOTE_F7,-4,
NOTE_G7,-4, 0,-4, NOTE_E7,-4, 0,-4, NOTE_C7,-4, NOTE_D7,-4,
NOTE_B6,-4, 0,-4, 0,-4, 0,-4
};
const int noteDuration = 250; // The duration of each note in milliseconds
void setup() {
pinMode(BUTTON_PIN, INPUT_PULLUP);
pinMode(SPEAKER_PIN, OUTPUT);
}
void loop() {
if (digitalRead(BUTTON_PIN) == LOW) {
playSong();
// Add any additional logic here after the song is played
}
}
void playSong() {
for (int i = 0; i < sizeof(melody) / sizeof(melody[0]); i = i + 2) {
int note = melody[i];
int duration = noteDuration * melody[i + 1];
if (note == 0) {
// Pause if the note is 0
noTone(SPEAKER_PIN);
} else {
// Play the note
tone(SPEAKER_PIN, note, duration);
}
// Delay between notes
delay(noteDuration / 2);
}
}