Components and supplies
Resistor 220 ohm
Resistor 330 ohm
Arduino UNO
LED (generic)
Pushbutton Switch, Push-Pull
Buzzer
Tools and machines
Breadboard, 170 Pin
Project description
Code
Notes file
arduino
the notes list to the buzzer
1/************************************************* 2 * Public Constants 3 *************************************************/ 4 5#define NOTE_B1 31 6#define NOTE_C1 33 7#define NOTE_CS1 35 8#define NOTE_D1 37 9#define NOTE_DS1 39 10#define NOTE_E1 41 11#define NOTE_F1 44 12#define NOTE_FS1 46 13#define NOTE_G1 49 14#define NOTE_GS1 52 15#define NOTE_A2 55 16#define NOTE_AS2 58 17#define NOTE_B2 62 18#define NOTE_C2 65 19#define NOTE_CS2 69 20#define NOTE_D2 73 21#define NOTE_DS2 78 22#define NOTE_E2 82 23#define NOTE_F2 87 24#define NOTE_FS2 93 25#define NOTE_G2 98 26#define NOTE_GS2 104 27#define NOTE_A3 110 28#define NOTE_AS3 117 29#define NOTE_B3 123 30#define NOTE_C3 131 31#define NOTE_CS3 139 32#define NOTE_D3 147 33#define NOTE_DS3 156 34#define NOTE_E3 165 35#define NOTE_F3 175 36#define NOTE_FS3 185 37#define NOTE_G3 196 38#define NOTE_GS3 208 39#define NOTE_A4 220 40#define NOTE_AS4 233 41#define NOTE_B4 247 42#define NOTE_C4 262 43#define NOTE_CS4 277 44#define NOTE_D4 294 45#define NOTE_DS4 311 46#define NOTE_E4 330 47#define NOTE_F4 349 48#define NOTE_FS4 370 49#define NOTE_G4 392 50#define NOTE_GS4 415 51#define NOTE_A5 440 52#define NOTE_AS5 466 53#define NOTE_B5 494 54#define NOTE_C5 523 55#define NOTE_CS5 554 56#define NOTE_D5 587 57#define NOTE_DS5 622 58#define NOTE_E5 659 59#define NOTE_F5 698 60#define NOTE_FS5 740 61#define NOTE_G5 784 62#define NOTE_GS5 831 63#define NOTE_A6 880 64#define NOTE_AS6 932 65#define NOTE_B6 988 66#define NOTE_C6 1047 67#define NOTE_CS6 1109 68#define NOTE_D6 1175 69#define NOTE_DS6 1245 70#define NOTE_E6 1319 71#define NOTE_F6 1397 72#define NOTE_FS6 1480 73#define NOTE_G6 1568 74#define NOTE_GS6 1661 75#define NOTE_A7 1760 76#define NOTE_AS7 1865 77#define NOTE_B7 1976 78#define NOTE_C7 2093 79#define NOTE_CS7 2217 80#define NOTE_D7 2349 81#define NOTE_DS7 2489 82#define NOTE_E7 2637 83#define NOTE_F7 2794 84#define NOTE_FS7 2960 85#define NOTE_G7 3136 86#define NOTE_GS7 3322 87#define NOTE_A8 3520 88#define NOTE_AS8 3729 89#define NOTE_B8 3951 90#define NOTE_C8 4186 91#define NOTE_CS8 4435 92#define NOTE_D8 4699 93#define NOTE_DS8 4978 94
Bounce library
Prevent pushbutton bounce effect
Notes file
arduino
the notes list to the buzzer
1/************************************************* 2 * Public Constants 3 *************************************************/ 4 5#define NOTE_B1 31 6#define NOTE_C1 33 7#define NOTE_CS1 35 8#define NOTE_D1 37 9#define NOTE_DS1 39 10#define NOTE_E1 41 11#define NOTE_F1 44 12#define NOTE_FS1 46 13#define NOTE_G1 49 14#define NOTE_GS1 52 15#define NOTE_A2 55 16#define NOTE_AS2 58 17#define NOTE_B2 62 18#define NOTE_C2 65 19#define NOTE_CS2 69 20#define NOTE_D2 73 21#define NOTE_DS2 78 22#define NOTE_E2 82 23#define NOTE_F2 87 24#define NOTE_FS2 93 25#define NOTE_G2 98 26#define NOTE_GS2 104 27#define NOTE_A3 110 28#define NOTE_AS3 117 29#define NOTE_B3 123 30#define NOTE_C3 131 31#define NOTE_CS3 139 32#define NOTE_D3 147 33#define NOTE_DS3 156 34#define NOTE_E3 165 35#define NOTE_F3 175 36#define NOTE_FS3 185 37#define NOTE_G3 196 38#define NOTE_GS3 208 39#define NOTE_A4 220 40#define NOTE_AS4 233 41#define NOTE_B4 247 42#define NOTE_C4 262 43#define NOTE_CS4 277 44#define NOTE_D4 294 45#define NOTE_DS4 311 46#define NOTE_E4 330 47#define NOTE_F4 349 48#define NOTE_FS4 370 49#define NOTE_G4 392 50#define NOTE_GS4 415 51#define NOTE_A5 440 52#define NOTE_AS5 466 53#define NOTE_B5 494 54#define NOTE_C5 523 55#define NOTE_CS5 554 56#define NOTE_D5 587 57#define NOTE_DS5 622 58#define NOTE_E5 659 59#define NOTE_F5 698 60#define NOTE_FS5 740 61#define NOTE_G5 784 62#define NOTE_GS5 831 63#define NOTE_A6 880 64#define NOTE_AS6 932 65#define NOTE_B6 988 66#define NOTE_C6 1047 67#define NOTE_CS6 1109 68#define NOTE_D6 1175 69#define NOTE_DS6 1245 70#define NOTE_E6 1319 71#define NOTE_F6 1397 72#define NOTE_FS6 1480 73#define NOTE_G6 1568 74#define NOTE_GS6 1661 75#define NOTE_A7 1760 76#define NOTE_AS7 1865 77#define NOTE_B7 1976 78#define NOTE_C7 2093 79#define NOTE_CS7 2217 80#define NOTE_D7 2349 81#define NOTE_DS7 2489 82#define NOTE_E7 2637 83#define NOTE_F7 2794 84#define NOTE_FS7 2960 85#define NOTE_G7 3136 86#define NOTE_GS7 3322 87#define NOTE_A8 3520 88#define NOTE_AS8 3729 89#define NOTE_B8 3951 90#define NOTE_C8 4186 91#define NOTE_CS8 4435 92#define NOTE_D8 4699 93#define NOTE_DS8 4978 94
Bounce library
Prevent pushbutton bounce effect
Genius game
arduino
This is a game. The goal is to repeat an aleatory sequence with 7 levels of difficulty.
1/* 2The goal of this game is to repeat a sequence of lights and notes with 7 levels of difficulty. 3Based on the "Piezo Buzzer Sound Recall Game" example of Lee Assam from Arduino Bootcamp. 4Created: 26/06/2020 5Author: Raul G Roig 6*/ 7 8#include <Bounce2.h> 9#include "pitches.h" 10 11//Define the LEDs pins 12#define LED1 3 13#define LED2 4 14#define LED3 5 15#define LED4 6 16 17// Defining the buttons pins 18#define BUTTON1 7 19#define BUTTON2 8 20#define BUTTON3 9 21#define BUTTON4 10 22 23//Defining the Buzzer pin 24#define BUZZER 11 25 26// Instantiate Bounce objects 27Bounce debouncer1 = Bounce(); 28Bounce debouncer2 = Bounce(); 29Bounce debouncer3 = Bounce(); 30Bounce debouncer4 = Bounce(); 31 32//Change this variable to increase number of notes to play 33//int noteSequence[4]; 34//int numNotes = sizeof(noteSequence)/sizeof(int); 35int noteSequence_1[3]; 36int noteSequence_2[4]; 37int noteSequence_3[5]; 38int noteSequence_4[6]; 39int noteSequence_5[7]; 40int noteSequence_6[8]; 41int noteSequence_7[9]; 42int numNotes = 3; 43int currentNote = 0; 44int lives = 3; 45boolean isPlaying = false; 46 47void setup() { 48 49 // Start serial 50 Serial.begin(9600); 51 52 // Setup the buttons with an internal pull-up 53 pinMode(BUTTON1, INPUT_PULLUP); 54 pinMode(BUTTON2, INPUT_PULLUP); 55 pinMode(BUTTON3, INPUT_PULLUP); 56 pinMode(BUTTON4, INPUT_PULLUP); 57 58 // Setup the Bounce instances 59 debouncer1.attach(BUTTON1); 60 debouncer1.interval(5); // interval in ms 61 debouncer2.attach(BUTTON2); 62 debouncer2.interval(5); // interval in ms 63 debouncer3.attach(BUTTON3); 64 debouncer3.interval(5); // interval in ms 65 debouncer4.attach(BUTTON4); 66 debouncer4.interval(5); // interval in ms 67 68 // wait a little before start 69 delay(2000); 70 // print first sentences 71 Serial.println("O jogo vai comear, boa sorte!"); 72 Serial.print("Fase: "); 73 Serial.println(numNotes-2); 74 Serial.print("Vidas: "); 75 Serial.println(lives); 76 77 // generating a random number 78 randomSeed(analogRead(0)); 79 // get first sequence of notes 80 generateSequence(); 81} 82 83void loop() { 84 85 if (!isPlaying) { 86 isPlaying = true; 87 currentNote = 0; 88 switch(numNotes){ 89 case 3: 90 playSequence(noteSequence_1); 91 break; 92 case 4: 93 playSequence(noteSequence_2); 94 break; 95 case 5: 96 playSequence(noteSequence_3); 97 break; 98 case 6: 99 playSequence(noteSequence_4); 100 break; 101 case 7: 102 playSequence(noteSequence_5); 103 break; 104 case 8: 105 playSequence(noteSequence_6); 106 break; 107 case 9: 108 playSequence(noteSequence_7); 109 break; 110 } 111 112 } 113 114 // Update the Bounce instances 115 debouncer1.update(); 116 debouncer2.update(); 117 debouncer3.update(); 118 debouncer4.update(); 119 120 // Check the pressed buttons 121 if (debouncer1.fell()){ 122 playButtonNote(1); 123 verifyNote(1); 124 } 125 126 if (debouncer2.fell()){ 127 playButtonNote(2); 128 verifyNote(2); 129 } 130 131 if (debouncer3.fell()){ 132 playButtonNote(3); 133 verifyNote(3); 134 } 135 if (debouncer4.fell()){ 136 playButtonNote(4); 137 verifyNote(4); 138 } 139} 140 141void playSequence(int notes[]) { 142 for (int i=0; i< numNotes; i++) { 143 playNote(notes[i]); 144 } 145} 146 147void verifyNote(int note) { 148 int noteInSequence; 149 switch(numNotes){ 150 case 3: 151 noteInSequence = noteSequence_1[currentNote]; 152 break; 153 case 4: 154 noteInSequence = noteSequence_2[currentNote]; 155 break; 156 case 5: 157 noteInSequence = noteSequence_3[currentNote]; 158 break; 159 case 6: 160 noteInSequence = noteSequence_4[currentNote]; 161 break; 162 case 7: 163 noteInSequence = noteSequence_5[currentNote]; 164 break; 165 case 8: 166 noteInSequence = noteSequence_6[currentNote]; 167 break; 168 case 9: 169 noteInSequence = noteSequence_7[currentNote]; 170 break; 171 } 172 if (noteInSequence == note) { //Note is correct 173 currentNote++; // Go to the next note 174 if (currentNote == numNotes && numNotes == 9) { // Finish all phases 175 Serial.println("Voc venceu, parabns!"); 176 final_win(); 177 } else if (currentNote == numNotes){ // Finish phase 178 Serial.println("Muito bem, voc acertou!"); 179 win(); 180 Serial.print("Fase: "); 181 Serial.println(numNotes-2); 182 Serial.print("Vidas: "); 183 Serial.println(lives); 184 } 185 } else if (lives == 1){ // Note is wrong 186 Serial.println("Que pena, voc perdeu..."); 187 final_lose(); 188 } else { 189 Serial.println("Ops, voc errou."); 190 lose(); 191 Serial.print("Fase: "); 192 Serial.println(numNotes-2); 193 Serial.print("Vidas: "); 194 Serial.println(lives); 195 } 196} 197 198void generateSequence() { 199 Serial.print("Sequncia:"); 200 for (int i = 0; i < numNotes; i ++ ) { 201 int num = random(1, 5); 202 Serial.print(num); 203 Serial.print(", "); 204 switch(numNotes){ 205 case 3: 206 noteSequence_1[i] = num; 207 break; 208 case 4: 209 noteSequence_2[i] = num; 210 break; 211 case 5: 212 noteSequence_3[i] = num; 213 break; 214 case 6: 215 noteSequence_4[i] = num; 216 break; 217 case 7: 218 noteSequence_5[i] = num; 219 break; 220 case 8: 221 noteSequence_6[i] = num; 222 break; 223 case 9: 224 noteSequence_7[i] = num; 225 break; 226 } 227 } 228 Serial.println(); 229} 230 231void playNote(int note) { 232 int noteDuration; 233 int intervalDuration; 234 switch (numNotes) { 235 case 3: 236 noteDuration = 1500; 237 intervalDuration = 700; 238 break; 239 case 4: 240 noteDuration = 1300; 241 intervalDuration = 625; 242 break; 243 case 5: 244 noteDuration = 1100; 245 intervalDuration = 550; 246 break; 247 case 6: 248 noteDuration = 900; 249 intervalDuration = 475; 250 break; 251 case 7: 252 noteDuration = 700; 253 intervalDuration = 400; 254 break; 255 case 8: 256 noteDuration = 550; 257 intervalDuration = 325; 258 break; 259 case 9: 260 noteDuration = 400; 261 intervalDuration = 250; 262 break; 263 } 264 switch (note) { 265 case 1: 266 tone(BUZZER, NOTE_C3); 267 digitalWrite(LED1, HIGH); 268 delay(noteDuration); 269 noTone(BUZZER); 270 digitalWrite(LED1, LOW); 271 delay (intervalDuration); 272 break; 273 case 2: 274 tone(BUZZER, NOTE_G3); 275 digitalWrite(LED2, HIGH); 276 delay(noteDuration); 277 noTone(BUZZER); 278 digitalWrite(LED2, LOW); 279 delay (intervalDuration); 280 break; 281 case 3: 282 tone(BUZZER, NOTE_D4); 283 digitalWrite(LED3, HIGH); 284 delay(noteDuration); 285 noTone(BUZZER); 286 digitalWrite(LED3, LOW); 287 delay (intervalDuration); 288 break; 289 case 4: 290 tone(BUZZER, NOTE_A4); 291 digitalWrite(LED4, HIGH); 292 delay(noteDuration); 293 noTone(BUZZER); 294 digitalWrite(LED4, LOW); 295 delay (intervalDuration); 296 break; 297 } 298} 299 300void playButtonNote(int note) { 301 switch (note) { 302 case 1: 303 tone(BUZZER, NOTE_C3); 304 digitalWrite(LED1, HIGH); 305 delay(500); 306 noTone(BUZZER); 307 digitalWrite(LED1, LOW); 308 break; 309 case 2: 310 tone(BUZZER, NOTE_G3); 311 digitalWrite(LED2, HIGH); 312 delay(500); 313 noTone(BUZZER); 314 digitalWrite(LED2, LOW); 315 break; 316 case 3: 317 tone(BUZZER, NOTE_D4); 318 digitalWrite(LED3, HIGH); 319 delay(500); 320 noTone(BUZZER); 321 digitalWrite(LED3, LOW); 322 break; 323 case 4: 324 tone(BUZZER, NOTE_A4); 325 digitalWrite(LED4, HIGH); 326 delay(500); 327 noTone(BUZZER); 328 digitalWrite(LED4, LOW); 329 break; 330 } 331} 332 333void win() { 334 currentNote = 0; 335 isPlaying = false; 336 numNotes++; 337 generateSequence(); 338 delay(500); 339 340 // Classic melody used in the Arduino tone example 341 // notes in the melody: 342 int melody[] = { 343 NOTE_C4, NOTE_G3, NOTE_G3, NOTE_A3, NOTE_G3, 0, NOTE_B3, NOTE_C4 344 }; 345 346 // note durations: 4 = quarter note, 8 = eighth note, etc.: 347 int noteDurations[] = { 348 4, 8, 8, 4, 4, 4, 4, 4 349 }; 350 351 // iterate over the notes of the melody: 352 for (int thisNote = 0; thisNote < 8; thisNote++) { 353 354 // to calculate the note duration, take one second divided by the note type. 355 //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc. 356 int noteDuration = 1000 / noteDurations[thisNote]; 357 tone(BUZZER, melody[thisNote], noteDuration); 358 switch(thisNote){ 359 case 0: 360 digitalWrite(LED1, HIGH); 361 break; 362 case 1: 363 digitalWrite(LED2, HIGH); 364 break; 365 case 2: 366 digitalWrite(LED3, HIGH); 367 break; 368 case 3: 369 digitalWrite(LED4, HIGH); 370 break; 371 case 4: 372 digitalWrite(LED1, LOW); 373 break; 374 case 5: 375 digitalWrite(LED2, LOW); 376 break; 377 case 6: 378 digitalWrite(LED3, LOW); 379 break; 380 case 7: 381 digitalWrite(LED4, LOW); 382 break; 383 } 384 385 // to distinguish the notes, set a minimum time between them. 386 // the note's duration + 30% seems to work well: 387 int pauseBetweenNotes = noteDuration * 1.30; 388 delay(pauseBetweenNotes); 389 // stop the tone playing: 390 noTone(BUZZER); 391 } 392 delay(1500); 393} 394 395void lose() { 396 currentNote = 0; 397 lives--; 398 isPlaying = false; 399 generateSequence(); 400 delay(500); 401 402 // notes in the melody: 403 int melody[] = { 404 NOTE_C3, NOTE_C2, NOTE_C1 405 }; 406 407 // note durations: 408 int noteDurations[] = { 409 400, 500, 800 410 }; 411 412 // iterate over the notes of the melody: 413 for (int thisNote = 0; thisNote < 3; thisNote++) { 414 415 // note duration directly 416 tone(BUZZER, melody[thisNote]); 417 digitalWrite(LED1, HIGH); 418 digitalWrite(LED2, HIGH); 419 digitalWrite(LED3, HIGH); 420 digitalWrite(LED4, HIGH); 421 delay(noteDurations[thisNote]); 422 423 // stop the tone playing: 424 noTone(BUZZER); 425 digitalWrite(LED1, LOW); 426 digitalWrite(LED2, LOW); 427 digitalWrite(LED3, LOW); 428 digitalWrite(LED4, LOW); 429 430 // to distinguish the notes, set a minimum time between them. 431 delay(200); 432 } 433 delay(1500); 434} 435 436void final_win(){ 437 currentNote = 0; 438 isPlaying = false; 439 numNotes = 3; 440 lives = 3; 441 generateSequence(); 442 delay(500); 443 444 // Melody of "We are the champions" by Queen 445 // notes in the melody: 446 447 int melody[] = { 448 NOTE_F3, NOTE_DS3, NOTE_F3, NOTE_DS3, NOTE_C3, NOTE_GS2, NOTE_D3, 0, 449 NOTE_F3, NOTE_G3, NOTE_A4, NOTE_C4, NOTE_A4, NOTE_D3, NOTE_E3, NOTE_D3, 0, 450 NOTE_D3, NOTE_E3, NOTE_D3, NOTE_E3, NOTE_AS3, 451 NOTE_AS4, NOTE_A4, NOTE_AS4, NOTE_A4, NOTE_G3, 452 NOTE_A4, NOTE_F3, NOTE_AS4, NOTE_A4, NOTE_F3, NOTE_AS4, 453 NOTE_GS3, NOTE_F3, NOTE_AS4, NOTE_GS3, NOTE_F3, 0, 454 NOTE_DS3, NOTE_C3, NOTE_F3 455 }; 456 457 // note durations: 458 int noteDurations[] = { 459 1000, 250, 250, 500, 750, 250, 1000, 1500, 460 1000, 250, 250, 500, 500, 250, 250, 1500, 1500, 461 750, 500, 250, 750, 750, 462 750, 500, 250, 750, 750, 463 750, 500, 250, 750, 500, 250, 464 750, 500, 250, 750, 750, 750, 465 250, 250, 3000 466 }; 467 468 // iterate over the notes of the melody: 469 for (int thisNote = 0; thisNote < 42; thisNote++) { 470 if(melody[thisNote] == 0){ 471 noTone(BUZZER); 472 } else { 473 tone(BUZZER, melody[thisNote]); 474 } 475 digitalWrite(LED1, HIGH); 476 digitalWrite(LED2, HIGH); 477 digitalWrite(LED3, HIGH); 478 digitalWrite(LED4, HIGH); 479 delay(noteDurations[thisNote]); 480 481 // stop the tone playing: 482 noTone(BUZZER); 483 digitalWrite(LED1, LOW); 484 digitalWrite(LED2, LOW); 485 digitalWrite(LED3, LOW); 486 digitalWrite(LED4, LOW); 487 // to distinguish the notes, set a minimum time between them. 488 delay(100); 489 } 490 delay(1500); 491} 492 493void final_lose(){ 494 currentNote = 0; 495 numNotes = 3; 496 lives = 3; 497 isPlaying = false; 498 generateSequence(); 499 delay(500); 500 501 // Melody of "Sonata n2, op 35 (Marche Funebre)" by Chopin 502 // notes in the melody: 503 int melody[] = { 504 NOTE_D2, NOTE_D2, NOTE_D2, NOTE_D2, NOTE_F2, NOTE_E2, NOTE_E2, NOTE_D2, NOTE_D2, NOTE_D2, NOTE_D2 505 }; 506 507 // note durations: 508 int noteDurations[] = { 509 1000, 750, 250, 1000, 750, 250, 750, 250, 750, 250, 1000 510 }; 511 512 // iterate over the notes of the melody: 513 for (int thisNote = 0; thisNote < 11; thisNote++) { 514 tone(BUZZER, melody[thisNote]); 515 digitalWrite(LED1, HIGH); 516 digitalWrite(LED2, HIGH); 517 digitalWrite(LED3, HIGH); 518 digitalWrite(LED4, HIGH); 519 delay(noteDurations[thisNote]); 520 521 // stop the tone playing: 522 noTone(BUZZER); 523 digitalWrite(LED1, LOW); 524 digitalWrite(LED2, LOW); 525 digitalWrite(LED3, LOW); 526 digitalWrite(LED4, LOW); 527 // to distinguish the notes, set a minimum time between them. 528 delay(100); 529 } 530 delay(1500); 531} 532
Downloadable files
tinkercad assembly
the assembly of the circuit with all the parts needed
tinkercad assembly
tinkercad assembly
the assembly of the circuit with all the parts needed
tinkercad assembly
Comments
Only logged in users can leave comments