seby20

Untitled

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