Created
December 7, 2013 16:07
-
-
Save elubow/7844436 to your computer and use it in GitHub Desktop.
Using a Piezo Element and Arduino, you can play jingle bells using this code sample.
This file contains 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
int speakerPin = 5; | |
int length = 26; | |
char notes[] = "eeeeeeegcde fffffeeeeddedg"; | |
int beats[] = { 1, 1, 2, 1, 1, 2, 1, 1, 1, 1, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2}; | |
int tempo = 300; | |
void playTone(int tone, int duration) { | |
for (long i = 0; i < duration * 1000L; i += tone * 2) { | |
digitalWrite(speakerPin, HIGH); | |
delayMicroseconds(tone); | |
digitalWrite(speakerPin, LOW); | |
delayMicroseconds(tone); | |
} | |
} | |
void playNote(char note, int duration) { | |
char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' }; | |
int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956 }; | |
// play the tone corresponding to the note name | |
for (int i = 0; i < 8; i++) { | |
if (names[i] == note) { | |
playTone(tones[i], duration); | |
} | |
} | |
} | |
void setup() { | |
pinMode(speakerPin, OUTPUT); | |
} | |
void loop() { | |
for (int i = 0; i < length; i++) { | |
if (notes[i] == ' ') { | |
delay(beats[i] * tempo); // rest | |
} else { | |
playNote(notes[i], beats[i] * tempo); | |
} | |
// pause between notes | |
delay(tempo / 2); | |
} | |
} |
Will you please add a license?
Will you please add a license?
It's a gist. Use it for whatever you want.
OK, legally, that's a license. But gists aren't automatically public
domain; you own a copyright and should add a license.
…On Sat, Jan 18, 2020 at 2:06 AM Eric Lubow ***@***.***> wrote:
Will you please add a license?
It's a gist. Use it for whatever you want.
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<https://gist.github.com/7844436?email_source=notifications&email_token=AN54XP3OJ6QNGYR7JXHCTFLQ6LIDNA5CNFSM4JUVANW2YY3PNVWWK3TUL52HS4DFVNDWS43UINXW23LFNZ2KUY3PNVWWK3TUL5UWJTQAF7Z5A#gistcomment-3142608>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AN54XP36X3KKGSWACCRJ3BLQ6LIDNANCNFSM4JUVANWQ>
.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
SEND HELP