// Note frequencies
#define NOTE_C4 262
#define NOTE_D4 294
#define NOTE_E4 330
#define NOTE_F4 349
#define NOTE_G4 392
#define NOTE_A4 440
#define NOTE_AS4 466
#define NOTE_C5 523
int buzzer = 8; // Buzzer connected to pin 8
// Melody (Happy Birthday)
int melody[] = {
NOTE_C4, NOTE_C4, NOTE_D4, NOTE_C4, NOTE_F4, NOTE_E4,
NOTE_C4, NOTE_C4, NOTE_D4, NOTE_C4, NOTE_G4, NOTE_F4,
NOTE_C4, NOTE_C4, NOTE_C5, NOTE_A4, NOTE_F4, NOTE_E4, NOTE_D4,
NOTE_AS4, NOTE_AS4, NOTE_A4, NOTE_F4, NOTE_G4, NOTE_F4
};
// Durations (4 = quarter note, 2 = half note, 1 = whole note)
int noteDurations[] = {
4,4,2,2,2,1,
4,4,2,2,2,1,
4,4,2,2,2,2,1,
4,4,2,2,2,1
};
void setup() {
pinMode(buzzer, OUTPUT);
}
void loop() {
// Play all notes in the melody
for (int thisNote = 0; thisNote < 26; thisNote++) {
int noteDuration = 1000 / noteDurations[thisNote];
tone(buzzer, melody[thisNote], noteDuration);
// Small pause between notes
int pauseBetweenNotes = noteDuration * 1.3;
delay(pauseBetweenNotes);
noTone(buzzer); // Stop playing
}
delay(1000); // Pause before repeating
}
// happy birthday tone generate using buzzer