Skip to content

Instantly share code, notes, and snippets.

@rubyist
Last active January 1, 2016 05:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rubyist/8099505 to your computer and use it in GitHub Desktop.
Save rubyist/8099505 to your computer and use it in GitHub Desktop.
"You're The Best" from Karate Kid. Patches for accuracy welcome.
const int buzzerPin = 6;
// Trick these 4 out for accuracy
const int songLength = 17;
char notes[] = "efg dab bbbbbaagg";
int beats[] = {1,2,5,3,2,4,2,5,2,2,2,2,3,3,3,3,2};
int tempo = 90;
// Boom. Gold.
void setup()
{
pinMode(buzzerPin, OUTPUT);
}
void loop()
{
int i, duration;
for (i = 0; i < songLength; i++) {
duration = beats[i] * tempo;
if (notes[i] == ' ') {
delay(duration);
} else {
tone(buzzerPin, frequency(notes[i]), duration);
delay(duration);
}
delay(tempo/10);
}
delay(500);
}
int frequency(char note)
{
int i;
const int numNotes = 8;
char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };
int frequencies[] = {262, 294, 330, 349, 392, 440, 494, 523};
for (i = 0; i < numNotes; i++)
{
if (names[i] == note)
{
return(frequencies[i]);
}
}
return(0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment