Instantly share code, notes, and snippets.

Embed
What would you like to do?
First few bars of Frolic (Curb Your Enthusiasm theme) for Arudino
/*
First few bars of Frolic from Curb Your Enthusiasm
Requires pitches.h
nina@ninalp.com
*/
#include "pitches.h"
const int speakerPinMelody = 9;
const int speakerPinTweeter = 10; /* not really necessary; just for comedic effect */
// notes in the melody:
int melody[] = {
NOTE_G2, NOTE_A2, NOTE_B2,
NOTE_B5, NOTE_B5, NOTE_B5, NOTE_B5, NOTE_AS5, NOTE_A5, NOTE_AS5, NOTE_B5, NOTE_AS5, NOTE_A5,
NOTE_A5, NOTE_A5, 0, NOTE_AS5, NOTE_B5, NOTE_B5, NOTE_B5, NOTE_B5, NOTE_AS5, NOTE_A5, NOTE_AS5, NOTE_B5, 0, NOTE_A7, 0, NOTE_G2, 0,
NOTE_A5, NOTE_A5, NOTE_A5, NOTE_A5, NOTE_GS5, NOTE_G5, NOTE_GS5, NOTE_A5, NOTE_GS5, NOTE_G5,
NOTE_G5, NOTE_G5, 0, NOTE_GS5, NOTE_A5, NOTE_A5, NOTE_A5, NOTE_A5, NOTE_GS5, NOTE_G5, NOTE_GS5, NOTE_A5, 0,
};
// note durations: 4 = quarter note, 8 = eighth note, etc.:
int durations[] = {
3, 3, 3,
8, 8, 8, 4, 8, 2, 8, 4, 8, 2,
8, 8, 8, 8, 8, 8, 8, 4, 8, 2, 8, 4, 8, 8, 4, 4, 2,
8, 8, 8, 4, 8, 2, 8, 4, 8, 2,
8, 8, 8, 8, 8, 8, 8, 4, 8, 2, 8, 4, 6
};
int pins[] = {
speakerPinMelody, speakerPinMelody, speakerPinMelody,
speakerPinMelody, speakerPinMelody, speakerPinMelody, speakerPinMelody, speakerPinMelody, speakerPinMelody, speakerPinMelody, speakerPinMelody, speakerPinMelody, speakerPinMelody,
speakerPinMelody, speakerPinMelody, speakerPinMelody, speakerPinMelody, speakerPinMelody, speakerPinMelody, speakerPinMelody, speakerPinMelody, speakerPinMelody, speakerPinMelody, speakerPinMelody, speakerPinMelody, speakerPinMelody, speakerPinTweeter, speakerPinMelody, speakerPinMelody, speakerPinMelody,
speakerPinMelody, speakerPinMelody, speakerPinMelody, speakerPinMelody, speakerPinMelody, speakerPinMelody, speakerPinMelody, speakerPinMelody, speakerPinMelody, speakerPinMelody,
speakerPinMelody, speakerPinMelody, speakerPinMelody, speakerPinMelody, speakerPinMelody, speakerPinMelody, speakerPinMelody, speakerPinMelody, speakerPinMelody, speakerPinMelody, speakerPinMelody, speakerPinMelody, speakerPinMelody,
};
void setup() {
}
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];
int pin = pins[thisNote];
tone(pin, 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);
noTone(speakerPinTweeter);
}
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