Advertisement
Guest User

Untitled

a guest
Mar 21st, 2021
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.65 KB | None | 0 0
  1. ############## INO FILE ##############
  2.  
  3.  
  4. #include "Arduino.h"
  5. #include <midi.h>
  6. #include <controller.h>
  7.  
  8. // Function declarations:
  9. void updateButtons();
  10.  
  11. MIDI_CREATE_DEFAULT_INSTANCE();
  12.  
  13. //************************************************************
  14. //***SET THE NUMBER OF CONTROLS USED**************************
  15. //************************************************************
  16. byte NUMBER_BUTTONS = 2;
  17.  
  18.  
  19. //************************************************************
  20. //***DEFINE WHAT THE BUTTONS SHOULD DO************************
  21. //***Button (Pin Number, Command, Note Number, Channel, Debounce Time)
  22. //*** Command parameter 0=NOTE 1=CC 2=Toggle CC 3=PC **
  23. //************************************************************
  24. Button but1 (2, 3, 19, 1, 5); //on pin 2, PC command, pc = 19, MIDI channel 1, 5ms debounce time --> Wes Patch
  25. Button but2 (3, 3, 18, 1, 5); //on pin 3, PC command, pc = 19, MIDI channel 1, 5ms debounce time --> Foo Fighters Patch
  26. //************************************************************
  27. //*** Add all the buttons to a button array
  28. Button *BUTTONS[] {&but1, &but2};
  29.  
  30.  
  31.  
  32. void setup()
  33. {
  34. MIDI.begin(MIDI_CHANNEL_OFF);
  35. Serial.begin(9600);
  36. }
  37.  
  38.  
  39. void loop()
  40. {
  41. if (NUMBER_BUTTONS != 0) updateButtons();
  42.  
  43. }
  44.  
  45. // Functions:
  46. void updateButtons () {
  47. // cycle through BUTTONS array
  48. for (int i = 0 ; i < NUMBER_BUTTONS ; i++) {
  49. byte message = BUTTONS[i]->getValue();
  50.  
  51. // if button is pressed:
  52. if (message == 0) {
  53. switch (BUTTONS[i]->Bcommand) {
  54. case 0: // Note
  55. MIDI.sendNoteOn(BUTTONS[i]->Bvalue, 127, BUTTONS[i]->Bchannel);
  56. break;
  57. case 1: // CC
  58. MIDI.sendControlChange(BUTTONS[i]->Bvalue, 127, BUTTONS[i]->Bchannel);
  59. break;
  60. case 2: // Toggle CC
  61. if (BUTTONS[i]->Btoggle == 0) {
  62. MIDI.sendControlChange(BUTTONS[i]->Bvalue, 127, BUTTONS[i]->Bchannel);
  63. BUTTONS[i]->Btoggle = 1;
  64. } else if (BUTTONS[i]->Btoggle == 1) {
  65. MIDI.sendControlChange(BUTTONS[i]->Bvalue, 0, BUTTONS[i]->Bchannel);
  66. BUTTONS[i]->Btoggle = 0;
  67. }
  68. break;
  69. case 3: // PC
  70. MIDI.sendProgramChange(BUTTONS[i]->Bvalue, BUTTONS[i]->Bchannel);
  71. Serial.print("Sending PC Value: ");
  72. Serial.print(BUTTONS[i]->Bvalue);
  73. Serial.print(" on MIDI channel: ");
  74. Serial.println(BUTTONS[i]->Bchannel);
  75. break;
  76. }
  77. }
  78.  
  79. // if button is unpressed
  80. if (message == 1) {
  81. switch (BUTTONS[i]->Bcommand) {
  82. case 0: //Note released
  83. MIDI.sendNoteOff(BUTTONS[i]->Bvalue, 0, BUTTONS[i]->Bchannel);
  84. break;
  85. case 1:
  86. MIDI.sendControlChange(BUTTONS[i]->Bvalue, 0, BUTTONS[i]->Bchannel);
  87. break;
  88. }
  89. }
  90.  
  91. }
  92. }
  93.  
  94.  
  95.  
  96. ############## HEADER FILE ##############
  97. #ifndef Controller_h
  98. #define Controller_h
  99.  
  100. #include <Arduino.h>
  101.  
  102. //Button (Pin Number, Command, Note Number, Channel, Debounce Time)
  103. class Button {
  104. public:
  105. Button(byte pin, int command, byte value, byte channel, byte debounce);
  106. byte getValue();
  107. int Bcommand;
  108. byte Bvalue;
  109. byte Bchannel;
  110. byte Btoggle;
  111.  
  112. private:
  113. byte _pin;
  114. byte _value;
  115. int _command;
  116. byte _debounce;
  117. unsigned long _time;
  118. byte _status;
  119. byte _last;
  120.  
  121. };
  122.  
  123. #endif
  124.  
  125.  
  126. ############## CPP FILE ##############
  127. #include "controller.h"
  128.  
  129. Button::Button(byte pin, int command, byte value, byte channel, byte debounce) {
  130. _pin = pin;
  131. _command = command;
  132. _value = value;
  133. _debounce = debounce;
  134. Bvalue = value;
  135. Bcommand = command;
  136. Bchannel = channel;
  137. Btoggle = 0;
  138. _status = 0b00000010;
  139. _last = 1;
  140. }
  141.  
  142. byte Button::getValue() {
  143. // If BUSY bit is not set - read button
  144. if (bitRead(_status, 0) == false) { // if BUSY (last bit) is false
  145. if (digitalRead(_pin) == _last) { // if same as last state - exit
  146. return 2;
  147. }
  148. }
  149.  
  150. // If NEW Bit set - Key just pressed
  151. if (bitRead(_status, 1) == true) { // if NEW (second last bit) is true
  152. bitSet (_status, 0); // set BUSY (last bit) to true
  153. bitClear(_status, 1); // set NEW (second last bit) to false
  154. _time = millis();
  155. return 200;
  156. }
  157.  
  158. // Check if debounce time has passed. If not, exit
  159. if (millis() - _time < _debounce ) {
  160. return 100;
  161. }
  162.  
  163. // Debounce time has passed now. Read pin to see if still set the same
  164. // If it has changed back - assume false alarm
  165. if (digitalRead(_pin) == _last) {
  166. bitClear(_status, 0); // set BUSY to false
  167. bitSet(_status, 1); // set NEW to true
  168. return 255;
  169. }
  170.  
  171. // If this point is reached, event is valid. return event type
  172. else {
  173. bitClear(_status, 0); // set BUSY to false
  174. bitSet(_status, 1); // set NEW to true
  175. _last = ((~_last) & 0b00000001); // invert _last
  176. return _last;
  177. }
  178. }
  179.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement