3-Song Arduino Music Box with LCD and LED Light Show
This project is an Arduino-based mini “jukebox” built in Tinkercad. Three push buttons each play a different melody on a piezo buzzer: “Happy Birthday,” “Jingle Bells,” and “Ode to Joy.” A 16×2 LCD screen shows the current song title, and a row of colored LEDs blinks while the music is playing, creating a simple light show.
Components and supplies
5
LED(Light Emitting Diode) White,Green,Red,Yellow,Blue Color
1
16x2 LCD display with I²C interface
7
220 Ω ohm Resistors
1
10k Ohms Potentiometer
1
Arduino Nano Matter
1
Large Breadbord
1
Piezo speaker
3
Resistor (10k Ohm)
3
push button arduino
Apps and platforms
1
Arduino Web Editor
Project description
Code
3-Song Arduino Music Box Code
cpp
Structure + Explanation
1#include <LiquidCrystal.h> 2 3/************************************************* 4 * Public Constants 5 *************************************************/ 6 7#define NOTE_B0 31 8#define NOTE_C1 33 9#define NOTE_CS1 35 10#define NOTE_D1 37 11#define NOTE_DS1 39 12#define NOTE_E1 41 13#define NOTE_F1 44 14#define NOTE_FS1 46 15#define NOTE_G1 49 16#define NOTE_GS1 52 17#define NOTE_A1 55 18#define NOTE_AS1 58 19#define NOTE_B1 62 20#define NOTE_C2 65 21#define NOTE_CS2 69 22#define NOTE_D2 73 23#define NOTE_DS2 78 24#define NOTE_E2 82 25#define NOTE_F2 87 26#define NOTE_FS2 93 27#define NOTE_G2 98 28#define NOTE_GS2 104 29#define NOTE_A2 110 30#define NOTE_AS2 117 31#define NOTE_B2 123 32#define NOTE_C3 131 33#define NOTE_CS3 139 34#define NOTE_D3 147 35#define NOTE_DS3 156 36#define NOTE_E3 165 37#define NOTE_F3 175 38#define NOTE_FS3 185 39#define NOTE_G3 196 40#define NOTE_GS3 208 41#define NOTE_A3 220 42#define NOTE_AS3 233 43#define NOTE_B3 247 44#define NOTE_C4 262 45#define NOTE_CS4 277 46#define NOTE_D4 294 47#define NOTE_DS4 311 48#define NOTE_E4 330 49#define NOTE_F4 349 50#define NOTE_FS4 370 51#define NOTE_G4 392 52#define NOTE_GS4 415 53#define NOTE_A4 440 54#define NOTE_AS4 466 55#define NOTE_B4 494 56#define NOTE_C5 523 57#define NOTE_CS5 554 58#define NOTE_D5 587 59#define NOTE_DS5 622 60#define NOTE_E5 659 61#define NOTE_F5 698 62#define NOTE_FS5 740 63#define NOTE_G5 784 64#define NOTE_GS5 831 65#define NOTE_A5 880 66#define NOTE_AS5 932 67#define NOTE_B5 988 68#define NOTE_C6 1047 69#define NOTE_CS6 1109 70#define NOTE_D6 1175 71#define NOTE_DS6 1245 72#define NOTE_E6 1319 73#define NOTE_F6 1397 74#define NOTE_FS6 1480 75#define NOTE_G6 1568 76#define NOTE_GS6 1661 77#define NOTE_A6 1760 78#define NOTE_AS6 1865 79#define NOTE_B6 1976 80#define NOTE_C7 2093 81#define NOTE_CS7 2217 82#define NOTE_D7 2349 83#define NOTE_DS7 2489 84#define NOTE_E7 2637 85#define NOTE_F7 2794 86#define NOTE_FS7 2960 87#define NOTE_G7 3136 88#define NOTE_GS7 3322 89#define NOTE_A7 3520 90#define NOTE_AS7 3729 91#define NOTE_B7 3951 92#define NOTE_C8 4186 93#define NOTE_CS8 4435 94#define NOTE_D8 4699 95#define NOTE_DS8 4978 96 97 98//Notes in Happy Birthday song 99int notesHBD[] = { 100 NOTE_C4, NOTE_C4, NOTE_D4, NOTE_C4, NOTE_F4, NOTE_E4, NOTE_C4, NOTE_C4, NOTE_D4, NOTE_C4, NOTE_G4, NOTE_F4, NOTE_C4, NOTE_C4, NOTE_C5, NOTE_A4, NOTE_F4, NOTE_E4, NOTE_D4, NOTE_AS4, NOTE_AS4, NOTE_A4, NOTE_F4, NOTE_G4, NOTE_F4 101}; 102 103//note durations for Happy Birthday to You: 4 = quarter note, 8 = eight note, etc. 104int durationsHBD[] = { 105 8, 8, 4, 4, 4, 2, 8, 8, 4, 4, 4, 2, 8, 8, 4, 4, 4, 4, 4, 8, 8, 4, 4, 4, 2, 106}; 107 108//Notes in Jingle Bells song 109int notesJB[] = { 110NOTE_E5, NOTE_E5, NOTE_E5, 111NOTE_E5, NOTE_E5, NOTE_E5, 112NOTE_E5, NOTE_G5, NOTE_C5, NOTE_D5, 113NOTE_E5, 114NOTE_F5, NOTE_F5, NOTE_F5, NOTE_F5, 115NOTE_F5, NOTE_E5, NOTE_E5, NOTE_E5, NOTE_E5, 116NOTE_E5, NOTE_D5, NOTE_D5, NOTE_E5, 117NOTE_D5, NOTE_G5 118}; 119 120// note durations for Christmas Song: JingleBells 121int durationsJB[] = { 1228, 8, 4, 1238, 8, 4, 1248, 8, 8, 8, 1252, 1268, 8, 8, 8, 1278, 8, 8, 16, 16, 1288, 8, 8, 8, 1294, 4 130}; 131 132//Notes in Ode to Joy - Beethoven's Symphony No. 9 133int notesOde[] = { 134 // 1st section 135 NOTE_E4, NOTE_E4, NOTE_F4, NOTE_G4, 136 NOTE_G4, NOTE_F4, NOTE_E4, NOTE_D4, 137 NOTE_C4, NOTE_C4, NOTE_D4, NOTE_E4, 138 NOTE_E4, NOTE_D4, NOTE_D4, 139 140 // 2nd section 141 NOTE_E4, NOTE_E4, NOTE_F4, NOTE_G4, 142 NOTE_G4, NOTE_F4, NOTE_E4, NOTE_D4, 143 NOTE_C4, NOTE_C4, NOTE_D4, NOTE_E4, 144 NOTE_D4, NOTE_C4, NOTE_C4, 145 146 // 3rd section 147 NOTE_D4, NOTE_D4, NOTE_E4, NOTE_C4, 148 NOTE_D4, NOTE_E4, NOTE_F4, NOTE_E4, 149 NOTE_C4, NOTE_D4, NOTE_E4, NOTE_F4, 150 NOTE_E4, NOTE_D4, NOTE_C4, NOTE_D4, 151 NOTE_G3, 152 153 // 4th section 154 NOTE_E4, NOTE_E4, NOTE_F4, NOTE_G4, 155 NOTE_G4, NOTE_F4, NOTE_E4, NOTE_D4, 156 NOTE_C4, NOTE_C4, NOTE_D4, NOTE_E4, 157 NOTE_D4, NOTE_C4, NOTE_C4 158}; 159 160int durationsOde[] = { 161 // 1st section 162 4, 4, 4, 4, 163 4, 4, 4, 4, 164 4, 4, 4, 4, 165 -4, 8, 2, 166 167 // 2nd section 168 4, 4, 4, 4, 169 4, 4, 4, 4, 170 4, 4, 4, 4, 171 -4, 8, 2, 172 173 // 3rd section 174 4, 4, 4, 4, 175 4, 8, 8, 4, 176 4, 4, 8, 8, 177 4, 4, 4, 4, 178 2, 179 180 // 4th section 181 4, 4, 4, 4, 182 4, 4, 4, 4, 183 4, 4, 4, 4, 184 -4, 8, 2 185}; 186 187// tempo for Ode to Joy (beats per minute) 188int tempoOde = 120; 189 190const int RS = 2; 191const int E = 3; 192const int DB4 = 4; 193const int DB5 = 5; 194const int DB6 = 6; 195const int DB7 = 7; 196 197const int WHITE_LED = A0; 198const int BLUE_LED = A1; 199const int RED_LED = A2; 200const int YELLOW_LED = A3; 201const int GREEN_LED = A4; 202 203const int SPEAKER = 10; 204 205const int BUTTON1 = 13; 206const int BUTTON2 = 12; 207const int BUTTON3 = 11; 208 209LiquidCrystal lcd(RS, E, DB4, DB5, DB6, DB7); 210 211 212void setup() 213{ 214 pinMode(SPEAKER, OUTPUT); 215 216 // Buttons (3) 217 pinMode(BUTTON1, INPUT); 218 pinMode(BUTTON2, INPUT); 219 pinMode(BUTTON3, INPUT); 220 221 // LEDs (5) 222 pinMode(WHITE_LED, OUTPUT); 223 pinMode(BLUE_LED, OUTPUT); 224 pinMode(RED_LED, OUTPUT); 225 pinMode(YELLOW_LED, OUTPUT); 226 pinMode(GREEN_LED, OUTPUT); 227 228 lcd.begin (16, 2); 229} 230 231void loop() 232{ 233 // Check to see if button 1 is pressed 234 if (digitalRead(BUTTON1) == HIGH) { 235 lcd.setCursor(0, 0); 236 lcd.print(" Happy Birthday"); 237 lcd.setCursor(0, 1); 238 lcd.print(" to You! "); 239 playHappyBirthday(); 240 // Play Happy Birthday song 241 // Print Happy Birthday to You (on LCD) 242 // Do something with the LEDs 243 lcd.clear(); 244 } 245 else if (digitalRead(BUTTON2) == HIGH) { 246 lcd.setCursor(0, 0); 247 lcd.print("Merry Christmas"); 248 lcd.setCursor(0, 1); 249 lcd.print(" Jingle Bells!"); 250 playJingleBells(); 251 // Play the Merry Christmas song 252 // Print JingleBells (on LCD) 253 // Do something with the LEDs 254 lcd.clear(); 255 } 256 else if (digitalRead(BUTTON3) == HIGH) { 257 lcd.setCursor(0, 0); 258 lcd.print(" Beethoven"); 259 lcd.setCursor(0, 1); 260 lcd.print(" Ode to Joy!"); 261 playOdeToJoy(); 262 lcd.clear(); 263 } 264 265 // Turn off all 5 LEDs 266 pinMode(WHITE_LED, LOW); 267 pinMode(BLUE_LED, LOW); 268 pinMode(RED_LED, LOW); 269 pinMode(YELLOW_LED, LOW); 270 pinMode(GREEN_LED, LOW); 271} 272 273void playHappyBirthday() { 274 for (int thisNote = 0 ; thisNote < 25 ; thisNote++) { 275 276 int randomLight1 = random(A0, A5); 277 int randomLight2 = random(A0, A5); 278 int randomLight3 = random(A0, A5); 279 int randomLight4 = random(A0, A5); 280 int randomLight5 = random(A0, A5); 281 282 analogWrite (randomLight1, 255); 283 analogWrite (randomLight2, 255); 284 analogWrite (randomLight3, 255); 285 analogWrite (randomLight4, 0); 286 analogWrite (randomLight5, 0); 287 288 int noteDuration = 1130/durationsHBD[thisNote]; 289 tone(SPEAKER, notesHBD[thisNote], noteDuration); 290 291 int pause = noteDuration * 1.275; 292 delay (pause); 293 294 noTone(SPEAKER); 295 } 296} 297 298void playJingleBells() { 299 int length = sizeof(notesJB) / sizeof(notesJB[0]); 300 301 for (int thisNote = 0; thisNote < length; thisNote++) { 302 int randomLight1 = random(A0, A5); 303 int randomLight2 = random(A0, A5); 304 int randomLight3 = random(A0, A5); 305 int randomLight4 = random(A0, A5); 306 int randomLight5 = random(A0, A5); 307 308 analogWrite(randomLight1, 255); 309 analogWrite(randomLight2, 255); 310 analogWrite(randomLight3, 255); 311 analogWrite(randomLight4, 0); 312 analogWrite(randomLight5, 0); 313 314 // quarter note = 1000/4, eighth = 1000/8, etc. 315 int noteDuration = 1000 / durationsJB[thisNote]; 316 317 buzz(SPEAKER, notesJB[thisNote], noteDuration); 318 319 int pauseBetweenNotes = noteDuration * 1.30; 320 delay(pauseBetweenNotes); 321 322 // stop the tone 323 buzz(SPEAKER, 0, noteDuration); 324 } 325} 326 327void buzz(int targetPin, long frequency, long length) { 328 long delayValue = 1000000 / frequency / 2; // calculate the delay value between transitions 329 //// 1 second's worth of microseconds, divided by the frequency, then split in half since 330 //// there are two phases to each cycle 331 long numCycles = frequency * length / 1000; // calculate the number of cycles for proper timing 332 //// multiply frequency, which is really cycles per second, by the number of seconds to 333 //// get the total number of cycles to produce 334 for (long i = 0; i < numCycles; i++) { // for the calculated length of time... 335 digitalWrite(targetPin, HIGH); // write the buzzer pin high to push out the diaphram 336 delayMicroseconds(delayValue); // wait for the calculated delay value 337 digitalWrite(targetPin, LOW); // write the buzzer pin low to pull back the diaphram 338 delayMicroseconds(delayValue); // wait again or the calculated delay value 339 } 340} 341 342void playOdeToJoy() { 343 int length = sizeof(notesOde) / sizeof(notesOde[0]); 344 long wholenote = (60000L * 4) / tempoOde; // whole note duration in ms 345 346 for (int i = 0; i < length; i++) { 347 int randomLight1 = random(A0, A5); 348 int randomLight2 = random(A0, A5); 349 int randomLight3 = random(A0, A5); 350 int randomLight4 = random(A0, A5); 351 int randomLight5 = random(A0, A5); 352 353 analogWrite(randomLight1, 255); 354 analogWrite(randomLight2, 255); 355 analogWrite(randomLight3, 255); 356 analogWrite(randomLight4, 0); 357 analogWrite(randomLight5, 0); 358 359 int divider = durationsOde[i]; 360 long noteDuration; 361 362 if (divider > 0) { 363 // regular note 364 noteDuration = wholenote / divider; 365 } else { 366 // dotted note: negative value means dotted 367 divider = -divider; 368 noteDuration = wholenote / divider; 369 noteDuration = noteDuration * 3 / 2; // x1.5 370 } 371 372 tone(SPEAKER, notesOde[i], noteDuration * 9 / 10); 373 delay(noteDuration); 374 noTone(SPEAKER); 375 } 376}
Downloadable files
Arduino 3-Song Music Box with LCD and LED Light Show
Tutorial
3 Songs Documentation.md
Comments
Only logged in users can leave comments