Created
October 1, 2013 22:19
-
-
Save octosteve/6786124 to your computer and use it in GitHub Desktop.
Imperial march played on an Arduino with a Piezo Element
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 = 9; | |
int length = 70; | |
String notes[] = {"G4","G4", "G4", "D#4/Eb4", "A#4/Bb4", "G4", "D#4/Eb4","A#4/Bb4", "G4", "D5", "D5", "D5", "D#5/Eb5", "A#4/Bb4", "F#4/Gb4", "D#4/Eb4","A#4/Bb4", "G4", "G5","G4","G4","G5","F#5/Gb5", "F5","E5","D#5/Eb5","E5", "rest", "G4", "rest","C#5/Db5","C5","B4","A#4/Bb4","A4","A#4/Bb4", "rest", "D#4/Eb4", "rest", "F#4/Gb4", "D#4/Eb4","A#4/Bb4", "G4" ,"D#4/Eb4","A#4/Bb4", "G4"}; | |
int beats[] = { 8, 8, 8, 6, 2, 8, 6 , 2 ,16 , 8, 8, 8, 6, 2, 8, 6, 2, 16,8,6,2,8,6,2,2, 2, 2,6,2,2,8,6,2,2,2,2,6,2,2,9,6,2,8,6,2,16 }; | |
int tempo = 50; | |
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(String note, int duration) { | |
String noteNames[] = { "D#4/Eb4", "E4", "F4", "F#4/Gb4", "G4", "G#4/Ab4", "A4", "A#4/Bb4", "B4", "C5", "C#5/Db5", "D5", "D#5/Eb5", "E5", "F5", "F#5/Gb5", "G5", "G#5/Ab5", "A5", "A#5/Bb5", "B5", "C6", "C#6/Db6", "D6", "D#6/Eb6", "E6", "F6", "F#6/Gb6", "G6" }; | |
int tones[] = { 1607, 1516, 1431, 1351, 1275, 1203, 1136, 1072, 1012, 955, 901, 851, 803, 758, 715, 675, 637, 601, 568, 536, 506, 477, 450, 425, 401, 379, 357, 337, 318 }; | |
for (int i = 0; i < 29; i++) { | |
if (noteNames[i] == note) { | |
playTone(tones[i], duration); | |
} | |
} | |
} | |
void setup() { | |
pinMode(speakerPin, OUTPUT); | |
} | |
void loop() { | |
for (int i = 0; i < length; i++) { | |
if (notes[i] == "rest") { | |
delay(beats[i] * tempo); | |
} else { | |
playNote(notes[i], beats[i] * tempo); | |
} | |
delay(tempo / 2); | |
} | |
} |
Hello
I found your name in an exercise of computer science. I also found a code who look like a bit like your's
int speakerPin = 9;
int length = 46;
String notes[] {"A#4/Bb4","A#4/Bb4","A#4/Bb4","A#4/Bb4","A#4/Bb4","A#4/Bb4","A#4/Bb4","A#4/Bb4","A4","B4","C#5/Db5","C5","D#4/Eb4","D#4/Eb4","D#4/Eb4","D#4/Eb4","D#4/Eb4","D#4/Eb4","D#5/Eb5","D#5/Eb5","D5","D5","D5","E5","E5","F#4/Gb4","F#4/Gb4","F#5/Gb5","F5","G4","G4","G4","G4","G4","G4","G4","G4","G4","G4","G4","G5","G5","rest","rest","rest","rest"};
int ordreNotes[]={4,7,13,16,33,35,41,44,34,32,30,31,3,6,15,37,40,43,12,25,9,10,11,24,26,14,39,22,23,0,1,2,5,8,17,19,20,28,42,45,18,21,27,29,36,38};
int beats[] = { 8, 8, 8, 6, 2, 8, 6 , 2 ,16 , 8, 8, 8, 6, 2, 8, 6, 2, 16,8,6,2,8,6,2,2, 2, 2,6,2,2,8,6,2,2,2,2,6,2,2,9,6,2,8,6,2,16 };
int tempo = 50;
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(String note, int duration) {
String noteNames[] = { "D#4/Eb4", "E4", "F4", "F#4/Gb4", "G4", "G#4/Ab4", "A4", "A#4/Bb4", "B4", "C5", "C#5/Db5", "D5", "D#5/Eb5", "E5", "F5", "F#5/Gb5", "G5", "G#5/Ab5", "A5", "A#5/Bb5", "B5", "C6", "C#6/Db6", "D6", "D#6/Eb6", "E6", "F6", "F#6/Gb6", "G6" };
int tones[] = { 1607, 1516, 1431, 1351, 1275, 1203, 1136, 1072, 1012, 955, 901, 851, 803, 758, 715, 675, 637, 601, 568, 536, 506, 477, 450, 425, 401, 379, 357, 337, 318 };
for (int i = 0; i < 29; i++) {
if (noteNames[i] == note) {
playTone(tones[i], duration);
}
}
}
void setup() {
Serial.begin(9600);
//rédaction de vos tests unitaires
Serial.println("tests : debut");
// ...
Serial.println("tests : fin");
pinMode(speakerPin, OUTPUT);
}
void loop() {
for (int i = 0; i < length; i++) {
if (notes[i] == "rest") {
delay(beats[i] * tempo);
} else {/*
if(ordreNotes[i] == i){//modifier ici pour aller chercher la note au rang voulu
playNote(notes[(ordreNotes[i])], beats[i] * tempo);
}*/
playNote(notes[tabOrdreNotesIndexSeartch(i)], beats[i] * tempo);
}
delay(tempo / 2);
}
}
int tabOrdreNotesIndexSeartch(int value){
int indexValue=0;
while(indexValue<sizeof(ordreNotes) && ordreNotes[indexValue]!=value){
indexValue++;
}
return indexValue;
}
//Exercice inspiré par le code de Steven Nunez
this is an exercise of the IUT informatics of Bordeaux
I find your work noteworthy.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sounds great! I'll give you credit for this, cheers!