Advertisement
ccarman602

Piezo Speaker Super Mario Brothers

Jun 3rd, 2020
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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. // use pin 3 for output to piezo
  19. #define melodyPin 3
  20.  
  21. // define the frequencies for all notes
  22. #define NOTE_B0  31
  23. #define NOTE_C1  33
  24. #define NOTE_CS1 35
  25. #define NOTE_D1  37
  26. #define NOTE_DS1 39
  27. #define NOTE_E1  41
  28. #define NOTE_F1  44
  29. #define NOTE_FS1 46
  30. #define NOTE_G1  49
  31. #define NOTE_GS1 52
  32. #define NOTE_A1  55
  33. #define NOTE_AS1 58
  34. #define NOTE_B1  62
  35. #define NOTE_C2  65
  36. #define NOTE_CS2 69
  37. #define NOTE_D2  73
  38. #define NOTE_DS2 78
  39. #define NOTE_E2  82
  40. #define NOTE_F2  87
  41. #define NOTE_FS2 93
  42. #define NOTE_G2  98
  43. #define NOTE_GS2 104
  44. #define NOTE_A2  110
  45. #define NOTE_AS2 117
  46. #define NOTE_B2  123
  47. #define NOTE_C3  131
  48. #define NOTE_CS3 139
  49. #define NOTE_D3  147
  50. #define NOTE_DS3 156
  51. #define NOTE_E3  165
  52. #define NOTE_F3  175
  53. #define NOTE_FS3 185
  54. #define NOTE_G3  196
  55. #define NOTE_GS3 208
  56. #define NOTE_A3  220
  57. #define NOTE_AS3 233
  58. #define NOTE_B3  247
  59. #define NOTE_C4  262
  60. #define NOTE_CS4 277
  61. #define NOTE_D4  294
  62. #define NOTE_DS4 311
  63. #define NOTE_E4  330
  64. #define NOTE_F4  349
  65. #define NOTE_FS4 370
  66. #define NOTE_G4  392
  67. #define NOTE_GS4 415
  68. #define NOTE_A4  440
  69. #define NOTE_AS4 466
  70. #define NOTE_B4  494
  71. #define NOTE_C5  523
  72. #define NOTE_CS5 554
  73. #define NOTE_D5  587
  74. #define NOTE_DS5 622
  75. #define NOTE_E5  659
  76. #define NOTE_F5  698
  77. #define NOTE_FS5 740
  78. #define NOTE_G5  784
  79. #define NOTE_GS5 831
  80. #define NOTE_A5  880
  81. #define NOTE_AS5 932
  82. #define NOTE_B5  988
  83. #define NOTE_C6  1047
  84. #define NOTE_CS6 1109
  85. #define NOTE_D6  1175
  86. #define NOTE_DS6 1245
  87. #define NOTE_E6  1319
  88. #define NOTE_F6  1397
  89. #define NOTE_FS6 1480
  90. #define NOTE_G6  1568
  91. #define NOTE_GS6 1661
  92. #define NOTE_A6  1760
  93. #define NOTE_AS6 1865
  94. #define NOTE_B6  1976
  95. #define NOTE_C7  2093
  96. #define NOTE_CS7 2217
  97. #define NOTE_D7  2349
  98. #define NOTE_DS7 2489
  99. #define NOTE_E7  2637
  100. #define NOTE_F7  2794
  101. #define NOTE_FS7 2960
  102. #define NOTE_G7  3136
  103. #define NOTE_GS7 3322
  104. #define NOTE_A7  3520
  105. #define NOTE_AS7 3729
  106. #define NOTE_B7  3951
  107. #define NOTE_C8  4186
  108. #define NOTE_CS8 4435
  109. #define NOTE_D8  4699
  110. #define NOTE_DS8 4978
  111.  
  112. //Mario main theme melody
  113. int melody[] = {
  114.   NOTE_E7, NOTE_E7, 0, NOTE_E7,
  115.   0, NOTE_C7, NOTE_E7, 0,
  116.   NOTE_G7, 0, 0,  0,
  117.   NOTE_G6, 0, 0, 0,
  118.  
  119.   NOTE_C7, 0, 0, NOTE_G6,
  120.   0, 0, NOTE_E6, 0,
  121.   0, NOTE_A6, 0, NOTE_B6,
  122.   0, NOTE_AS6, NOTE_A6, 0,
  123.  
  124.   NOTE_G6, NOTE_E7, NOTE_G7,
  125.   NOTE_A7, 0, NOTE_F7, NOTE_G7,
  126.   0, NOTE_E7, 0, NOTE_C7,
  127.   NOTE_D7, NOTE_B6, 0, 0,
  128.  
  129.   NOTE_C7, 0, 0, NOTE_G6,
  130.   0, 0, NOTE_E6, 0,
  131.   0, NOTE_A6, 0, NOTE_B6,
  132.   0, NOTE_AS6, NOTE_A6, 0,
  133.  
  134.   NOTE_G6, NOTE_E7, NOTE_G7,
  135.   NOTE_A7, 0, NOTE_F7, NOTE_G7,
  136.   0, NOTE_E7, 0, NOTE_C7,
  137.   NOTE_D7, NOTE_B6, 0, 0
  138. };
  139. //Mario main them tempo
  140. int tempo[] = {
  141.   12, 12, 12, 12,
  142.   12, 12, 12, 12,
  143.   12, 12, 12, 12,
  144.   12, 12, 12, 12,
  145.  
  146.   12, 12, 12, 12,
  147.   12, 12, 12, 12,
  148.   12, 12, 12, 12,
  149.   12, 12, 12, 12,
  150.  
  151.   9, 9, 9,
  152.   12, 12, 12, 12,
  153.   12, 12, 12, 12,
  154.   12, 12, 12, 12,
  155.  
  156.   12, 12, 12, 12,
  157.   12, 12, 12, 12,
  158.   12, 12, 12, 12,
  159.   12, 12, 12, 12,
  160.  
  161.   9, 9, 9,
  162.   12, 12, 12, 12,
  163.   12, 12, 12, 12,
  164.   12, 12, 12, 12,
  165. };
  166. //Underworld melody
  167. int underworld_melody[] = {
  168.   NOTE_C4, NOTE_C5, NOTE_A3, NOTE_A4,
  169.   NOTE_AS3, NOTE_AS4, 0,
  170.   0,
  171.   NOTE_C4, NOTE_C5, NOTE_A3, NOTE_A4,
  172.   NOTE_AS3, NOTE_AS4, 0,
  173.   0,
  174.   NOTE_F3, NOTE_F4, NOTE_D3, NOTE_D4,
  175.   NOTE_DS3, NOTE_DS4, 0,
  176.   0,
  177.   NOTE_F3, NOTE_F4, NOTE_D3, NOTE_D4,
  178.   NOTE_DS3, NOTE_DS4, 0,
  179.   0, NOTE_DS4, NOTE_CS4, NOTE_D4,
  180.   NOTE_CS4, NOTE_DS4,
  181.   NOTE_DS4, NOTE_GS3,
  182.   NOTE_G3, NOTE_CS4,
  183.   NOTE_C4, NOTE_FS4, NOTE_F4, NOTE_E3, NOTE_AS4, NOTE_A4,
  184.   NOTE_GS4, NOTE_DS4, NOTE_B3,
  185.   NOTE_AS3, NOTE_A3, NOTE_GS3,
  186.   0, 0, 0
  187. };
  188. //Underworld tempo
  189. int underworld_tempo[] = {
  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.   3,
  199.   12, 12, 12, 12,
  200.   12, 12, 6,
  201.   6, 18, 18, 18,
  202.   6, 6,
  203.   6, 6,
  204.   6, 6,
  205.   18, 18, 18, 18, 18, 18,
  206.   10, 10, 10,
  207.   10, 10, 10,
  208.   3, 3, 3
  209. };
  210.  
  211. void setup(void)
  212. {
  213.   pinMode(melodyPin, OUTPUT);//buzzer
  214.   pinMode(LED_BUILTIN, OUTPUT);//led indicator when singing a note
  215.  
  216. }
  217. void loop()
  218. {
  219.   //sing the tunes
  220.   sing(1);
  221.   sing(1);
  222.   sing(2);
  223. }
  224. int song = 0;
  225.  
  226. void sing(int s) {
  227.   // iterate over the notes of the melody:
  228.   song = s;
  229.   if (song == 2) {
  230.     Serial.println(" 'Underworld Theme'");
  231.     int size = sizeof(underworld_melody) / sizeof(int);
  232.     for (int thisNote = 0; thisNote < size; thisNote++) {
  233.  
  234.       // to calculate the note duration, take one second
  235.       // divided by the note type.
  236.       //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
  237.       int noteDuration = 1000 / underworld_tempo[thisNote];
  238.  
  239.       buzz(melodyPin, underworld_melody[thisNote], noteDuration);
  240.  
  241.       // to distinguish the notes, set a minimum time between them.
  242.       // the note's duration + 30% seems to work well:
  243.       int pauseBetweenNotes = noteDuration * 1.30;
  244.       delay(pauseBetweenNotes);
  245.  
  246.       // stop the tone playing:
  247.       buzz(melodyPin, 0, noteDuration);
  248.  
  249.     }
  250.  
  251.   } else {
  252.  
  253.     Serial.println(" 'Mario Theme'");
  254.     int size = sizeof(melody) / sizeof(int);
  255.     for (int thisNote = 0; thisNote < size; thisNote++) {
  256.  
  257.       // to calculate the note duration, take one second
  258.       // divided by the note type.
  259.       //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
  260.       int noteDuration = 1000 / tempo[thisNote];
  261.  
  262.       buzz(melodyPin, melody[thisNote], noteDuration);
  263.  
  264.       // to distinguish the notes, set a minimum time between them.
  265.       // the note's duration + 30% seems to work well:
  266.       int pauseBetweenNotes = noteDuration * 1.30;
  267.       delay(pauseBetweenNotes);
  268.  
  269.       // stop the tone playing:
  270.       buzz(melodyPin, 0, noteDuration);
  271.  
  272.     }
  273.   }
  274. }
  275.  
  276. void buzz(int targetPin, long frequency, long length) {
  277.   digitalWrite(LED_BUILTIN, HIGH);
  278.   long delayValue = 1000000 / frequency / 2; // calculate the delay value between transitions
  279.   //// 1 second's worth of microseconds, divided by the frequency, then split in half since
  280.   //// there are two phases to each cycle
  281.   long numCycles = frequency * length / 1000; // calculate the number of cycles for proper timing
  282.   //// multiply frequency, which is really cycles per second, by the number of seconds to
  283.   //// get the total number of cycles to produce
  284.   for (long i = 0; i < numCycles; i++) { // for the calculated length of time...
  285.     digitalWrite(targetPin, HIGH); // write the buzzer pin high to push out the diaphram
  286.     delayMicroseconds(delayValue); // wait for the calculated delay value
  287.     digitalWrite(targetPin, LOW); // write the buzzer pin low to pull back the diaphram
  288.     delayMicroseconds(delayValue); // wait again or the calculated delay value
  289.   }
  290.   digitalWrite(LED_BUILTIN, LOW);
  291.  
  292. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement