Components and supplies
Buzzer
Rotary Potentiometer, 10 kohm
Male/Female Jumper Wires
Male/Male Jumper Wires
Alphanumeric LCD, 16 x 2
LED (generic)
Arduino UNO
Resistor 220 ohm
Project description
Code
Happy Birthday
c_cpp
1#include <LiquidCrystal.h> 2#include "pitches.h" 3LiquidCrystal lcd(7, 8, 9, 10, 11, 12); 4int speakerPin = 5; // the speaker attach to 5int length = 26; // the nuber of notes 6char notes[] = "aabacdaabaecaahfcndbggfcec "; // a space represents a rest 7int beats[] = {2, 2, 4, 4, 4, 8, 2, 2, 4, 4, 4, 8, 2, 2, 4, 4, 4, 4, 0, 4, 3, 1, 4, 4, 4, 8, 16 }; 8int tempo = 75; 9 10 11void playTone(int tone, int duration) { 12for (long i = 0; i < duration * 1000L; i += tone * 2) { 13digitalWrite(speakerPin, HIGH); 14delayMicroseconds(tone); 15digitalWrite(speakerPin, LOW); 16delayMicroseconds(tone); 17} 18} 19void playNote(char note, int duration) { 20char names[] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'n' }; 21int tones[] = { 1515, 1351, 1136, 1205, 1012, 903, 852, 759 }; // play the tone corresponding to the note name 22 23for (int i = 0; i < 8; i++) { 24if (names[i] == note) { 25playTone(tones[i], duration); 26} 27} 28} 29void setup() { 30 lcd.begin(16, 2); 31 lcd.print("EPPI BORDAI TO"); 32 pinMode(LED_BUILTIN, OUTPUT); 33 pinMode(speakerPin, OUTPUT); // initialize the speaker as an output 34 pinMode(2, INPUT); 35 digitalWrite(2, HIGH); 36} 37 38void loop() { 39 lcd.setCursor(0, 1); 40 lcd.print("MI"); 41 digitalWrite(LED_BUILTIN, HIGH); 42 delay(2000); 43 digitalWrite(LED_BUILTIN, LOW); 44 delay(2000); 45 for (int i = 0; i < length; i++) { 46if (notes[i] == ' ') { 47delay(beats[i] * tempo); // rest 48} 49else { 50playNote(notes[i], beats[i] * tempo); 51// pause between notes 52 53 if (notes[i + 1] != 'n') { 54 delay(tempo ); 55 } 56 else { 57 i += 2; 58 int digitalVal = digitalRead(2); 59 if(HIGH == digitalVal) 60 { 61 digitalWrite(speakerPin, HIGH); // turn the speaker off 62 } 63 else 64 { 65 digitalWrite(speakerPin, LOW); // turn the speaker on 66 } 67} 68} 69} 70} 71
Happy Birthday
c_cpp
1#include <LiquidCrystal.h> 2#include "pitches.h" 3LiquidCrystal 4 lcd(7, 8, 9, 10, 11, 12); 5int speakerPin = 5; // the speaker attach to 6int 7 length = 26; // the nuber of notes 8char notes[] = "aabacdaabaecaahfcndbggfcec 9 "; // a space represents a rest 10int beats[] = {2, 2, 4, 4, 4, 8, 2, 2, 4, 4, 11 4, 8, 2, 2, 4, 4, 4, 4, 0, 4, 3, 1, 4, 4, 4, 8, 16 }; 12int tempo = 75; 13 14 15void 16 playTone(int tone, int duration) { 17for (long i = 0; i < duration * 1000L; i += 18 tone * 2) { 19digitalWrite(speakerPin, HIGH); 20delayMicroseconds(tone); 21digitalWrite(speakerPin, 22 LOW); 23delayMicroseconds(tone); 24} 25} 26void playNote(char note, int duration) 27 { 28char names[] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'n' }; 29int tones[] 30 = { 1515, 1351, 1136, 1205, 1012, 903, 852, 759 }; // play the tone corresponding 31 to the note name 32 33for (int i = 0; i < 8; i++) { 34if (names[i] == note) { 35playTone(tones[i], 36 duration); 37} 38} 39} 40void setup() { 41 lcd.begin(16, 2); 42 lcd.print("EPPI 43 BORDAI TO"); 44 pinMode(LED_BUILTIN, OUTPUT); 45 pinMode(speakerPin, OUTPUT); 46 // initialize the speaker as an output 47 pinMode(2, INPUT); 48 digitalWrite(2, 49 HIGH); 50} 51 52void loop() { 53 lcd.setCursor(0, 1); 54 lcd.print("MI"); 55 56 digitalWrite(LED_BUILTIN, HIGH); 57 delay(2000); 58 digitalWrite(LED_BUILTIN, 59 LOW); 60 delay(2000); 61 for (int i = 0; i < length; i++) { 62if (notes[i] 63 == ' ') { 64delay(beats[i] * tempo); // rest 65} 66else { 67playNote(notes[i], 68 beats[i] * tempo); 69// pause between notes 70 71 if (notes[i + 1] != 'n') { 72 73 delay(tempo ); 74 } 75 else { 76 i += 2; 77 int digitalVal = digitalRead(2); 78 79 if(HIGH == digitalVal) 80 { 81 digitalWrite(speakerPin, HIGH); // turn the 82 speaker off 83 } 84 else 85 { 86 digitalWrite(speakerPin, LOW); // turn 87 the speaker on 88 } 89} 90} 91} 92} 93
Downloadable files
wiring scheme
wiring scheme
Comments
Only logged in users can leave comments
brambi001
5 Followers
•7 Projects
0
1
ChipStewart
2 years ago
What is the difference between the two codes?