//draft by https://www.arduino.cc/en/Tutorial/BuiltInExamples/toneMelody
//optimized and extended by turb0ente on 2022/04/10
#include "pitches.h"
#include "preussens_gloria.h"
// iterate one time over the notes of the melody:
void setup() {
int pauseBetweenNotes = 0; // global declaration
for (int thisNote = 0; thisNote < (sizeof(melody)/sizeof(melody[0])); thisNote++) {
/* CAUTION! sizeof() delivers size in bytes! ;-) Derivate throug any byte(size) of vector
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 = 800 / noteDurations[thisNote]; // on real arduino
int noteDuration = 1400 / noteDurations[thisNote]; // on arduino simulator wokwi (not in rythm :-|)
if (melody[thisNote] == 0) {
noTone(8); // pause, if freq == 0 Hz
} else {
tone(8, melody[thisNote], noteDuration); // play tone, if freq =/= 0 Hz
}
// to distinguish the notes, set a minimum time between them.
// The note's duration + 30% seems to work well
if (noteDurations[thisNote] <= 4/3) {
pauseBetweenNotes = noteDuration*1.10; // correction for note durations > 1/2,
// don't know why but necessary
} else {
pauseBetweenNotes = noteDuration*1.30; // fine for everything but too long for notes > 1/2!
}
delay(pauseBetweenNotes);
}
}
void loop() {
// do nothing
delay(2000);
}