Guest User

Super Mario Bros Theme

a guest
Oct 12th, 2015
467
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. #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 3
  109. //Mario main theme melody
  110. int 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
  137. int 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
  164. int 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
  186. int 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.  
  208. void setup(void)
  209. {
  210. pinMode(3, OUTPUT);//buzzer
  211. pinMode(13, OUTPUT);//led indicator when singing a note
  212.  
  213. }
  214. void loop()
  215. {
  216. //sing the tunes
  217. sing(1);
  218. sing(1);
  219. sing(2);
  220. }
  221. int song = 0;
  222.  
  223. void sing(int s) {
  224. // iterate over the notes of the melody:
  225. song = s;
  226. if (song == 2) {
  227. Serial.println(" 'Underworld Theme'");
  228. int size = sizeof(underworld_melody) / sizeof(int);
  229. for (int thisNote = 0; thisNote < size; thisNote++) {
  230.  
  231. // to calculate the note duration, take one second
  232. // divided by the note type.
  233. //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
  234. int noteDuration = 1000 / underworld_tempo[thisNote];
  235.  
  236. buzz(melodyPin, underworld_melody[thisNote], noteDuration);
  237.  
  238. // to distinguish the notes, set a minimum time between them.
  239. // the note's duration + 30% seems to work well:
  240. int pauseBetweenNotes = noteDuration * 1.30;
  241. delay(pauseBetweenNotes);
  242.  
  243. // stop the tone playing:
  244. buzz(melodyPin, 0, noteDuration);
  245.  
  246. }
  247.  
  248. } else {
  249.  
  250. Serial.println(" 'Mario Theme'");
  251. int size = sizeof(melody) / sizeof(int);
  252. for (int thisNote = 0; thisNote < size; thisNote++) {
  253.  
  254. // to calculate the note duration, take one second
  255. // divided by the note type.
  256. //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
  257. int noteDuration = 1000 / tempo[thisNote];
  258.  
  259. buzz(melodyPin, melody[thisNote], noteDuration);
  260.  
  261. // to distinguish the notes, set a minimum time between them.
  262. // the note's duration + 30% seems to work well:
  263. int pauseBetweenNotes = noteDuration * 1.30;
  264. delay(pauseBetweenNotes);
  265.  
  266. // stop the tone playing:
  267. buzz(melodyPin, 0, noteDuration);
  268.  
  269. }
  270. }
  271. }
  272.  
  273. void buzz(int targetPin, long frequency, long length) {
  274. digitalWrite(13, HIGH);
  275. long delayValue = 1000000 / frequency / 2; // calculate the delay value between transitions
  276. //// 1 second's worth of microseconds, divided by the frequency, then split in half since
  277. //// there are two phases to each cycle
  278. long numCycles = frequency * length / 1000; // calculate the number of cycles for proper timing
  279. //// multiply frequency, which is really cycles per second, by the number of seconds to
  280. //// get the total number of cycles to produce
  281. for (long i = 0; i < numCycles; i++) { // for the calculated length of time...
  282. digitalWrite(targetPin, HIGH); // write the buzzer pin high to push out the diaphram
  283. delayMicroseconds(delayValue); // wait for the calculated delay value
  284. digitalWrite(targetPin, LOW); // write the buzzer pin low to pull back the diaphram
  285. delayMicroseconds(delayValue); // wait again or the calculated delay value
  286. }
  287. digitalWrite(13, LOW);
  288.  
  289. }
RAW Paste Data