#include "pitches.h"
// Pin where the buzzer is connected
#define BUZZER_PIN 9
// Define the notes and durations for the melody (Halo theme intro)
const int melody[] = {
// Enter the notes of the Halo theme intro here
// For example:
NOTE_FS4, NOTE_FS4, NOTE_FS4, NOTE_G4, NOTE_B4, NOTE_AS4, NOTE_A4, NOTE_G4, NOTE_FS4,
NOTE_E4, NOTE_E4, NOTE_FS4, NOTE_E4, NOTE_FS4, NOTE_GS4, NOTE_A4, NOTE_A4, NOTE_G4, NOTE_FS4,
NOTE_FS4, NOTE_G4, NOTE_FS4, NOTE_G4, NOTE_FS4, NOTE_E4, NOTE_FS4, NOTE_E4, NOTE_FS4, NOTE_G4,
NOTE_A4, NOTE_A4, NOTE_G4, NOTE_FS4, NOTE_G4, NOTE_A4, NOTE_G4, NOTE_A4, NOTE_G4, NOTE_FS4,
NOTE_E4, NOTE_FS4, NOTE_E4, NOTE_FS4, NOTE_GS4, NOTE_A4, NOTE_A4, NOTE_G4, NOTE_FS4, NOTE_FS4
};
// Define the note durations
const int noteDurations[] = {
// Enter the durations corresponding to the notes above
// For example:
8, 8, 8, 8, 4, 8, 8, 8, 4,
8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
8, 8, 8, 8, 8, 8, 8, 8, 4, 4
};
// Define beats per minute (BPM)
int bpm = 10;
void setup() {
// Set the buzzer pin as an output
pinMode(BUZZER_PIN, OUTPUT);
}
void loop() {
playMelody();
}
void playMelody() {
// Calculate the duration of a quarter note in milliseconds based on BPM
float quarterNoteDuration = 60000.0 / bpm;
// Iterate over the melody and play each note
for (int i = 0; i < sizeof(melody) / sizeof(melody[0]); i++) {
// Calculate the note duration
int duration = quarterNoteDuration / noteDurations[i];
tone(BUZZER_PIN, melody[i], duration);
// Pause between notes
delay(duration * 1.3);
// Stop playing the note
noTone(BUZZER_PIN);
}
// Pause between repetitions of the melody
delay(2000);
}