Never gonna Give you Up With Passive Buzzer!
Connecting a Passive Buzzer To An Arduino To Make it Be Able to Sing!
Components and supplies
1
Jumper wires (generic)
1
Arduino UNO
Apps and platforms
1
Arduino IDE
Project description
Code
The Code
c_cpp
Code
1/* RickRollCode 2 3 AUTHOR: Rowan Packard 4 rowanpackard@gmail.com 5 6 DISCLAIMER: The song "Never Gonna Give You Up" by Rick Astley 7 is not the creative property of the author. This code simply 8 plays a Piezo buzzer rendition of the song. 9*/ 10 11#define a3f 208 // 208 Hz 12#define b3f 233 // 233 Hz 13#define b3 247 // 247 Hz 14#define c4 261 // 261 Hz MIDDLE C 15#define c4s 277 // 277 Hz 16#define e4f 311 // 311 Hz 17#define f4 349 // 349 Hz 18#define a4f 415 // 415 Hz 19#define b4f 466 // 466 Hz 20#define b4 493 // 493 Hz 21#define c5 523 // 523 Hz 22#define c5s 554 // 554 Hz 23#define e5f 622 // 622 Hz 24#define f5 698 // 698 Hz 25#define f5s 740 // 740 Hz 26#define a5f 831 // 831 Hz 27 28#define rest -1 29 30int piezo = 9; // Connect your piezo buzzer to this pin or change it to match your circuit! 31int led = LED_BUILTIN; 32 33volatile int beatlength = 100; // determines tempo 34float beatseparationconstant = 0.3; 35 36int threshold; 37 38int a; // part index 39int b; // song index 40int c; // lyric index 41 42boolean flag; 43 44// Parts 1 and 2 (Intro) 45 46int song1_intro_melody[] = 47{c5s, e5f, e5f, f5, a5f, f5s, f5, e5f, c5s, e5f, rest, a4f, a4f}; 48 49int song1_intro_rhythmn[] = 50{6, 10, 6, 6, 1, 1, 1, 1, 6, 10, 4, 2, 10}; 51 52// Parts 3 or 5 (Verse 1) 53 54int song1_verse1_melody[] = 55{ rest, c4s, c4s, c4s, c4s, e4f, rest, c4, b3f, a3f, 56 rest, b3f, b3f, c4, c4s, a3f, a4f, a4f, e4f, 57 rest, b3f, b3f, c4, c4s, b3f, c4s, e4f, rest, c4, b3f, b3f, a3f, 58 rest, b3f, b3f, c4, c4s, a3f, a3f, e4f, e4f, e4f, f4, e4f, 59 c4s, e4f, f4, c4s, e4f, e4f, e4f, f4, e4f, a3f, 60 rest, b3f, c4, c4s, a3f, rest, e4f, f4, e4f 61}; 62 63int song1_verse1_rhythmn[] = 64{ 2, 1, 1, 1, 1, 2, 1, 1, 1, 5, 65 1, 1, 1, 1, 3, 1, 2, 1, 5, 66 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 3, 67 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 4, 68 5, 1, 1, 1, 1, 1, 1, 1, 2, 2, 69 2, 1, 1, 1, 3, 1, 1, 1, 3 70}; 71 72const char* lyrics_verse1[] = 73{ "We're ", "no ", "strangers ", "", "to ", "love ", "", "\ \ 74", 75 "You ", "know ", "the ", "rules ", "and ", "so ", "do ", "I\ \ 76", 77 "A ", "full ", "commitment's ", "", "", "what ", "I'm ", "thinking ", "", "of", "\ \ 78", 79 "You ", "wouldn't ", "", "get ", "this ", "from ", "any ", "", "other ", "", "guy\ \ 80", 81 "I ", "just ", "wanna ", "", "tell ", "you ", "how ", "I'm ", "feeling", "\ \ 82", 83 "Gotta ", "", "make ", "you ", "understand", "", "\ \ 84" 85}; 86 87// Parts 4 or 6 (Chorus) 88 89int song1_chorus_melody[] = 90{ b4f, b4f, a4f, a4f, 91 f5, f5, e5f, b4f, b4f, a4f, a4f, e5f, e5f, c5s, c5, b4f, 92 c5s, c5s, c5s, c5s, 93 c5s, e5f, c5, b4f, a4f, a4f, a4f, e5f, c5s, 94 b4f, b4f, a4f, a4f, 95 f5, f5, e5f, b4f, b4f, a4f, a4f, a5f, c5, c5s, c5, b4f, 96 c5s, c5s, c5s, c5s, 97 c5s, e5f, c5, b4f, a4f, rest, a4f, e5f, c5s, rest 98}; 99 100int song1_chorus_rhythmn[] = 101{ 1, 1, 1, 1, 102 3, 3, 6, 1, 1, 1, 1, 3, 3, 3, 1, 2, 103 1, 1, 1, 1, 104 3, 3, 3, 1, 2, 2, 2, 4, 8, 105 1, 1, 1, 1, 106 3, 3, 6, 1, 1, 1, 1, 3, 3, 3, 1, 2, 107 1, 1, 1, 1, 108 3, 3, 3, 1, 2, 2, 2, 4, 8, 4 109}; 110 111const char* lyrics_chorus[] = 112{ "Never ", "", "gonna ", "", "give ", "you ", "up\ \ 113", 114 "Never ", "", "gonna ", "", "let ", "you ", "down", "", "\ \ 115", 116 "Never ", "", "gonna ", "", "run ", "around ", "", "", "", "and ", "desert ", "", "you\ \ 117", 118 "Never ", "", "gonna ", "", "make ", "you ", "cry\ \ 119", 120 "Never ", "", "gonna ", "", "say ", "goodbye ", "", "", "\ \ 121", 122 "Never ", "", "gonna ", "", "tell ", "a ", "lie ", "", "", "and ", "hurt ", "you\ \ 123" 124}; 125 126void setup() 127{ 128 pinMode(piezo, OUTPUT); 129 pinMode(led, OUTPUT); 130 131 digitalWrite(led, LOW); 132 Serial.begin(9600); 133 flag = true; 134 a = 4; 135 b = 0; 136 c = 0; 137} 138 139void loop() 140{ 141 142 143 // play next step in song 144 if (flag == true) { 145 play(); 146 } 147} 148 149void play() { 150 int notelength; 151 if (a == 1 || a == 2) { 152 // intro 153 notelength = beatlength * song1_intro_rhythmn[b]; 154 if (song1_intro_melody[b] > 0) { 155 digitalWrite(led, HIGH); 156 tone(piezo, song1_intro_melody[b], notelength); 157 } 158 b++; 159 if (b >= sizeof(song1_intro_melody) / sizeof(int)) { 160 a++; 161 b = 0; 162 c = 0; 163 } 164 } 165 else if (a == 3 || a == 5) { 166 // verse 167 notelength = beatlength * 2 * song1_verse1_rhythmn[b]; 168 if (song1_verse1_melody[b] > 0) { 169 digitalWrite(led, HIGH); 170 Serial.print(lyrics_verse1[c]); 171 tone(piezo, song1_verse1_melody[b], notelength); 172 c++; 173 } 174 b++; 175 if (b >= sizeof(song1_verse1_melody) / sizeof(int)) { 176 a++; 177 b = 0; 178 c = 0; 179 } 180 } 181 else if (a == 4 || a == 6) { 182 // chorus 183 notelength = beatlength * song1_chorus_rhythmn[b]; 184 if (song1_chorus_melody[b] > 0) { 185 digitalWrite(led, HIGH); 186 Serial.print(lyrics_chorus[c]); 187 tone(piezo, song1_chorus_melody[b], notelength); 188 c++; 189 } 190 b++; 191 if (b >= sizeof(song1_chorus_melody) / sizeof(int)) { 192 Serial.println(""); 193 a++; 194 b = 0; 195 c = 0; 196 } 197 } 198 delay(notelength); 199 noTone(piezo); 200 digitalWrite(led, LOW); 201 delay(notelength * beatseparationconstant); 202 if (a == 7) { // loop back around to beginning of song 203 a = 1; 204 } 205}
Downloadable files
Scematic
Scematic

Scematic
Scematic

Comments
Only logged in users can leave comments