Components and supplies
Arduino UNO
Jumper wires (generic)
Breadboard (generic)
Buzzer
Apps and platforms
Arduino IDE
Project description
Code
Anchors Aweigh
arduino
1/* 2 * Based on the sketch found at https://gist.github.com/elubow/7844436. 3 * The original sketch plays "Jingle Bells" repeatedly on pin 5. 4 * This version plays "Anchors Aweigh" once on pin 13. 5 * No licensing information was found for the original sketch. This version 6 * (or at least the modifications, if the original is under any license) is 7 * Public Domain. 8 * 9 * Notes: 10 * Changed song to "Anchors Aweigh" 11 * Added line to prevent repeated playing 12 * Changed beats[] from int to float 13 * Added notes : 14 * D above high C* : D 15 * E above high C* : E 16 * F Sharp : F 17 -------------- 18 * Note: This is high C, not "high seas." Get it? Navy? 19 */ 20int speakerPin = 13; 21int length = 51; 22char notes[] = "cegaeaCDgCaCagabCFadCbgfdcegaeaCDgCaCagabcEgFgDgfgC"; 23float beats[] = { 2, 1, 1, 1.5, 0.5, 2, 2, 1, 1, 4, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1.5, 0.5, 2, 2, 1, 1, 4, 2, 1, 1, 1, 1, 1, 1, .75, .25, .5, .125, .75, .25, .5, .5, 4}; 24 25int tempo = 300; 26void playTone(int tone, int duration) { 27 for (long i = 0; i < duration * 1000L; i += tone * 2) { 28 digitalWrite(speakerPin, HIGH); 29 delayMicroseconds(tone); 30 digitalWrite(speakerPin, LOW); 31 delayMicroseconds(tone); 32 } 33} 34void playNote(char note, int duration) { 35 char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C', 'D', 'E', 'F' }; 36 int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956, 901, 849, 1390 }; 37 38 // play the tone corresponding to the note name 39 for (int i = 0; i < 11; i++) { 40 if (names[i] == note) { 41 playTone(tones[i], duration); 42 } 43 } 44} 45void setup() { 46 pinMode(speakerPin, OUTPUT); 47} 48void loop() { 49 for (int i = 0; i < length; i++) { 50 if (notes[i] == ' ') { 51 delay(beats[i] * tempo); // rest 52 } else { 53 playNote(notes[i], beats[i] * tempo); 54 } 55 56 // pause between notes 57 delay(tempo / 2); 58 } 59 while (true) {} 60}
Anchors Aweigh
arduino
1/* 2 * Based on the sketch found at https://gist.github.com/elubow/7844436. 3 4 * The original sketch plays "Jingle Bells" repeatedly on pin 5. 5 * This version 6 plays "Anchors Aweigh" once on pin 13. 7 * No licensing information was found 8 for the original sketch. This version 9 * (or at least the modifications, if the 10 original is under any license) is 11 * Public Domain. 12 * 13 * Notes: 14 * 15 Changed song to "Anchors Aweigh" 16 * Added line to prevent repeated playing 17 18 * Changed beats[] from int to float 19 * Added notes : 20 * D above high 21 C* : D 22 * E above high C* : E 23 * F Sharp : F 24 -------------- 25 26 * Note: This is high C, not "high seas." Get it? Navy? 27 */ 28int speakerPin 29 = 13; 30int length = 51; 31char notes[] = "cegaeaCDgCaCagabCFadCbgfdcegaeaCDgCaCagabcEgFgDgfgC"; 32float 33 beats[] = { 2, 1, 1, 1.5, 0.5, 2, 2, 1, 1, 4, 2, 1, 1, 1, 1, 1, 1, 1, 1, 34 1, 1, 1, 1, 1, 1, 2, 1, 1, 1.5, 0.5, 2, 2, 1, 1, 4, 2, 1, 1, 1, 1, 35 1, 1, .75, .25, .5, .125, .75, .25, .5, .5, 4}; 36 37int tempo = 300; 38void 39 playTone(int tone, int duration) { 40 for (long i = 0; i < duration * 1000L; i 41 += tone * 2) { 42 digitalWrite(speakerPin, HIGH); 43 delayMicroseconds(tone); 44 45 digitalWrite(speakerPin, LOW); 46 delayMicroseconds(tone); 47 } 48} 49void 50 playNote(char note, int duration) { 51 char names[] = { 'c', 'd', 'e', 'f', 'g', 52 'a', 'b', 'C', 'D', 'E', 'F' }; 53 int tones[] = { 1915, 1700, 1519, 1432, 1275, 54 1136, 1014, 956, 901, 849, 1390 }; 55 56 // play the tone corresponding to the 57 note name 58 for (int i = 0; i < 11; i++) { 59 if (names[i] == note) { 60 61 playTone(tones[i], duration); 62 } 63 } 64} 65void setup() { 66 pinMode(speakerPin, 67 OUTPUT); 68} 69void loop() { 70 for (int i = 0; i < length; i++) { 71 if 72 (notes[i] == ' ') { 73 delay(beats[i] * tempo); // rest 74 } else { 75 76 playNote(notes[i], beats[i] * tempo); 77 } 78 79 // pause between 80 notes 81 delay(tempo / 2); 82 } 83 while (true) {} 84}
Downloadable files
Circuit Diagram
Circuit Diagram
Schematic
Schematic
Circuit Diagram
Circuit Diagram
Comments
Only logged in users can leave comments
ScoopGracie
0 Followers
•0 Projects
Table of contents
Intro
1
0