Skip to content

Instantly share code, notes, and snippets.

@ibanezmatt13
Created August 18, 2013 18:38
Show Gist options
  • Save ibanezmatt13/6263218 to your computer and use it in GitHub Desktop.
Save ibanezmatt13/6263218 to your computer and use it in GitHub Desktop.
#include "pitches.h"
boolean state;
// notes in the melody:
int melody[] = {
NOTE_G4, NOTE_E4,NOTE_C4, NOTE_D4, NOTE_G3, NOTE_G3, NOTE_B3, NOTE_D4, NOTE_F4, NOTE_E4, NOTE_C4};
int verse1[] = {
NOTE_E5, NOTE_C3, NOTE_C3, NOTE_E5, NOTE_G5, NOTE_G2, NOTE_G2, NOTE_G2,0};
int verse2[] = {
NOTE_E5, NOTE_C3, NOTE_C3, NOTE_E5, NOTE_D5, NOTE_G2, NOTE_G2, NOTE_G2,0};
int verse3[] = {
NOTE_G5, NOTE_C3, NOTE_C3, NOTE_G5, NOTE_C6, NOTE_G2, NOTE_G2, NOTE_G2,0};
int ending[] = {
NOTE_G4, NOTE_E4,NOTE_C4, NOTE_D4, NOTE_G3, NOTE_G3, NOTE_A3, NOTE_B3, NOTE_C4,0};
int versedurations[] = {
2,2,2,2,2,2,2,2};
// note durations: 4 = quarter note, 8 = eighth note, etc.:
int melodydurations[] = {
2,4,4,2,2,4,4,4,4,2,2};
int enddurations[] = {
2,4,4,2,2,4,4,2,2,2};
void setup()
{
pinMode(7, INPUT);
}
void play()
{
// iterate over the notes of the melody:
for (int thisNote = 0; thisNote < 11; 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/melodydurations[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 * 0.5;
delay(pauseBetweenNotes);
// stop the tone playing:
noTone(8);
}
for (int thisNote = 0; thisNote < 8; 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/versedurations[thisNote];
tone(8, verse1[thisNote],noteDuration);
// to distinguish the notes, set a minimum time between them.
// the note's duration + 30% seems to work well:
int pauseBetweenNotes = noteDuration * 0.5;
delay(pauseBetweenNotes);
// stop the tone playing:
noTone(8);
}
for (int thisNote = 0; thisNote < 11; 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/melodydurations[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 * 0.5;
delay(pauseBetweenNotes);
// stop the tone playing:
noTone(8);
}
for (int thisNote = 0; thisNote < 8; 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/versedurations[thisNote];
tone(8, verse2[thisNote],noteDuration);
// to distinguish the notes, set a minimum time between them.
// the note's duration + 30% seems to work well:
int pauseBetweenNotes = noteDuration * 0.5;
delay(pauseBetweenNotes);
// stop the tone playing:
noTone(8);
}
for (int thisNote = 0; thisNote < 11; 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/melodydurations[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 * 0.5;
delay(pauseBetweenNotes);
// stop the tone playing:
noTone(8);
}
for (int thisNote = 0; thisNote < 8; 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/versedurations[thisNote];
tone(8, verse3[thisNote],noteDuration);
// to distinguish the notes, set a minimum time between them.
// the note's duration + 30% seems to work well:
int pauseBetweenNotes = noteDuration * 0.5;
delay(pauseBetweenNotes);
// stop the tone playing:
noTone(8);
}
for (int thisNote = 0; thisNote < 10; 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/enddurations[thisNote];
tone(8, ending[thisNote],noteDuration);
// to distinguish the notes, set a minimum time between them.
// the note's duration + 30% seems to work well:
int pauseBetweenNotes = noteDuration * 0.5;
delay(pauseBetweenNotes);
// stop the tone playing:
noTone(8);
}
}
void loop()
{
play()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment