Advertisement
microrobotics

KY-006 basic buzzer module play Imperial March

Apr 4th, 2023 (edited)
3,105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.   Imperial March - Star Wars
  3.   Connect a piezo buzzer or speaker to pin 11 or select a new pin.                                                                                                                    
  4. */
  5.  
  6. #define NOTE_B0  31
  7. #define NOTE_C1  33
  8. #define NOTE_CS1 35
  9. #define NOTE_D1  37
  10. #define NOTE_DS1 39
  11. #define NOTE_E1  41
  12. #define NOTE_F1  44
  13. #define NOTE_FS1 46
  14. #define NOTE_G1  49
  15. #define NOTE_GS1 52
  16. #define NOTE_A1  55
  17. #define NOTE_AS1 58
  18. #define NOTE_B1  62
  19. #define NOTE_C2  65
  20. #define NOTE_CS2 69
  21. #define NOTE_D2  73
  22. #define NOTE_DS2 78
  23. #define NOTE_E2  82
  24. #define NOTE_F2  87
  25. #define NOTE_FS2 93
  26. #define NOTE_G2  98
  27. #define NOTE_GS2 104
  28. #define NOTE_A2  110
  29. #define NOTE_AS2 117
  30. #define NOTE_B2  123
  31. #define NOTE_C3  131
  32. #define NOTE_CS3 139
  33. #define NOTE_D3  147
  34. #define NOTE_DS3 156
  35. #define NOTE_E3  165
  36. #define NOTE_F3  175
  37. #define NOTE_FS3 185
  38. #define NOTE_G3  196
  39. #define NOTE_GS3 208
  40. #define NOTE_A3  220
  41. #define NOTE_AS3 233
  42. #define NOTE_B3  247
  43. #define NOTE_C4  262
  44. #define NOTE_CS4 277
  45. #define NOTE_D4  294
  46. #define NOTE_DS4 311
  47. #define NOTE_E4  330
  48. #define NOTE_F4  349
  49. #define NOTE_FS4 370
  50. #define NOTE_G4  392
  51. #define NOTE_GS4 415
  52. #define NOTE_A4  440
  53. #define NOTE_AS4 466
  54. #define NOTE_B4  494
  55. #define NOTE_C5  523
  56. #define NOTE_CS5 554
  57. #define NOTE_D5  587
  58. #define NOTE_DS5 622
  59. #define NOTE_E5  659
  60. #define NOTE_F5  698
  61. #define NOTE_FS5 740
  62. #define NOTE_G5  784
  63. #define NOTE_GS5 831
  64. #define NOTE_A5  880
  65. #define NOTE_AS5 932
  66. #define NOTE_B5  988
  67. #define NOTE_C6  1047
  68. #define NOTE_CS6 1109
  69. #define NOTE_D6  1175
  70. #define NOTE_DS6 1245
  71. #define NOTE_E6  1319
  72. #define NOTE_F6  1397
  73. #define NOTE_FS6 1480
  74. #define NOTE_G6  1568
  75. #define NOTE_GS6 1661
  76. #define NOTE_A6  1760
  77. #define NOTE_AS6 1865
  78. #define NOTE_B6  1976
  79. #define NOTE_C7  2093
  80. #define NOTE_CS7 2217
  81. #define NOTE_D7  2349
  82. #define NOTE_DS7 2489
  83. #define NOTE_E7  2637
  84. #define NOTE_F7  2794
  85. #define NOTE_FS7 2960
  86. #define NOTE_G7  3136
  87. #define NOTE_GS7 3322
  88. #define NOTE_A7  3520
  89. #define NOTE_AS7 3729
  90. #define NOTE_B7  3951
  91. #define NOTE_C8  4186
  92. #define NOTE_CS8 4435
  93. #define NOTE_D8  4699
  94. #define NOTE_DS8 4978
  95. #define REST      0
  96.  
  97.  
  98. // change this to make the song slower or faster
  99. int tempo = 120;
  100.  
  101. // change this to whichever pin you want to use
  102. int buzzer = 11;
  103.  
  104. // notes of the moledy followed by the duration.
  105. // a 4 means a quarter note, 8 an eighteenth , 16 sixteenth, so on
  106. // !!negative numbers are used to represent dotted notes,
  107. // so -4 means a dotted quarter note, that is, a quarter plus an eighteenth!!
  108. int melody[] = {
  109.  
  110.   // Dart Vader theme (Imperial March) - Star wars
  111.   // Score available at https://musescore.com/user/202909/scores/1141521
  112.   // The tenor saxophone part was used
  113.  
  114.   NOTE_A4,-4, NOTE_A4,-4, NOTE_A4,16, NOTE_A4,16, NOTE_A4,16, NOTE_A4,16, NOTE_F4,8, REST,8,
  115.   NOTE_A4,-4, NOTE_A4,-4, NOTE_A4,16, NOTE_A4,16, NOTE_A4,16, NOTE_A4,16, NOTE_F4,8, REST,8,
  116.   NOTE_A4,4, NOTE_A4,4, NOTE_A4,4, NOTE_F4,-8, NOTE_C5,16,
  117.  
  118.   NOTE_A4,4, NOTE_F4,-8, NOTE_C5,16, NOTE_A4,2,//4
  119.   NOTE_E5,4, NOTE_E5,4, NOTE_E5,4, NOTE_F5,-8, NOTE_C5,16,
  120.   NOTE_A4,4, NOTE_F4,-8, NOTE_C5,16, NOTE_A4,2,
  121.  
  122.   NOTE_A5,4, NOTE_A4,-8, NOTE_A4,16, NOTE_A5,4, NOTE_GS5,-8, NOTE_G5,16, //7
  123.   NOTE_DS5,16, NOTE_D5,16, NOTE_DS5,8, REST,8, NOTE_A4,8, NOTE_DS5,4, NOTE_D5,-8, NOTE_CS5,16,
  124.  
  125.   NOTE_C5,16, NOTE_B4,16, NOTE_C5,16, REST,8, NOTE_F4,8, NOTE_GS4,4, NOTE_F4,-8, NOTE_A4,-16,//9
  126.   NOTE_C5,4, NOTE_A4,-8, NOTE_C5,16, NOTE_E5,2,
  127.  
  128.   NOTE_A5,4, NOTE_A4,-8, NOTE_A4,16, NOTE_A5,4, NOTE_GS5,-8, NOTE_G5,16, //7
  129.   NOTE_DS5,16, NOTE_D5,16, NOTE_DS5,8, REST,8, NOTE_A4,8, NOTE_DS5,4, NOTE_D5,-8, NOTE_CS5,16,
  130.  
  131.   NOTE_C5,16, NOTE_B4,16, NOTE_C5,16, REST,8, NOTE_F4,8, NOTE_GS4,4, NOTE_F4,-8, NOTE_A4,-16,//9
  132.   NOTE_A4,4, NOTE_F4,-8, NOTE_C5,16, NOTE_A4,2,
  133.  
  134. };
  135.  
  136. // sizeof gives the number of bytes, each int value is composed of two bytes (16 bits)
  137. // there are two values per note (pitch and duration), so for each note there are four bytes
  138. int notes = sizeof(melody) / sizeof(melody[0]) / 2;
  139.  
  140. // this calculates the duration of a whole note in ms
  141. int wholenote = (60000 * 4) / tempo;
  142.  
  143. int divider = 0, noteDuration = 0;
  144.  
  145. void setup() {
  146.   // iterate over the notes of the melody.
  147.   // Remember, the array is twice the number of notes (notes + durations)
  148.   for (int thisNote = 0; thisNote < notes * 2; thisNote = thisNote + 2) {
  149.  
  150.     // calculates the duration of each note
  151.     divider = melody[thisNote + 1];
  152.     if (divider > 0) {
  153.       // regular note, just proceed
  154.       noteDuration = (wholenote) / divider;
  155.     } else if (divider < 0) {
  156.       // dotted notes are represented with negative durations!!
  157.       noteDuration = (wholenote) / abs(divider);
  158.       noteDuration *= 1.5; // increases the duration in half for dotted notes
  159.     }
  160.  
  161.     // we only play the note for 90% of the duration, leaving 10% as a pause
  162.     tone(buzzer, melody[thisNote], noteDuration*0.9);
  163.  
  164.     // Wait for the specief duration before playing the next note.
  165.     delay(noteDuration);
  166.    
  167.     // stop the waveform generation before the next note.
  168.     noTone(buzzer);
  169.   }
  170. }
  171.  
  172. void loop() {
  173.   // no need to repeat the melody.
  174. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement