Advertisement
seby20

Untitled

Dec 16th, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.07 KB | None | 0 0
  1. /* MAKING MUSIC WITH ARDUINO
  2.  
  3. "Encoder 4 - MIDI CC for Traktor"
  4.  
  5. by Gustavo Silveira
  6. ofered by: www.musiconerd.com & www.bitcontrollers.com
  7. gustavosilveira@musiconerd.com
  8.  
  9. */
  10.  
  11. #include <EnableInterrupt.h>
  12. #include <MIDI.h>
  13. #include <RotaryEncoder.h>;
  14.  
  15.  
  16. MIDI_CREATE_DEFAULT_INSTANCE();
  17.  
  18.  
  19.  
  20. #define outputA 8
  21. #define outputB 9
  22. #define outputA2 6
  23. #define outputB2 7
  24.  
  25. #define ledPin 5
  26.  
  27. int val = 0;
  28. int val2 = 0;
  29.  
  30. RotaryEncoder encoder(8,9,10,3,10);
  31. RotaryEncoder encoder2(6,7,10,3,10);
  32.  
  33. int counter = 0;
  34. int aState;
  35. int aLastState;
  36.  
  37. int counter2 = 0;
  38. int aState2;
  39. int aLastState2;
  40.  
  41. /////////////////////////////////////////////
  42. // buttons
  43. const int NButtons = 1;
  44. const int buttonPin[NButtons] = {4}; // the number of the pushbutton pin
  45. int buttonCState[NButtons] = {0}; // stores the button current value
  46. int buttonPState[NButtons] = {0}; // stores the button previous value
  47.  
  48. /////////////////////////////////////////////
  49.  
  50. // debounce
  51. unsigned long lastDebounceTime[NButtons] = {0}; // the last time the output pin was toggled
  52. unsigned long debounceDelay = 5; // the debounce time; increase if the output flickers
  53.  
  54. /////////////////////////////////////////////
  55. // midi
  56. byte midiCh = 1; // MIDI channel to be used
  57. byte note = 36; // Lowest MIDI note
  58. byte cc = 1; // Lowest MIDI CC
  59.  
  60. void setup() {
  61. enableInterrupt(outputA, encoderRot, CHANGE);
  62. enableInterrupt(outputB, encoderRot, CHANGE);
  63. enableInterrupt(val, encoderRot, CHANGE);
  64.  
  65. pinMode (outputA, INPUT_PULLUP);
  66. pinMode (outputB, INPUT_PULLUP);
  67. pinMode (val, INPUT_PULLUP);
  68. pinMode (buttonPin[0], INPUT_PULLUP);
  69. pinMode (ledPin, OUTPUT);
  70.  
  71. enableInterrupt(outputA2, encoderRot2, CHANGE);
  72. enableInterrupt(outputB2, encoderRot2, CHANGE);
  73. enableInterrupt(val2, encoderRot2, CHANGE);
  74.  
  75. pinMode (outputA2, INPUT_PULLUP);
  76. pinMode (outputB2, INPUT_PULLUP);
  77. pinMode (val2, INPUT_PULLUP);
  78.  
  79. // Reads the initial state of the outputA
  80. aLastState = digitalRead(outputA);
  81. aLastState2 = digitalRead(outputA2);
  82.  
  83. MIDI.begin();
  84. Serial.begin(115200);
  85.  
  86. MIDI.setHandleControlChange(handleControlChange); // Listens to control change
  87. MIDI.setHandleNoteOn(handleNoteOn); // Listens to note ons
  88. MIDI.setHandleNoteOff(handleNoteOff); // Listens to note offs
  89. }
  90.  
  91. void loop() {
  92.  
  93.  
  94. int enc = encoder.readEncoder();
  95. if(enc != 0) {
  96. val = val + (enc);
  97. val = min(val,127);
  98. val = max(val,0);
  99. //Serial.println(val);
  100. }
  101.  
  102. int enc2 = encoder2.readEncoder();
  103. if(enc2 != 0) {
  104. val2 = val2 + (enc2);
  105. val2 = min(val2,127);
  106. val2 = max(val2,0);
  107. //Serial.println(val);
  108. }
  109. delayMicroseconds(1);
  110.  
  111. buttons();
  112.  
  113. }
  114.  
  115. void encoderRot () {
  116.  
  117. aState = digitalRead(outputA); // Reads the current "state" of the Encoder
  118. // If the previous state and the current state of outputA are different, it means that a pulse occurred
  119. if (aState != aLastState) {
  120. // If the outputB state is different from outputA it means that the Encoder is rotating clockwise
  121. if (digitalRead(outputB) != aState) {
  122. MIDI.sendControlChange(cc, val, 1);
  123. } else {
  124. MIDI.sendControlChange(cc, val, 1);
  125. }
  126. //Serial.print("Midi CC: "); Serial.println(val);
  127. }
  128. aLastState = aState; // Stores the actual value in the previous value
  129. }
  130.  
  131. void encoderRot2 () {
  132. aState2 = digitalRead(outputA2); // Reads the current "state" of the Encoder
  133. // If the previous state and the current state of outputA are different, it means that a pulse occurred
  134. if (aState2 != aLastState2) {
  135. // If the outputB state is different from outputA it means that the Encoder is rotating clockwise
  136. if (digitalRead(outputB2) != aState2) {
  137. MIDI.sendControlChange(cc, val2, 2);
  138. } else {
  139. MIDI.sendControlChange(cc, val2, 2);
  140. }
  141. //Serial.print("Midi CC: "); Serial.println(val);
  142. }
  143. aLastState2 = aState2; // Stores the actual value in the previous value
  144. }
  145.  
  146.  
  147.  
  148.  
  149. /////////////////////////////////////////////
  150. // BUTTONS
  151. void buttons() {
  152.  
  153. for (int i = 0; i < NButtons; i++) {
  154.  
  155. buttonCState[i] = digitalRead(buttonPin[i]);
  156.  
  157. if ((millis() - lastDebounceTime[i]) > debounceDelay) {
  158.  
  159. if (buttonPState[i] != buttonCState[i]) {
  160. lastDebounceTime[i] = millis();
  161.  
  162. if (buttonCState[i] == LOW) {
  163. MIDI.sendNoteOn(note + i, 127, midiCh);
  164. // Serial.print("button on >> ");
  165. // Serial.println(i);
  166. }
  167. else {
  168. MIDI.sendNoteOn(note + i, 0, midiCh);
  169. // Serial.print("button off >> ");
  170. // Serial.println(i);
  171. }
  172. buttonPState[i] = buttonCState[i];
  173. }
  174. }
  175. }
  176. }
  177.  
  178. ////////////////////////////////////////////
  179. // MIDI IN
  180. void handleControlChange(byte channel, byte number, byte value) { // channel, CC, value
  181.  
  182.  
  183. }
  184.  
  185.  
  186. void handleNoteOn(byte channel, byte number, byte value) { // channel, note, velocity
  187.  
  188.  
  189. }
  190.  
  191.  
  192. void handleNoteOff(byte channel, byte number, byte value) { // channel, note, velocity
  193.  
  194.  
  195. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement