-
Notifications
You must be signed in to change notification settings - Fork 0
/
ThemeSongModule.h
57 lines (42 loc) · 1.31 KB
/
ThemeSongModule.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/**
* ThemeSongModule.h - Plays the theme song!!
*/
#include "Melody.h"
int tempo = 200;
int buzzer = BUZZER_PIN;
// 2 values per note, pitch & duration, so notecount is divided by 2 at the end
int notes = sizeof(melody) / sizeof(melody[0]) / 2;
// this calculates the duration of a whole note in ms
int wholenote = (60000 * 4) / tempo;
int divider = 0, noteDuration = 0;
int thisNote = 0;
long long noteChangeTime = 0;
void themeSongLoop() {
// iterate over the notes of the melody.
// Remember, the array is twice the number of notes (notes + durations)
if (thisNote >= notes * 2) {
thisNote = 0;
return;
}
long long currentTime = millis();
// if should still play current note - return
if (currentTime - noteChangeTime < noteDuration) {
return;
}
noteChangeTime = currentTime;
// need to change the note
noTone(buzzer);
// 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; // dotted notes are longer
}
// 10% pause, note change time
tone(buzzer, melody[thisNote], noteDuration);
thisNote += 2;
}