Advertisement
GrodenVR

Arduino USB-MIDI drum pedals for Paradiddle V1.0

Jul 14th, 2019
2,852
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.04 KB | None | 0 0
  1. #include <frequencyToNote.h>
  2. #include <MIDIUSB.h>
  3. #include <MIDIUSB_Defs.h>
  4. #include <pitchToFrequency.h>
  5. #include <pitchToNote.h>
  6. #include <Keyboard.h>
  7.  
  8. // MIDI and keyboard output for Arduino Leonardo
  9. // Kick Drum and hihat pedals for Paradiddle
  10. // Discord mic toggle channel mute
  11. // MIDI code referenced from https://www.arduino.cc/en/Tutorial/MidiDevice
  12.  
  13. // define pins
  14. const uint8_t kick = 2;
  15. const uint8_t hihat = 8;
  16. const uint8_t mic = 4;
  17. const uint8_t mute = 7;
  18. const byte notePitches[3] = {36, 42, 46}; // will be using MIDI drum notes:
  19. // 36 kick drum, 42 close hi-hat, 46 open hi-hat
  20.  
  21. // set volumes. 64 gives normal volume for MIDI notes
  22. uint8_t kickVolume = 64;
  23. uint8_t closehihatVolume = 30;
  24. uint8_t openhihatVolume = 0; // 64 is too loud in Paradiddle (gives sound of hitting hihat)
  25.  
  26. uint8_t pressedPedals = 0x00;
  27. uint8_t previousPedals = 0x00;
  28.  
  29.  
  30. void setup()
  31. {
  32. // make pins an input and use internal resistor (pullup)
  33. // so it reads high unless connected to ground
  34. pinMode(kick, INPUT_PULLUP);
  35. pinMode(hihat, INPUT_PULLUP);
  36. pinMode(mic, INPUT_PULLUP); // red button (mute mic)
  37. pinMode(mute, INPUT_PULLUP); // blue button (mute channel)
  38.  
  39. Keyboard.begin();
  40. }
  41.  
  42. void loop()
  43. {
  44. readPedals();
  45. readButtons();
  46. playNotes();
  47. }
  48.  
  49. // First parameter is the event type (0x0B = control change).
  50. // Second parameter is the event type, combined with the channel.
  51. // Third parameter is the control number number (0-119).
  52. // Fourth parameter is the control value (0-127).
  53.  
  54. void controlChange(byte channel, byte control, byte value)
  55. {
  56. midiEventPacket_t event = {0x0B, 0xB0 | channel, control, value};
  57. MidiUSB.sendMIDI(event);
  58. }
  59.  
  60. void readPedals()
  61. {
  62. if (digitalRead(kick) == LOW)
  63. {
  64. bitWrite(pressedPedals, 0, 1);
  65. delay(20); // prevents multiple events and feels better
  66. }
  67. else
  68. bitWrite(pressedPedals, 0, 0);
  69.  
  70. if (digitalRead(hihat) == LOW)
  71. {
  72. bitWrite(pressedPedals, 1, 1);
  73. }
  74. else
  75. bitWrite(pressedPedals, 1, 0);
  76. }
  77.  
  78. void readButtons()
  79. {
  80. if(digitalRead(mic)==LOW) // red button: Discord mic mute
  81. {
  82. Keyboard.press(KEY_LEFT_CTRL);
  83. Keyboard.press(KEY_LEFT_SHIFT);
  84. Keyboard.press(77); // send m key
  85. delay(100);
  86. Keyboard.releaseAll();
  87. delay(300);
  88. }
  89.  
  90. if(digitalRead(mute)==LOW) // blue button: Discord mute channel
  91. {
  92. Keyboard.press(KEY_LEFT_CTRL);
  93. Keyboard.press(KEY_LEFT_SHIFT);
  94. Keyboard.press(68); // send d key
  95. delay(100);
  96. Keyboard.releaseAll();
  97. delay(300);
  98. }
  99. }
  100.  
  101. void playNotes()
  102. {
  103. if (bitRead(pressedPedals, 0) != bitRead(previousPedals, 0)) // kick pedal
  104. {
  105. if (bitRead(pressedPedals, 0))
  106. {
  107. bitWrite(previousPedals, 0 , 1);
  108. noteOn(0, notePitches[0], kickVolume);
  109. MidiUSB.flush();
  110. }
  111. else
  112. {
  113. bitWrite(previousPedals, 0 , 0);
  114. noteOff(0, notePitches[0], 0);
  115. MidiUSB.flush();
  116. }
  117. }
  118.  
  119. if (bitRead(pressedPedals, 1) != bitRead(previousPedals, 1)) // hi-hat
  120. {
  121. if (bitRead(pressedPedals, 1))
  122. {
  123. bitWrite(previousPedals, 1 , 1);
  124. noteOn(0, notePitches[1], closehihatVolume);
  125. MidiUSB.flush();
  126. }
  127. else
  128. {
  129. bitWrite(previousPedals, 1 , 0);
  130. noteOn(0, notePitches[2], openhihatVolume);
  131. MidiUSB.flush();
  132. }
  133. }
  134. }
  135.  
  136. // First parameter is the event type (0x09 = note on, 0x08 = note off).
  137. // Second parameter is note-on/note-off, combined with the channel.
  138. // Channel can be anything between 0-15. Typically reported to the user as 1-16.
  139. // Third parameter is the note number (48 = middle C).
  140. // Fourth parameter is the velocity (64 = normal, 127 = fastest).
  141.  
  142. void noteOn(byte channel, byte pitch, byte velocity)
  143. {
  144. midiEventPacket_t noteOn = {0x09, 0x90 | channel, pitch, velocity};
  145. MidiUSB.sendMIDI(noteOn);
  146. }
  147.  
  148. void noteOff(byte channel, byte pitch, byte velocity)
  149. {
  150. midiEventPacket_t noteOff = {0x08, 0x80 | channel, pitch, velocity};
  151. MidiUSB.sendMIDI(noteOff);
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement