Components and supplies
Arduino Mega 2560
Solderless Breadboard Full Size
20x4 LCD Display Module Shield
Jumper wires (generic)
Male/Female Jumper Wires
Pushbutton Switch, Pushbutton
Buzzer, Piezo
USB Data Sync Cable
Project description
Code
Arduino PIano
c_cpp
Defines + Setup + Interrupts + Main Loop + Voids + For Loops
1#include "pitches.h" 2#include <LiquidCrystal_I2C.h> 3#include <Wire.h> 4#define buttonC 22 5#define buttonCS 24 6#define buttonD 26 7#define buttonDS 28 8#define buttonE 30 9#define buttonF 32 10#define buttonFS 34 11#define buttonG 36 12#define buttonGS 38 13#define buttonA 40 14#define buttonAS 42 15#define buttonB 44 16#define buttonC2 46 17 18bool screen = true; 19bool halt = false; 20bool pressed = false; 21const int resetbutton = 2; 22const int countbutton = 3; 23 24const int buzzer = 11; 25volatile int count = 0; 26 27int fameBPM = 168; 28int preludeBPM = 150; 29int loveBPM = 160; 30 31LiquidCrystal_I2C lcd(0x27, 20, 4); 32 33int flamingo[] = 34{ 35 //1ST loop 36 NOTE_GS4, NOTE_AS4, NOTE_C5, NOTE_DS5, 37 NOTE_F5, NOTE_F5, NOTE_G5, NOTE_F5, 38 NOTE_DS5, NOTE_C5, NOTE_F5, NOTE_DS5, 39 40 NOTE_G4, 41 NOTE_GS4, NOTE_AS4, NOTE_C5, NOTE_DS5, 42 NOTE_F5, NOTE_F5, NOTE_G5, NOTE_F5, 43 NOTE_DS5, NOTE_D5, NOTE_AS4, 44 45 NOTE_G4, NOTE_C5, NOTE_G4, NOTE_C5, NOTE_AS4, 46 47 NOTE_G4, NOTE_FS4, 48 NOTE_F4, NOTE_GS4, NOTE_DS5, 49 NOTE_F4, NOTE_GS4, NOTE_DS5, 50 NOTE_F4, NOTE_DS5, NOTE_DS5, 51 NOTE_F4, NOTE_DS5, NOTE_F4, 52 NOTE_G4, NOTE_DS5, NOTE_G4, NOTE_AS4, 53 54 //2ND LOOP 55 NOTE_GS4, NOTE_AS4, NOTE_C5, NOTE_DS5, 56 NOTE_F5, NOTE_F5, NOTE_G5, NOTE_F5, 57 NOTE_DS5, NOTE_C5, NOTE_F5, NOTE_DS5, 58 59 NOTE_G4, 60 NOTE_GS4, NOTE_AS4, NOTE_C5, NOTE_DS5, 61 NOTE_F5, NOTE_F5, NOTE_G5, NOTE_F5, 62 NOTE_DS5, NOTE_D5, NOTE_AS4, 63 64 NOTE_G4, NOTE_C5, NOTE_G4, NOTE_C5, NOTE_AS4, 65 66 NOTE_G4, NOTE_FS4, 67 NOTE_F4, NOTE_GS4, NOTE_DS5, 68 NOTE_F4, 69 NOTE_C5, NOTE_D5, NOTE_DS5, 70 0, 0 71}; 72 73//notes in milliseconds 74int tempo[] 75{ 76 //1st measure 77 169, 169, 169, 337, 78 337, 84, 84, 79 80 //2nd 81 337, 337, 337, 337, 82 83 //3rd 84 505, 169, 169, 169, 169, 337, 85 //4 86 337, 84, 84, 337, 337, 87 //5 88 674, 505, 337, 89 90 169, 169, 169, 505, 84, 84, 91 92 169, 169, 169, 169, 169, 169, 169, 169, 93 169, 169, 169, 169, 169, 169, 169, 169, 94 95 //2nd loop 9th measure 96 169, 169, 169, 337, 97 337, 84, 84, 98 99 337, 337, 337, 337, 100 101 505, 169, 169, 169, 169, 337, 102 103 337, 84, 84, 337, 337, 104 105 647, 505, 337, 106 107 169, 169, 169, 505, 84, 84, 108 109 169, 169, 169, 169, 337, 337, 110 111 674, 337, 337 112}; 113 114int fame[] 115{ 116 NOTE_AS4, NOTE_A4, NOTE_AS4, NOTE_G4, 117 NOTE_AS4, NOTE_D4, NOTE_AS4, NOTE_G4, 118 NOTE_AS4, NOTE_DS4, NOTE_DS5, NOTE_G4, 119 NOTE_AS4, NOTE_DS4, NOTE_DS5, NOTE_AS4, 120 121 NOTE_D5, NOTE_AS4, NOTE_DS5, NOTE_AS4, 122 NOTE_F5, NOTE_AS4, NOTE_B4, NOTE_A4, 123 NOTE_B4, NOTE_F4, NOTE_F5, NOTE_F4, 124 NOTE_C5, NOTE_F4, NOTE_F5, NOTE_A4, 125 126 NOTE_AS4, NOTE_A4, NOTE_AS4, NOTE_G4, 127 NOTE_AS4, NOTE_D4, NOTE_AS4, NOTE_G4, 128 NOTE_AS4, NOTE_DS4, NOTE_DS5, NOTE_G4, 129 NOTE_AS4, NOTE_DS4, NOTE_DS5, NOTE_AS4, 130 131 NOTE_D5, NOTE_AS4, NOTE_DS5, NOTE_AS4, 132 NOTE_F5, NOTE_AS4, NOTE_C5, NOTE_A4, 133 NOTE_C5, NOTE_F4, NOTE_F5, NOTE_F4, 134 NOTE_C5, NOTE_F4, NOTE_F5, NOTE_A4 135}; 136 137int fametempo[] 138{ 139 16, 16, 16, 16, 140 16, 16, 16, 16, 141 16, 16, 16, 16, 142 16, 16, 16, 16, 143 144 16, 16, 16, 16, 145 16, 16, 16, 16, 146 16, 16, 16, 16, 147 16, 16, 16, 16, 148 149 16, 16, 16, 16, 150 16, 16, 16, 16, 151 16, 16, 16, 16, 152 16, 16, 16, 16, 153 154 16, 16, 16, 16, 155 16, 16, 16, 16, 156 16, 16, 16, 16, 157 16, 16, 16, 16 158}; 159 160int prelude[] 161{ 162 NOTE_C3, NOTE_D3, NOTE_E3, NOTE_G3, 163 NOTE_C4, NOTE_D4, NOTE_E4, NOTE_G4, 164 NOTE_C5, NOTE_D5, NOTE_E5, NOTE_G5, 165 NOTE_C6, NOTE_D6, NOTE_E6, NOTE_G6, 166 NOTE_C7, 167 168 NOTE_G6, NOTE_E6, NOTE_D6, NOTE_C6, 169 NOTE_G5, NOTE_E5, NOTE_D5, NOTE_C5, 170 NOTE_G4, NOTE_E4, NOTE_D4, NOTE_C4, 171 NOTE_G3, NOTE_E3, NOTE_D3, 172 173 NOTE_A2, NOTE_B2, NOTE_C2, NOTE_E2, 174 NOTE_A3, NOTE_B3, NOTE_C3, NOTE_E3, 175 NOTE_A4, NOTE_B4, NOTE_C4, NOTE_E4, 176 NOTE_A5, NOTE_B5, NOTE_C5, NOTE_E5, 177 NOTE_A6, 178 179 NOTE_E5, NOTE_C5, NOTE_B5, NOTE_A5, 180 NOTE_E4, NOTE_C4, NOTE_B4, NOTE_A4, 181 NOTE_E3, NOTE_C3, NOTE_B3, NOTE_A3, 182 NOTE_E2, NOTE_C2, NOTE_B2, 183//2ND LOOP 184 NOTE_C3, NOTE_D3, NOTE_E3, NOTE_G3, 185 NOTE_C4, NOTE_D4, NOTE_E4, NOTE_G4, 186 NOTE_C5, NOTE_D5, NOTE_E5, NOTE_G5, 187 NOTE_C6, NOTE_D6, NOTE_E6, NOTE_G6, 188 NOTE_C7, 189 190 NOTE_G6, NOTE_E6, NOTE_D6, NOTE_C6, 191 NOTE_G5, NOTE_E5, NOTE_D5, NOTE_C5, 192 NOTE_G4, NOTE_E4, NOTE_D4, NOTE_C4, 193 NOTE_G3, NOTE_E3, NOTE_D3, 194 195 NOTE_A2, NOTE_B2, NOTE_C2, NOTE_E2, 196 NOTE_A3, NOTE_B3, NOTE_C3, NOTE_E3, 197 NOTE_A4, NOTE_B4, NOTE_C4, NOTE_E4, 198 NOTE_A5, NOTE_B5, NOTE_C5, NOTE_E5, 199 NOTE_A6, 200 201 NOTE_E6, NOTE_C6, NOTE_B6, NOTE_A6, 202 NOTE_E5, NOTE_C5, NOTE_B5, NOTE_A5, 203 NOTE_E4, NOTE_C4, NOTE_B4, NOTE_A4, 204 NOTE_E3, NOTE_C3, NOTE_B2, NOTE_A2, 205//3rd - new section 206 NOTE_C3, NOTE_F3, NOTE_G3, NOTE_A3, 207 NOTE_C4, NOTE_F4, NOTE_G4, NOTE_A4, 208 NOTE_C5, NOTE_F5, NOTE_G5, NOTE_A5, 209 NOTE_C6, NOTE_F6, NOTE_G6, NOTE_A6, 210 211 NOTE_G6, NOTE_F6, NOTE_C6, NOTE_A5, 212 NOTE_G5, NOTE_F5, NOTE_C5, NOTE_A4, 213 NOTE_G4, NOTE_F4, NOTE_C4, NOTE_A3, 214 NOTE_G3, NOTE_F3, NOTE_C3, NOTE_B2, 215 216 NOTE_D3, NOTE_G3, NOTE_A3, NOTE_B3, 217 NOTE_D4, NOTE_G4, NOTE_A4, NOTE_B4, 218 NOTE_D5, NOTE_G5, NOTE_A5, NOTE_B5, 219 NOTE_D6, NOTE_G6, NOTE_A6, NOTE_B6, 220 221 NOTE_A6, NOTE_G6, NOTE_D6, NOTE_B5, 222 NOTE_A5, NOTE_G5, NOTE_D5, NOTE_B4, 223 NOTE_A4, NOTE_G4, NOTE_D4, NOTE_B3, 224 NOTE_A3, NOTE_G3, NOTE_D3, 225//13 226 NOTE_GS2, NOTE_C3, NOTE_DS3, NOTE_G3, 227 NOTE_GS3, NOTE_C4, NOTE_DS4, NOTE_G4, 228 NOTE_GS4, NOTE_C5, NOTE_DS5, NOTE_G5, 229 NOTE_GS5, NOTE_C6, NOTE_DS6, NOTE_G6, 230 231 NOTE_DS6, NOTE_C6, NOTE_GS5, NOTE_G5, 232 NOTE_DS5, NOTE_C5, NOTE_GS4, NOTE_G4, 233 NOTE_DS4, NOTE_C4, NOTE_GS3, NOTE_G3, 234 NOTE_DS3, NOTE_C3, NOTE_GS2, 235 236 NOTE_D3, NOTE_F3, NOTE_A3, NOTE_AS3, 237 NOTE_D4, NOTE_F4, NOTE_A4, NOTE_AS4, 238 NOTE_D5, NOTE_F5, NOTE_A5, NOTE_AS5, 239 NOTE_D6, NOTE_F6, NOTE_A6, NOTE_AS6, 240 241 NOTE_A6, NOTE_F6, NOTE_D6, NOTE_AS5, 242 NOTE_A5, NOTE_F5, NOTE_D5, NOTE_AS4, 243 NOTE_A4, NOTE_F4, NOTE_D4, NOTE_AS3, 244 NOTE_A3, NOTE_F3, NOTE_D3, NOTE_AS2 245}; 246 247int preludetempo[] 248{ 249 16,16,16,16, 250 16,16,16,16, 251 16,16,16,16, 252 16,16,16,16, 253 254 16,16,16,16, 255 16,16,16,16, 256 16,16,16,16, 257 16,16,16,16, 258 259 16,16,16,16, 260 16,16,16,16, 261 16,16,16,16, 262 16,16,16,16, 263 264 16,16,16,16, 265 16,16,16,16, 266 16,16,16,16, 267 16,16,16,16, 268 269 16,16,16,16, 270 16,16,16,16, 271 16,16,16,16, 272 16,16,16,16, 273 274 16,16,16,16, 275 16,16,16,16, 276 16,16,16,16, 277 16,16,16,16, 278 279 16,16,16,16, 280 16,16,16,16, 281 16,16,16,16, 282 16,16,16,16, 283 284 16,16,16,16, 285 16,16,16,16, 286 16,16,16,16, 287 16,16,16,16, 288 289 16,16,16,16, 290 16,16,16,16, 291 16,16,16,16, 292 16,16,16,16, 293 294 16,16,16,16, 295 16,16,16,16, 296 16,16,16,16, 297 16,16,16,16, 298 299 16,16,16,16, 300 16,16,16,16, 301 16,16,16,16, 302 16,16,16,16, 303 304 16,16,16,16, 305 16,16,16,16, 306 16,16,16,16, 307 16,16,16,16, 308 309 16,16,16,16, 310 16,16,16,16, 311 16,16,16,16, 312 16,16,16,16, 313 314 16,16,16,16, 315 16,16,16,16, 316 16,16,16,16, 317 16,16,16,16, 318 319 16,16,16,16, 320 16,16,16,16, 321 16,16,16,16, 322 16,16,16,16, 323 324 16,16,16,16, 325 16,16,16,16, 326 16,16,16,16, 327 16,16,16 328}; 329 330int love[] 331{ 332 NOTE_G5, NOTE_FS5, NOTE_E5, NOTE_C5, 333 NOTE_G5, NOTE_FS5, NOTE_E5, NOTE_C5, 334 NOTE_G5, NOTE_FS5, NOTE_D5, NOTE_B4, 335 NOTE_G5, NOTE_FS5, NOTE_D5, NOTE_B4, 336 337 NOTE_G5, NOTE_A5, NOTE_G5, NOTE_A5, 338 NOTE_FS5, NOTE_A5, NOTE_G5, NOTE_A5, 339 NOTE_FS5, NOTE_A5, NOTE_FS5, NOTE_C6, 340 NOTE_C6, NOTE_C6, NOTE_B5, NOTE_B5 341}; 342 343int lovetempo [] 344{ 345 16,16,16,16, 346 16,16,16,16, 347 16,16,16,16, 348 16,16,16,16, 349 350 16,16,16,16, 351 16,16,16,16, 352 16,16,16,16, 353 16,16,16,16, 354}; 355 356void setup() 357{ 358 Serial.begin(9600); 359 Serial.print("count = "); 360 Serial.println(count); 361 pinMode(buttonC , INPUT_PULLUP); 362 pinMode(buttonCS , INPUT_PULLUP); 363 pinMode(buttonD , INPUT_PULLUP); 364 pinMode(buttonDS , INPUT_PULLUP); 365 pinMode(buttonE , INPUT_PULLUP); 366 pinMode(buttonF , INPUT_PULLUP); 367 pinMode(buttonFS , INPUT_PULLUP); 368 pinMode(buttonG , INPUT_PULLUP); 369 pinMode(buttonGS , INPUT_PULLUP); 370 pinMode(buttonA , INPUT_PULLUP); 371 pinMode(buttonAS , INPUT_PULLUP); 372 pinMode(buttonB , INPUT_PULLUP); 373 pinMode(buttonC2 , INPUT_PULLUP); 374 pinMode(resetbutton , INPUT_PULLUP); 375 pinMode(countbutton , INPUT_PULLUP); 376 attachInterrupt(digitalPinToInterrupt(3), plus, LOW); 377 attachInterrupt(digitalPinToInterrupt(2), reset, LOW); 378 lcd.init(); 379 lcd.backlight(); 380 lcd.begin(20,4); 381 lcd.clear(); 382 lcd.setCursor(0,0); 383} 384 385void loop() 386{ 387 if (count == 1) 388 { 389 lcd.clear(); 390 lcd.setCursor(0, 0); 391 lcd.print("Flamingo"); 392 lcd.setCursor(0, 1); 393 lcd.print("By: Kero Kero Bonito"); 394 flamingosong(); 395 } 396 397 if (count == 2) 398 { 399 lcd.clear(); 400 lcd.setCursor(0, 0); 401 lcd.print("Hall of Fame"); 402 lcd.setCursor(0, 1); 403 lcd.print("By: The Script"); 404 famesong(); 405 } 406 407 if (count == 3) 408 { 409 lcd.clear(); 410 lcd.setCursor(0, 0); 411 lcd.print("Prelude"); 412 lcd.setCursor(0, 1); 413 lcd.print("From: FF7"); 414 preludesong(); 415 } 416 417 if (count == 4) 418 { 419 lcd.clear(); 420 lcd.setCursor(0, 0); 421 lcd.print("Best Love Song"); 422 lcd.setCursor(0, 1); 423 lcd.print("By: T-Pain"); 424 lovesong(); 425 } 426 notes(); 427} 428 429void plus() 430{ 431 int reading = digitalRead(countbutton); 432 433 if (reading == LOW && !pressed) // if button is pressed and was not pressed before 434 { 435 Serial.println(reading); 436 count++; 437 halt = true; 438 Serial.print("count = "); 439 Serial.println(count); 440 441 pressed = true; 442 } 443 else if (reading == HIGH) { 444 pressed = false; 445 } 446} 447 448void reset() 449{ 450 count = 0; 451 halt = true; 452 Serial.print("count = "); 453 Serial.println(count); 454} 455 456void notes() 457{ 458 while (digitalRead(buttonC) == LOW) 459 { 460 tone(buzzer, NOTE_C4); 461 lcd.setCursor ( 0, 0 ); 462 lcd.print("C - 262 Hz"); 463 } 464 while (digitalRead(buttonCS) == LOW) 465 { 466 tone(buzzer, NOTE_CS4); 467 lcd.setCursor ( 0, 0 ); 468 lcd.print("C# - 277 Hz"); 469 } 470 while (digitalRead(buttonD) == LOW) 471 { 472 tone(buzzer, NOTE_D4); 473 lcd.setCursor ( 0, 0 ); 474 lcd.print("D - 294 Hz"); 475 } 476 while (digitalRead(buttonDS) == LOW) 477 { 478 tone(buzzer, NOTE_DS4); 479 lcd.setCursor ( 0, 0 ); 480 lcd.print("D# - 311 Hz"); 481 } 482 while (digitalRead(buttonE) == LOW) 483 { 484 tone(buzzer, NOTE_E4); 485 lcd.setCursor ( 0, 0 ); 486 lcd.print("E - 330 Hz"); 487 } 488 while (digitalRead(buttonF) == LOW) 489 { 490 tone(buzzer, NOTE_F4); 491 lcd.setCursor ( 0, 0 ); 492 lcd.print("F - 349 Hz"); 493 } 494 while (digitalRead(buttonFS) == LOW) 495 { 496 tone(buzzer, NOTE_FS4); 497 lcd.setCursor ( 0, 0 ); 498 lcd.print("F# - 370 Hz"); 499 } 500 while (digitalRead(buttonG) == LOW) 501 { 502 tone(buzzer, NOTE_G4); 503 lcd.setCursor ( 0, 0 ); 504 lcd.print("G - 392 Hz"); 505 } 506 while (digitalRead(buttonGS) == LOW) 507 { 508 tone(buzzer, NOTE_GS4); 509 lcd.setCursor ( 0, 0 ); 510 lcd.print("G# - 415 Hz"); 511 } 512 while (digitalRead(buttonA) == LOW) 513 { 514 tone(buzzer, NOTE_A4); 515 lcd.setCursor ( 0, 0 ); 516 lcd.print("A - 440 Hz"); 517 } 518 while (digitalRead(buttonAS) == LOW) 519 { 520 tone(buzzer, NOTE_AS4); 521 lcd.setCursor ( 0, 0 ); 522 lcd.print("A# - 466 Hz"); 523 } 524 while (digitalRead(buttonB) == LOW) 525 { 526 tone(buzzer, NOTE_B4); 527 lcd.setCursor ( 0, 0 ); 528 lcd.print("B - 494 Hz"); 529 } 530 while (digitalRead(buttonC2) == LOW) 531 { 532 tone(buzzer, NOTE_C5); 533 lcd.setCursor ( 0, 0 ); 534 lcd.print("C - 523 Hz"); 535 } 536 lcd.clear(); 537 noTone(buzzer); 538} 539 540void flamingosong() 541{ 542 int size = sizeof(flamingo) / sizeof(int); 543 for (int thisNote = 0; (thisNote < size && !halt); thisNote++) 544 { 545 int noteDuration = tempo[thisNote]; 546 547 tone(buzzer, flamingo[thisNote], noteDuration); 548 549 int pauseBetweenNotes = noteDuration * 1; 550 delay(pauseBetweenNotes); 551 } 552 halt = false; 553} 554 555void famesong() 556{ 557 int size = sizeof(fame) / sizeof(int); 558 for (int thisNote = 0; (thisNote < size && !halt); thisNote++) 559 { 560 int noteDuration = (60000 / fameBPM) / (32 / fametempo[thisNote]); 561 562 tone(buzzer, fame[thisNote], noteDuration); 563 564 int pauseBetweenNotes = noteDuration * 1; 565 delay(pauseBetweenNotes); 566 } 567 halt = false; 568} 569 570void preludesong() 571{ 572 int size = sizeof(prelude) / sizeof(int); 573 for (int thisNote = 0; (thisNote < size && !halt); thisNote++) 574 { 575 int noteDuration = (60000 / preludeBPM) / (32 / preludetempo[thisNote]); 576 577 tone(buzzer, prelude[thisNote], noteDuration); 578 579 int pauseBetweenNotes = noteDuration * 1; 580 delay(pauseBetweenNotes); 581 } 582 halt = false; 583} 584 585void lovesong() 586{ 587 int size = sizeof(love) / sizeof(int); 588 for (int thisNote = 0; (thisNote < size && !halt); thisNote++) 589 { 590 int noteDuration = (60000 / loveBPM) / (32 / lovetempo[thisNote]); 591 592 tone(buzzer, love[thisNote], noteDuration); 593 594 int pauseBetweenNotes = noteDuration * 1; 595 delay(pauseBetweenNotes); 596 } 597 halt = false; 598}
Downloadable files
Arduino Piano
Use whatever buzzer. My 20x4 LCD display has a adapter on the back. So plug in the 5V, ground, SDA, SCL from LCD to Arduino. Make sure to plug the SDA and SCL into the correct pins on the Arduino.
Arduino Piano
Arduino Piano
Use whatever buzzer. My 20x4 LCD display has a adapter on the back. So plug in the 5V, ground, SDA, SCL from LCD to Arduino. Make sure to plug the SDA and SCL into the correct pins on the Arduino.
Arduino Piano
Comments
Only logged in users can leave comments
nlkong
0 Followers
•1 Projects
12
1
zacktbuilder
4 years ago
can you do it i have a led one????