#include <EasyBuzzer.h>
#include "songs.h"
int tempo = harry_potter_tempo;
const int size = sizeof(harry_potter) / sizeof(harry_potter[0]);
int melody[size];
int notes;
// this calculates the duration of a whole note in ms
int wholenote;
int divider = 0;
int noteDuration = 0;
void setup()
{
EasyBuzzer.setPin(6);
Serial.begin(115200);
copyArray(harry_potter, melody, size);
notes = sizeof(melody) / sizeof(melody[0]) / 2;
// this calculates the duration of a whole note in ms
wholenote = (60000 * 4) / tempo;
divider = 0, noteDuration = 0;
}
void loop()
{
for (int thisNote = 0; thisNote < notes * 2; thisNote = thisNote + 2) {
// calculates the duration of each note
divider = melody[thisNote + 1];
if (divider > 0) {
// regular note, just proceed
noteDuration = (wholenote) / divider;
} else if (divider < 0) {
// dotted notes are represented with negative durations!!
noteDuration = (wholenote) / abs(divider);
noteDuration *= 1.5; // increases the duration in half for dotted notes
}
// we only play the note for 90% of the duration, leaving 10% as a pause
//tone(buzzer, melody[thisNote], noteDuration * 0.9);
EasyBuzzer.beep(melody[thisNote]);
// Wait for the specief duration before playing the next note.
delay(noteDuration);
// stop the waveform generation before the next note.
//noTone(buzzer);
EasyBuzzer.stopBeep();
}
delay(500);
}