Advertisement
Guest User

Untitled

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