Last active
January 1, 2016 05:39
-
-
Save rubyist/8099505 to your computer and use it in GitHub Desktop.
"You're The Best" from Karate Kid. Patches for accuracy welcome.
This file contains hidden or 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
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