Advertisement
Guest User

Untitled

a guest
Sep 8th, 2017
1,078
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.53 KB | None | 0 0
  1. #include <MIDI.h>
  2. #include "Controller.h"
  3.  
  4. /*************************************************************
  5. MIDI CONTROLLER
  6.  
  7. by Notes and Volts
  8. www.notesandvolts.com
  9.  
  10. Version 1.2 **Arduino UNO ONLY!**
  11. *************************************************************/
  12.  
  13. MIDI_CREATE_DEFAULT_INSTANCE();
  14.  
  15. //************************************************************
  16. //***SET THE NUMBER OF CONTROLS USED**************************
  17. //************************************************************
  18. //---How many buttons are connected directly to pins?---------
  19. byte NUMBER_BUTTONS = 0;
  20. //---How many potentiometers are connected directly to pins?--
  21. byte NUMBER_POTS = 1;
  22. //---How many buttons are connected to a multiplexer?---------
  23. byte NUMBER_MUX_BUTTONS = 0;
  24. //---How many potentiometers are connected to a multiplexer?--
  25. byte NUMBER_MUX_POTS = 0;
  26. //************************************************************
  27.  
  28. //***ANY MULTIPLEXERS? (74HC4067)************************************
  29. //MUX address pins must be connected to Arduino UNO pins 2,3,4,5
  30. //A0 = PIN2, A1 = PIN3, A2 = PIN4, A3 = PIN5
  31. //*******************************************************************
  32. //Mux NAME (OUTPUT PIN, , How Many Mux Pins?(8 or 16) , Is It Analog?);
  33.  
  34.  
  35. //Mux M1(10, 16, false); //Digital multiplexer on Arduino pin 10
  36. //Mux M2(A5, 8, true); //Analog multiplexer on Arduino analog pin A0
  37. //*******************************************************************
  38.  
  39.  
  40. //***DEFINE DIRECTLY CONNECTED POTENTIOMETERS************************
  41. //Pot (Pin Number, Command, CC Control, Channel Number)
  42. //**Command parameter is for future use**
  43.  
  44. Pot PO1(A0, 0, 1, 1);
  45. Pot PO2(A1, 0, 10, 2);
  46. Pot PO3(A2, 0, 22, 3);
  47. Pot PO4(A3, 0, 118, 4);
  48. Pot PO5(A4, 0, 30, 5);
  49. Pot PO6(A5, 0, 31, 6);
  50. //*******************************************************************
  51. //Add pots used to array below like this-> Pot *POTS[] {&PO1, &PO2, &PO3, &PO4, &PO5, &PO6};
  52. Pot *POTS[] {&PO1};
  53. //*******************************************************************
  54.  
  55.  
  56. //***DEFINE DIRECTLY CONNECTED BUTTONS*******************************
  57. //Button (Pin Number, Command, Note Number, Channel, Debounce Time)
  58. //** Command parameter 0=NOTE 1=CC 2=Toggle CC **
  59.  
  60. Button BU1(2, 0, 60, 1, 5 );
  61. Button BU2(3, 0, 61, 1, 5 );
  62. Button BU3(4, 2, 62, 1, 5 );
  63. Button BU4(5, 1, 63, 1, 5 );
  64. Button BU5(6, 1, 64, 1, 5 );
  65. Button BU6(7, 1, 65, 1, 5 );
  66. Button BU7(8, 2, 66, 1, 5 );
  67. Button BU8(9, 2, 67, 1, 5 );
  68. Button BU9(10, 2, 68, 1, 5 );
  69. Button BU10(11, 2, 69, 1, 5 );
  70. Button BU11(12, 2, 70, 1, 5 );
  71. Button BU12(13, 2, 71, 1, 5 );
  72. //*******************************************************************
  73. //Add buttons used to array below like this-> Button *BUTTONS[] {&BU1, &BU2, &BU3, &BU4, &BU5, &BU6, &BU7, &BU8};
  74. Button *BUTTONS[] {&BU1, &BU2, &BU3, &BU4, &BU5, &BU6, &BU7, &BU8, &BU9, &BU10, &BU11, &BU12};
  75. //*******************************************************************
  76.  
  77.  
  78. //***DEFINE BUTTONS CONNECTED TO MULTIPLEXER*************************
  79. //Button::Button(Mux mux, byte muxpin, byte command, byte value, byte channel, byte debounce)
  80. //** Command parameter 0=NOTE 1=CC 2=Toggle CC **
  81.  
  82. //Button MBU1(M1, 0, 0, 70, 1, 5);
  83. //Button MBU2(M1, 1, 1, 71, 1, 5);
  84. //Button MBU3(M1, 2, 2, 72, 1, 5);
  85. //Button MBU4(M1, 3, 0, 73, 1, 5);
  86. //Button MBU5(M1, 4, 0, 74, 1, 5);
  87. //Button MBU6(M1, 5, 0, 75, 1, 5);
  88. //Button MBU7(M1, 6, 0, 76, 1, 5);
  89. //Button MBU8(M1, 7, 0, 77, 1, 5);
  90. //Button MBU9(M1, 8, 0, 78, 1, 5);
  91. //Button MBU10(M1, 9, 0, 79, 1, 5);
  92. //Button MBU11(M1, 10, 0, 80, 1, 5);
  93. //Button MBU12(M1, 11, 0, 81, 1, 5);
  94. //Button MBU13(M1, 12, 0, 82, 1, 5);
  95. //Button MBU14(M1, 13, 0, 83, 1, 5);
  96. //Button MBU15(M1, 14, 0, 84, 1, 5);
  97. //Button MBU16(M1, 15, 0, 85, 1, 5);
  98. //*******************************************************************
  99. ////Add multiplexed buttons used to array below like this-> Button *MUXBUTTONS[] {&MBU1, &MBU2, &MBU3, &MBU4, &MBU5, &MBU6.....};
  100. Button *MUXBUTTONS[] {};
  101.  
  102. //*******************************************************************
  103.  
  104.  
  105. //***DEFINE POTENTIOMETERS CONNECTED TO MULTIPLEXER*******************
  106. //Pot::Pot(Mux mux, byte muxpin, byte command, byte control, byte channel)
  107. //**Command parameter is for future use**
  108.  
  109. //Pot MPO1(M2, 0, 0, 1, 1);
  110. //Pot MPO2(M2, 1, 0, 7, 1);
  111. //Pot MPO3(M2, 2, 0, 50, 1);
  112. //Pot MPO4(M2, 3, 0, 55, 2);
  113. //Pot MPO5(M2, 4, 0, 50, 1);
  114. //Pot MPO6(M2, 5, 0, 55, 2);
  115. //Pot MPO7(M2, 6, 0, 50, 1);
  116. //Pot MPO8(M2, 7, 0, 55, 2);
  117. //Pot MPO9(M2, 8, 0, 50, 1);
  118. //Pot MPO10(M2, 9, 0, 55, 2);
  119. //Pot MPO11(M2, 10, 0, 50, 1);
  120. //Pot MPO12(M2, 11, 0, 55, 2);
  121. //Pot MPO13(M2, 12, 0, 50, 1);
  122. //Pot MPO14(M2, 13, 0, 55, 2);
  123. //Pot MPO15(M2, 14, 0, 50, 1);
  124. //Pot MPO16(M2, 15, 0, 55, 2);
  125. //*******************************************************************
  126. //Add multiplexed pots used to array below like this-> Pot *MUXPOTS[] {&MPO1, &MPO2, &MPO3, &MPO4, &MPO5, &MPO6.....};
  127. Pot *MUXPOTS[] {};
  128. //*******************************************************************
  129.  
  130.  
  131. void setup() {
  132. MIDI.begin(MIDI_CHANNEL_OFF);
  133. Serial.begin(115200);
  134. }
  135.  
  136. void loop() {
  137. if (NUMBER_BUTTONS != 0) updateButtons();
  138. if (NUMBER_POTS != 0) updatePots();
  139. if (NUMBER_MUX_BUTTONS != 0) updateMuxButtons();
  140. if (NUMBER_MUX_POTS != 0) updateMuxPots();
  141. }
  142.  
  143.  
  144. //*****************************************************************
  145. void updateButtons() {
  146.  
  147. // Cycle through Button array
  148. for (int i = 0; i < NUMBER_BUTTONS; i = i + 1) {
  149. byte message = BUTTONS[i]->getValue();
  150.  
  151. // Button is pressed
  152. if (message == 0) {
  153. switch (BUTTONS[i]->Bcommand) {
  154. case 0: //Note
  155. MIDI.sendNoteOn(BUTTONS[i]->Bvalue, 127, BUTTONS[i]->Bchannel);
  156. break;
  157. case 1: //CC
  158. MIDI.sendControlChange(BUTTONS[i]->Bvalue, 127, BUTTONS[i]->Bchannel);
  159. break;
  160. case 2: //Toggle
  161. if (BUTTONS[i]->Btoggle == 0) {
  162. MIDI.sendControlChange(BUTTONS[i]->Bvalue, 127, BUTTONS[i]->Bchannel);
  163. BUTTONS[i]->Btoggle = 1;
  164. }
  165. else if (BUTTONS[i]->Btoggle == 1) {
  166. MIDI.sendControlChange(BUTTONS[i]->Bvalue, 0, BUTTONS[i]->Bchannel);
  167. BUTTONS[i]->Btoggle = 0;
  168. }
  169. break;
  170. }
  171. }
  172.  
  173. // Button is not pressed
  174. if (message == 1) {
  175. switch (BUTTONS[i]->Bcommand) {
  176. case 0:
  177. MIDI.sendNoteOff(BUTTONS[i]->Bvalue, 0, BUTTONS[i]->Bchannel);
  178. break;
  179. case 1:
  180. MIDI.sendControlChange(BUTTONS[i]->Bvalue, 0, BUTTONS[i]->Bchannel);
  181. break;
  182. }
  183. }
  184. }
  185. }
  186. //*******************************************************************
  187. void updateMuxButtons() {
  188.  
  189. // Cycle through Mux Button array
  190. for (int i = 0; i < NUMBER_MUX_BUTTONS; i = i + 1) {
  191.  
  192. MUXBUTTONS[i]->muxUpdate();
  193. byte message = MUXBUTTONS[i]->getValue();
  194.  
  195. // Button is pressed
  196. if (message == 0) {
  197. switch (MUXBUTTONS[i]->Bcommand) {
  198. case 0: //Note
  199. MIDI.sendNoteOn(MUXBUTTONS[i]->Bvalue, 127, MUXBUTTONS[i]->Bchannel);
  200. break;
  201. case 1: //CC
  202. MIDI.sendControlChange(MUXBUTTONS[i]->Bvalue, 127, MUXBUTTONS[i]->Bchannel);
  203. break;
  204. case 2: //Toggle
  205. if (MUXBUTTONS[i]->Btoggle == 0) {
  206. MIDI.sendControlChange(MUXBUTTONS[i]->Bvalue, 127, MUXBUTTONS[i]->Bchannel);
  207. MUXBUTTONS[i]->Btoggle = 1;
  208. }
  209. else if (MUXBUTTONS[i]->Btoggle == 1) {
  210. MIDI.sendControlChange(MUXBUTTONS[i]->Bvalue, 0, MUXBUTTONS[i]->Bchannel);
  211. MUXBUTTONS[i]->Btoggle = 0;
  212. }
  213. break;
  214. }
  215. }
  216. // Button is not pressed
  217. if (message == 1) {
  218. switch (MUXBUTTONS[i]->Bcommand) {
  219. case 0:
  220. MIDI.sendNoteOff(MUXBUTTONS[i]->Bvalue, 0, MUXBUTTONS[i]->Bchannel);
  221. break;
  222. case 1:
  223. MIDI.sendControlChange(MUXBUTTONS[i]->Bvalue, 0, MUXBUTTONS[i]->Bchannel);
  224. break;
  225. }
  226. }
  227. }
  228. }
  229. //***********************************************************************
  230. void updatePots() {
  231. for (int i = 0; i < NUMBER_POTS; i = i + 1) {
  232. byte potmessage = POTS[i]->getValue();
  233. if (potmessage != 255) MIDI.sendControlChange(POTS[i]->Pcontrol, potmessage, POTS[i]->Pchannel);
  234. }
  235. }
  236. //***********************************************************************
  237. void updateMuxPots() {
  238. for (int i = 0; i < NUMBER_MUX_POTS; i = i + 1) {
  239. MUXPOTS[i]->muxUpdate();
  240. byte potmessage = MUXPOTS[i]->getValue();
  241. if (potmessage != 255) MIDI.sendControlChange(MUXPOTS[i]->Pcontrol, potmessage, MUXPOTS[i]->Pchannel);
  242. }
  243. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement