#include "pitches.h"
#define SPEAKER_PIN 8
const int melody[] = {
NOTE_D5, NOTE_D5, NOTE_D5, NOTE_D5,
NOTE_AS4, NOTE_C5, NOTE_D5, NOTE_CS5,
NOTE_D5
};
const int noteDurations[] = {
4, 4, 4, 2,
2, 2, 4, 4,
1
};
void buzzer() {
// iterate over the notes of the melody:
int size = sizeof(noteDurations) / sizeof(int);
for (int thisNote = 0; thisNote < size; thisNote++) {
// to calculate the note duration, take one second divided by the note type.
//e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
int noteDuration = 1000 / noteDurations[thisNote];
tone(SPEAKER_PIN, melody[thisNote], noteDuration);
// to distinguish the notes, set a minimum time between them.
// the note's duration + 30% seems to work well:
int pauseBetweenNotes = noteDuration * 1.30;
delay(pauseBetweenNotes);
// stop the tone playing:
noTone(SPEAKER_PIN);
}
}
void setup() {
pinMode(SPEAKER_PIN, OUTPUT);
}
void loop() {
buzzer();
}