Created
May 1, 2018 01:32
-
-
Save ninapavlich/658268ffc67c033cd9b8a58028ddf7f0 to your computer and use it in GitHub Desktop.
First few bars of Gymnopedia No 1 - very very roughly
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
First few bars of Gymnopedia No 1 - very very roughly | |
Requires pitches.h | |
nina@ninalp.com | |
*/ | |
#include "pitches.h" | |
const int speakerPinMelody = 3; | |
// notes in the melody: | |
int melody[] = { | |
NOTE_G2, NOTE_B2, | |
NOTE_D2, NOTE_D3, | |
NOTE_G2, NOTE_B2, | |
NOTE_D2, NOTE_D3, | |
NOTE_G2, NOTE_FS5, NOTE_A5, NOTE_G5, NOTE_FS5, NOTE_CS5, NOTE_B4, NOTE_CS5, NOTE_D5, NOTE_A4, | |
NOTE_G2, NOTE_B2, | |
NOTE_D2, NOTE_D3, | |
NOTE_G2, NOTE_B2, | |
NOTE_D2, NOTE_D3, | |
NOTE_G2, NOTE_FS5, NOTE_A5, NOTE_G5, NOTE_FS5, NOTE_CS5, NOTE_B4, NOTE_CS5, NOTE_D5, NOTE_A4 | |
}; | |
// note durations: 4 = quarter note, 8 = eighth note, etc.: | |
int durations[] = { | |
2, 1, | |
2, 1, | |
2, 1, | |
2, 1, | |
2, 2, 2, 2, 2, 2, 2, 2, 2, 1, | |
2, 1, | |
2, 1, | |
2, 1, | |
2, 1, | |
2, 2, 2, 2, 2, 2, 2, 2, 2, 1 | |
}; | |
void setup() { | |
pinMode(speakerPinMelody, OUTPUT); | |
} | |
void playSong() { | |
// iterate over the notes of the melody: | |
int totalNotes = int(sizeof(melody)); | |
for (int thisNote = 0; thisNote < totalNotes; 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 / durations[thisNote]; | |
tone(speakerPinMelody, melody[thisNote], noteDuration); | |
// tone(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(speakerPinMelody); | |
} | |
delay(0); | |
} | |
void loop() { | |
// no need to repeat the melody. | |
playSong(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment