Last active
September 12, 2019 09:03
-
-
Save ianklatzco/9127560 to your computer and use it in GitHub Desktop.
Plays the chest noise from Legend of Zelda to a speaker or piezoelectric. Should be used with arduino; also works with a 1MHz attiny if you use the second script.
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
/* This code is derived from: | |
* http://www.arduino.cc/en/Tutorial/Melody | |
* This plays the chest noise from the Legend of Zelda on a piezo buzzer connected to pin 9 and ground. It has been tuned to a buzzer I had on hand, but you may want to adjust the values, testing against a tuner. | |
*/ | |
int speakerPin = 9; | |
const int switchPin = 6; | |
char notes[] = "gabygabyxzCDxzCDabywabywzCDEzCDEbywFCDEqywFGDEqi azbC"; // a space represents a rest | |
int length = sizeof(notes); // the number of notes | |
int beats[] = { 1,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1, 2,3,3,16,}; | |
int tempo = 75/4; | |
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', 'x', 'a', 'z', 'b', 'C', 'y', 'D', 'w', 'E', 'F', 'q', 'G', 'i' }; | |
// c=C4, C = C5. These values have been tuned. | |
int tones[] = { 1898, 1690, 1500, 1420, 1265, 1194, 1126, 1063, 1001, 947, 893, 843, 795, 749, 710, 668, 630, 594 }; | |
// play the tone corresponding to the note name | |
for (int i = 0; i < 18; i++) { | |
if (names[i] == note) { | |
playTone((tones[i])/17, duration); | |
} | |
} | |
} | |
void setup() { | |
pinMode(speakerPin, OUTPUT); | |
pinMode(switchPin, INPUT); | |
digitalWrite(switchPin, HIGH); | |
} | |
void loop() { | |
pinMode(speakerPin, OUTPUT); | |
if (digitalRead(switchPin) == 1) { | |
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); | |
} | |
} | |
delay(100); | |
} |
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
/* This code is derived from: | |
* http://www.arduino.cc/en/Tutorial/Melody | |
* This plays the chest noise from the Legend of Zelda on a piezo buzzer connected to pin 9 and ground. It has been tuned to a buzzer I had on hand, but you may want to adjust the values, testing against a tuner. | |
*/ | |
int speakerPin = 0; | |
const int switchPin = 1; | |
char notes[] = "gabygabyxzCDxzCDabywabywzCDEzCDEbywFCDEqywFGDEqi azbC"; // a space represents a rest | |
int length = sizeof(notes); // the number of notes | |
int beats[] = { 1,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1, 2,3,3,16,}; | |
int tempo = 75; | |
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', 'x', 'a', 'z', 'b', 'C', 'y', 'D', 'w', 'E', 'F', 'q', 'G', 'i' }; | |
// c=C4, C = C5. These values have been tuned. | |
int tones[] = { 1898, 1690, 1500, 1420, 1265, 1194, 1126, 1063, 1001, 947, 893, 843, 795, 749, 710, 668, 630, 594 }; | |
// play the tone corresponding to the note name | |
for (int i = 0; i < 18; i++) { | |
if (names[i] == note) { | |
playTone(tones[i], duration); | |
} | |
} | |
} | |
void setup() { | |
pinMode(switchPin, INPUT); | |
digitalWrite(switchPin, HIGH); | |
} | |
void loop() { | |
pinMode(speakerPin, OUTPUT); | |
if (digitalRead(switchPin) == 1) { | |
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); | |
} | |
} | |
delay(100); | |
} |
excellent, code, but I would like to know how to do it sound only once as it remains in an indefinite loop.
▲
▲ ▲
This is awesome!
I used this in a school assignment where we have to enter a keycode to "unlock" a theoretical vault, and the little tune makes it a lot more fun.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey, thanks for this, I used it in a project and worked out great!
I think you have the two sketches names mixed up though