luistavares

Exemplo Tema Super Mario World com Arduino

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