Advertisement
Guest User

NASU Theme

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