Devices & Components
Arduino Uno Rev3
Solderless Breadboard Half Size
Speaker: 0.25W, 8 ohms
Buzzer
Jumper wires (generic)
Software & Tools
Arduino IDE
Project description
Code
Happy Birthday Music Arduino Code
arduino
1int speakerPin = 7; // Buzzer pin 2int length = 28; // the number of notes 3char notes[] = "GGAGcB GGAGdc GGxecBA yyecdc"; 4int beats[] = {2,2,8,8,8,16,1,2,2,8,8,8,16,1,2,2,8,8,8,8,16,1,2,2,8,8,8,16}; 5int tempo = 200;// time delay between notes 6 7void playTone(int tone, int duration) { 8for (long i = 0; i < duration * 1000L; i += tone * 2) { 9 digitalWrite(speakerPin, HIGH); 10 delayMicroseconds(tone); 11 digitalWrite(speakerPin, LOW); 12 delayMicroseconds(tone); 13} 14} 15 16void playNote(char note, int duration) { 17char names[] = {'C', 'D', 'E', 'F', 'G', 'A', 'B', 18 19 'c', 'd', 'e', 'f', 'g', 'a', 'b', 20 21 'x', 'y' }; 22 23int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 24 25 956, 834, 765, 593, 468, 346, 224, 26 27 655 , 715 }; 28 29int SPEE = 5; 30 31// play the tone corresponding to the note name 32 33for (int i = 0; i < 17; i++) { 34 35 if (names[i] == note) { 36 int newduration = duration/SPEE; 37 playTone(tones[i], newduration); 38 } 39} 40} 41 42void setup() { 43pinMode(speakerPin, OUTPUT); 44} 45 46void loop() { 47for (int i = 0; i < length; i++) { 48 if (notes[i] == ' ') { 49 delay(beats[i] * tempo); // delay between notes 50 } else { 51 playNote(notes[i], beats[i] * tempo); 52 } 53 // time delay between notes 54 delay(tempo); 55} 56}
Happy Birthday Music Arduino Code
arduino
1int speakerPin = 7; // Buzzer pin 2int length = 28; // the number of notes 3char notes[] = "GGAGcB GGAGdc GGxecBA yyecdc"; 4int beats[] = {2,2,8,8,8,16,1,2,2,8,8,8,16,1,2,2,8,8,8,8,16,1,2,2,8,8,8,16}; 5int tempo = 200;// time delay between notes 6 7void playTone(int tone, int duration) { 8for (long i = 0; i < duration * 1000L; i += tone * 2) { 9 digitalWrite(speakerPin, HIGH); 10 delayMicroseconds(tone); 11 digitalWrite(speakerPin, LOW); 12 delayMicroseconds(tone); 13} 14} 15 16void playNote(char note, int duration) { 17char names[] = {'C', 'D', 'E', 'F', 'G', 'A', 'B', 18 19 'c', 'd', 'e', 'f', 'g', 'a', 'b', 20 21 'x', 'y' }; 22 23int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 24 25 956, 834, 765, 593, 468, 346, 224, 26 27 655 , 715 }; 28 29int SPEE = 5; 30 31// play the tone corresponding to the note name 32 33for (int i = 0; i < 17; i++) { 34 35 if (names[i] == note) { 36 int newduration = duration/SPEE; 37 playTone(tones[i], newduration); 38 } 39} 40} 41 42void setup() { 43pinMode(speakerPin, OUTPUT); 44} 45 46void loop() { 47for (int i = 0; i < length; i++) { 48 if (notes[i] == ' ') { 49 delay(beats[i] * tempo); // delay between notes 50 } else { 51 playNote(notes[i], beats[i] * tempo); 52 } 53 // time delay between notes 54 delay(tempo); 55} 56}
Downloadable files
Wire connection Buzzer with arduino
Wire connection Buzzer with arduino

Comments
Only logged in users can leave comments