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

Comments
Only logged in users can leave comments