Components and supplies
Buzzer
Breadboard (generic)
Pushbutton switch 12mm
LED (generic)
Resistor 10k ohm
Apps and platforms
Arduino IDE
Project description
Code
Super Mario theme with button code
arduino
The code I modified from PrinceTronics.
1/* 2 Arduino Mario Bros Tunes 3 With Piezo Buzzer and PWM 4 5 Connect the positive side of the Buzzer to pin 3, 6 then the negative side to a 1k ohm resistor. Connect 7 the other side of the 1 k ohm resistor to 8 ground(GND) pin on the Arduino. 9 10 by: Dipto Pratyaksa 11 last updated: 31/3/13 12*/ 13 14/************************************************* 15 Public Constants 16 *************************************************/ 17 18#define NOTE_B0 31 19#define NOTE_C1 33 20#define NOTE_CS1 35 21#define NOTE_D1 37 22#define NOTE_DS1 39 23#define NOTE_E1 41 24#define NOTE_F1 44 25#define NOTE_FS1 46 26#define NOTE_G1 49 27#define NOTE_GS1 52 28#define NOTE_A1 55 29#define NOTE_AS1 58 30#define NOTE_B1 62 31#define NOTE_C2 65 32#define NOTE_CS2 69 33#define NOTE_D2 73 34#define NOTE_DS2 78 35#define NOTE_E2 82 36#define NOTE_F2 87 37#define NOTE_FS2 93 38#define NOTE_G2 98 39#define NOTE_GS2 104 40#define NOTE_A2 110 41#define NOTE_AS2 117 42#define NOTE_B2 123 43#define NOTE_C3 131 44#define NOTE_CS3 139 45#define NOTE_D3 147 46#define NOTE_DS3 156 47#define NOTE_E3 165 48#define NOTE_F3 175 49#define NOTE_FS3 185 50#define NOTE_G3 196 51#define NOTE_GS3 208 52#define NOTE_A3 220 53#define NOTE_AS3 233 54#define NOTE_B3 247 55#define NOTE_C4 262 56#define NOTE_CS4 277 57#define NOTE_D4 294 58#define NOTE_DS4 311 59#define NOTE_E4 330 60#define NOTE_F4 349 61#define NOTE_FS4 370 62#define NOTE_G4 392 63#define NOTE_GS4 415 64#define NOTE_A4 440 65#define NOTE_AS4 466 66#define NOTE_B4 494 67#define NOTE_C5 523 68#define NOTE_CS5 554 69#define NOTE_D5 587 70#define NOTE_DS5 622 71#define NOTE_E5 659 72#define NOTE_F5 698 73#define NOTE_FS5 740 74#define NOTE_G5 784 75#define NOTE_GS5 831 76#define NOTE_A5 880 77#define NOTE_AS5 932 78#define NOTE_B5 988 79#define NOTE_C6 1047 80#define NOTE_CS6 1109 81#define NOTE_D6 1175 82#define NOTE_DS6 1245 83#define NOTE_E6 1319 84#define NOTE_F6 1397 85#define NOTE_FS6 1480 86#define NOTE_G6 1568 87#define NOTE_GS6 1661 88#define NOTE_A6 1760 89#define NOTE_AS6 1865 90#define NOTE_B6 1976 91#define NOTE_C7 2093 92#define NOTE_CS7 2217 93#define NOTE_D7 2349 94#define NOTE_DS7 2489 95#define NOTE_E7 2637 96#define NOTE_F7 2794 97#define NOTE_FS7 2960 98#define NOTE_G7 3136 99#define NOTE_GS7 3322 100#define NOTE_A7 3520 101#define NOTE_AS7 3729 102#define NOTE_B7 3951 103#define NOTE_C8 4186 104#define NOTE_CS8 4435 105#define NOTE_D8 4699 106#define NOTE_DS8 4978 107 108#define melodyPin 49 109//Mario main theme melody 110int melody[] = { 111 NOTE_E7, NOTE_E7, 0, NOTE_E7, 112 0, NOTE_C7, NOTE_E7, 0, 113 NOTE_G7, 0, 0, 0, 114 NOTE_G6, 0, 0, 0, 115 116 NOTE_C7, 0, 0, NOTE_G6, 117 0, 0, NOTE_E6, 0, 118 0, NOTE_A6, 0, NOTE_B6, 119 0, NOTE_AS6, NOTE_A6, 0, 120 121 NOTE_G6, NOTE_E7, NOTE_G7, 122 NOTE_A7, 0, NOTE_F7, NOTE_G7, 123 0, NOTE_E7, 0, NOTE_C7, 124 NOTE_D7, NOTE_B6, 0, 0, 125 126 NOTE_C7, 0, 0, NOTE_G6, 127 0, 0, NOTE_E6, 0, 128 0, NOTE_A6, 0, NOTE_B6, 129 0, NOTE_AS6, NOTE_A6, 0, 130 131 NOTE_G6, NOTE_E7, NOTE_G7, 132 NOTE_A7, 0, NOTE_F7, NOTE_G7, 133 0, NOTE_E7, 0, NOTE_C7, 134 NOTE_D7, NOTE_B6, 0, 0 135}; 136//Mario main them tempo 137int tempo[] = { 138 12, 12, 12, 12, 139 12, 12, 12, 12, 140 12, 12, 12, 12, 141 12, 12, 12, 12, 142 143 12, 12, 12, 12, 144 12, 12, 12, 12, 145 12, 12, 12, 12, 146 12, 12, 12, 12, 147 148 9, 9, 9, 149 12, 12, 12, 12, 150 12, 12, 12, 12, 151 12, 12, 12, 12, 152 153 12, 12, 12, 12, 154 12, 12, 12, 12, 155 12, 12, 12, 12, 156 12, 12, 12, 12, 157 158 9, 9, 9, 159 12, 12, 12, 12, 160 12, 12, 12, 12, 161 12, 12, 12, 12, 162}; 163//Underworld melody 164int underworld_melody[] = { 165 NOTE_C4, NOTE_C5, NOTE_A3, NOTE_A4, 166 NOTE_AS3, NOTE_AS4, 0, 167 0, 168 NOTE_C4, NOTE_C5, NOTE_A3, NOTE_A4, 169 NOTE_AS3, NOTE_AS4, 0, 170 0, 171 NOTE_F3, NOTE_F4, NOTE_D3, NOTE_D4, 172 NOTE_DS3, NOTE_DS4, 0, 173 0, 174 NOTE_F3, NOTE_F4, NOTE_D3, NOTE_D4, 175 NOTE_DS3, NOTE_DS4, 0, 176 0, NOTE_DS4, NOTE_CS4, NOTE_D4, 177 NOTE_CS4, NOTE_DS4, 178 NOTE_DS4, NOTE_GS3, 179 NOTE_G3, NOTE_CS4, 180 NOTE_C4, NOTE_FS4, NOTE_F4, NOTE_E3, NOTE_AS4, NOTE_A4, 181 NOTE_GS4, NOTE_DS4, NOTE_B3, 182 NOTE_AS3, NOTE_A3, NOTE_GS3, 183 0, 0, 0 184}; 185//Underwolrd tempo 186int underworld_tempo[] = { 187 12, 12, 12, 12, 188 12, 12, 6, 189 3, 190 12, 12, 12, 12, 191 12, 12, 6, 192 3, 193 12, 12, 12, 12, 194 12, 12, 6, 195 3, 196 12, 12, 12, 12, 197 12, 12, 6, 198 6, 18, 18, 18, 199 6, 6, 200 6, 6, 201 6, 6, 202 18, 18, 18, 18, 18, 18, 203 10, 10, 10, 204 10, 10, 10, 205 3, 3, 3 206}; 207 208int thisNote = 0; 209int thisSong = 0; 210int buttonState = 0; 211const int buttonPin = 2; 212int acceso = 3; 213 214void setup(void) 215{ 216 pinMode(49, OUTPUT);//buzzer 217 pinMode(13, OUTPUT);//led indicator when singing a note 218 // led dal pin 3 al pin 7 219 for (int i = 3; i <= 7; i++) pinMode(i, OUTPUT); 220 // bottone sul pinBottone 221 pinMode(buttonPin, INPUT); 222 223} 224 225int size = sizeof(melody) / sizeof(int); 226int noteDuration; 227int activity = 0; 228 229void loop() 230{ 231 buttonState = digitalRead(buttonPin); 232 //sing the tunes 233 digitalWrite(acceso, HIGH); 234 if (buttonState == HIGH) { 235 if (thisSong == 2) { 236 activity = underworld_tempo[thisNote]; 237 buzz(melodyPin, underworld_melody[thisNote], noteDuration); 238 noteDuration = 1000 / underworld_tempo[thisNote]; 239 if (activity) { 240 digitalWrite(acceso, LOW); 241 if (acceso == 7) { 242 acceso = 3; 243 } else { 244 acceso++; 245 } 246 digitalWrite(acceso, HIGH); 247 } 248 } else { 249 activity = melody[thisNote]; 250 buzz(melodyPin, melody[thisNote], noteDuration); 251 noteDuration = 1000 / tempo[thisNote]; 252 if (activity) { 253 digitalWrite(acceso, LOW); 254 if (acceso == 7) { 255 acceso = 3; 256 } else { 257 acceso++; 258 } 259 digitalWrite(acceso, HIGH); 260 } 261 } 262 // to distinguish the notes, set a minimum time between them. 263 // the note's duration + 30% seems to work well: 264 int pauseBetweenNotes = noteDuration * 1.30; 265 delay(pauseBetweenNotes); 266 267 // stop the tone playing: 268 buzz(melodyPin, 0, noteDuration); 269 if (thisNote == size) { 270 thisNote = 0; 271 if (thisSong == 2) { 272 thisSong = 0; 273 size = sizeof(melody) / sizeof(int); 274 } else if (thisSong == 1) { 275 thisSong = 2; 276 size = sizeof(underworld_melody) / sizeof(int); 277 } else if (thisSong == 0) { 278 thisSong = 1; 279 size = sizeof(melody) / sizeof(int); 280 } 281 } else { 282 thisNote++; 283 } 284 285 } else { 286 if (thisSong == 2) { 287 activity = underworld_tempo[thisNote]; 288 if (activity == 0) { 289 buzz(melodyPin, underworld_melody[thisNote], noteDuration); 290 noteDuration = 1000 / underworld_tempo[thisNote]; 291 if (thisNote == size) { 292 thisNote = 0; 293 if (thisSong == 2) { 294 thisSong = 0; 295 size = sizeof(melody) / sizeof(int); 296 } else if (thisSong == 1) { 297 thisSong = 2; 298 size = sizeof(underworld_melody) / sizeof(int); 299 } else if (thisSong == 0) { 300 thisSong = 1; 301 size = sizeof(melody) / sizeof(int); 302 } 303 } else { 304 thisNote++; 305 } 306 } 307 } else { 308 activity = melody[thisNote]; 309 if (activity == 0) { 310 buzz(melodyPin, melody[thisNote], noteDuration); 311 noteDuration = 1000 / tempo[thisNote]; 312 if (thisNote == size) { 313 thisNote = 0; 314 if (thisSong == 2) { 315 thisSong = 0; 316 size = sizeof(melody) / sizeof(int); 317 } else if (thisSong == 1) { 318 thisSong = 2; 319 size = sizeof(underworld_melody) / sizeof(int); 320 } else if (thisSong == 0) { 321 thisSong = 1; 322 size = sizeof(melody) / sizeof(int); 323 } 324 } else { 325 thisNote++; 326 } 327 } 328 } 329 } 330 331 /*sing(1); 332 sing(1); 333 sing(2);*/ 334} 335int song = 0; 336 337void sing(int s) { 338 // iterate over the notes of the melody: 339 song = s; 340 if (song == 2) { 341 Serial.println(" 'Underworld Theme'"); 342 int size = sizeof(underworld_melody) / sizeof(int); 343 for (int thisNote = 0; thisNote < size; thisNote++) { 344 345 // to calculate the note duration, take one second 346 // divided by the note type. 347 //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc. 348 int noteDuration = 1000 / underworld_tempo[thisNote]; 349 350 buzz(melodyPin, underworld_melody[thisNote], noteDuration); 351 352 // to distinguish the notes, set a minimum time between them. 353 // the note's duration + 30% seems to work well: 354 int pauseBetweenNotes = noteDuration * 1.30; 355 delay(pauseBetweenNotes); 356 357 // stop the tone playing: 358 buzz(melodyPin, 0, noteDuration); 359 360 } 361 362 } else { 363 364 Serial.println(" 'Mario Theme'"); 365 int size = sizeof(melody) / sizeof(int); 366 for (int thisNote = 0; thisNote < size; thisNote++) { 367 368 // to calculate the note duration, take one second 369 // divided by the note type. 370 //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc. 371 int noteDuration = 1000 / tempo[thisNote]; 372 373 buzz(melodyPin, melody[thisNote], noteDuration); 374 375 // to distinguish the notes, set a minimum time between them. 376 // the note's duration + 30% seems to work well: 377 int pauseBetweenNotes = noteDuration * 1.30; 378 delay(pauseBetweenNotes); 379 380 // stop the tone playing: 381 buzz(melodyPin, 0, noteDuration); 382 383 } 384 } 385} 386 387void buzz(int targetPin, long frequency, long length) { 388 digitalWrite(13, HIGH); 389 long delayValue = 1000000 / frequency / 2; // calculate the delay value between transitions 390 //// 1 second's worth of microseconds, divided by the frequency, then split in half since 391 //// there are two phases to each cycle 392 long numCycles = frequency * length / 1000; // calculate the number of cycles for proper timing 393 //// multiply frequency, which is really cycles per second, by the number of seconds to 394 //// get the total number of cycles to produce 395 for (long i = 0; i < numCycles; i++) { // for the calculated length of time... 396 digitalWrite(targetPin, HIGH); // write the buzzer pin high to push out the diaphram 397 delayMicroseconds(delayValue); // wait for the calculated delay value 398 digitalWrite(targetPin, LOW); // write the buzzer pin low to pull back the diaphram 399 delayMicroseconds(delayValue); // wait again or the calculated delay value 400 } 401 digitalWrite(13, LOW); 402 403} 404
Downloadable files
Schematics of Super Mario theme with button
Simple schema of this project.
Schematics of Super Mario theme with button
Schematics of Super Mario theme with button
Simple schema of this project.
Schematics of Super Mario theme with button
Comments
Only logged in users can leave comments