Skip to content

Instantly share code, notes, and snippets.

@mathiasose
Last active August 29, 2015 14:10
Show Gist options
  • Save mathiasose/e5e5d1c19ea991330810 to your computer and use it in GitHub Desktop.
Save mathiasose/e5e5d1c19ea991330810 to your computer and use it in GitHub Desktop.
#include "pitches.h"
int melody[] = {
NOTE_G3, NOTE_G3, NOTE_G3, NOTE_E4, NOTE_D4, NOTE_C4, NOTE_G3,
NOTE_G3, NOTE_G3, NOTE_G3, NOTE_E4, NOTE_D4, NOTE_C4, NOTE_A3,
NOTE_A3, NOTE_A3, NOTE_F4, NOTE_E4, NOTE_D4, NOTE_B3,
NOTE_G4, NOTE_A4, NOTE_G4, NOTE_F4, NOTE_D4, NOTE_E4,
NOTE_G3, NOTE_G3, NOTE_G3, NOTE_E4, NOTE_D4, NOTE_C4, NOTE_G3,
NOTE_E4, NOTE_E4, NOTE_D4, NOTE_C4, NOTE_A3,
NOTE_A3, NOTE_A3, NOTE_F4, NOTE_E4, NOTE_D4, NOTE_G4, NOTE_G4, NOTE_G4,
NOTE_G4, NOTE_A4, NOTE_G4, NOTE_F4, NOTE_D4, NOTE_C4
};
int MELODY_LENGTH = 52;
// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] = {
8, 8, 4, 4, 4, 4, 2,
8, 8, 4, 4, 4, 4, 2,
8, 4, 4, 4, 4, 2,
8, 4, 4, 4, 4, 2,
8, 8, 4, 4, 4, 4, 2,
4, 4, 4, 4, 2,
8, 4, 4, 4, 4, 4, 4, 4,
8, 4, 4, 4, 4, 2
};
void setup() {
// iterate over the notes of the melody:
for (int thisNote = 0; thisNote < MELODY_LENGTH; 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(8, 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(8);
}
}
void loop() {
// no need to repeat the melody.
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment