Advertisement
seby20

Untitled

Dec 20th, 2018
307
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.04 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.  
  14. MIDI_CREATE_DEFAULT_INSTANCE();
  15.  
  16. #define outputA 8
  17. #define outputB 9
  18.  
  19. #define ledPin 13
  20.  
  21. int counter = 0;
  22. int aState;
  23. int aLastState;
  24.  
  25. /////////////////////////////////////////////
  26. // buttons
  27. const int NButtons = 1;
  28. const int buttonPin[NButtons] = {4}; // the number of the pushbutton pin
  29. int buttonCState[NButtons] = {0}; // stores the button current value
  30. int buttonPState[NButtons] = {0}; // stores the button previous value
  31.  
  32. /////////////////////////////////////////////
  33. // debounce
  34. unsigned long lastDebounceTime[NButtons] = {0}; // the last time the output pin was toggled
  35. unsigned long debounceDelay = 5; // the debounce time; increase if the output flickers
  36.  
  37. /////////////////////////////////////////////
  38. // midi
  39. byte midiCh = 1; // MIDI channel to be used
  40. byte note = 36; // Lowest MIDI note
  41. byte cc = 1; // Lowest MIDI CC
  42.  
  43. void setup() {
  44.  
  45. // attachInterrupt(0, encoder, CHANGE);
  46. // attachInterrupt(1, encoder, CHANGE);
  47. enableInterrupt(outputA, encoder, CHANGE);
  48. enableInterrupt(outputB, encoder, CHANGE);
  49.  
  50. pinMode (outputA, INPUT_PULLUP);
  51. pinMode (outputB, INPUT_PULLUP);
  52. pinMode (buttonPin[0], INPUT_PULLUP);
  53. pinMode (ledPin, OUTPUT);
  54.  
  55. MIDI.begin();
  56. MIDI.turnThruOff();
  57. Serial.begin (9600);
  58.  
  59. MIDI.setHandleControlChange(handleControlChange); // Listens to control change
  60. MIDI.setHandleNoteOn(handleNoteOn); // Listens to note ons
  61. MIDI.setHandleNoteOff(handleNoteOff); // Listens to note offs
  62.  
  63. // Reads the initial state of the outputA
  64. aLastState = digitalRead(outputA);
  65. }
  66.  
  67. void loop() {
  68. MIDI.read(); // The only thing that should be in the loop for "MIDI in"
  69.  
  70.  
  71.  
  72.  
  73. buttons();
  74.  
  75. }
  76.  
  77. void encoder () {
  78.  
  79. aState = digitalRead(outputA); // Reads the current "state" of the Encoder
  80. //MIDI.disconnectCallbackFromType(midi::ControlChange);
  81.  
  82. // If the previous state and the current state of outputA are different, it means that a pulse occurred
  83. if (aState != aLastState) {
  84. // If the outputB state is different from outputA it means that the Encoder is rotating clockwise
  85. if (digitalRead(outputB) != aState) {
  86. counter ++;
  87. if (counter > 127) {
  88. counter = 127;
  89. }
  90. } else {
  91. counter --;
  92. if (counter < 0) {
  93. counter = 0;
  94. }
  95. }
  96. // Do what needs to be done
  97.  
  98. MIDI.sendControlChange(cc, counter, midiCh);
  99. //Serial.print("Midi CC: "); Serial.println(counter);
  100.  
  101. }
  102. aLastState = aState; // Stores the actual value in the previous value
  103.  
  104. }
  105.  
  106. /////////////////////////////////////////////
  107. // BUTTONS
  108. void buttons() {
  109.  
  110. for (int i = 0; i < NButtons; i++) {
  111.  
  112. buttonCState[i] = digitalRead(buttonPin[i]);
  113.  
  114. if ((millis() - lastDebounceTime[i]) > debounceDelay) {
  115.  
  116. if (buttonPState[i] != buttonCState[i]) {
  117. lastDebounceTime[i] = millis();
  118.  
  119. if (buttonCState[i] == LOW) {
  120. MIDI.sendNoteOn(note + i, 127, midiCh);
  121. // Serial.print("button on >> ");
  122. // Serial.println(i);
  123. }
  124. else {
  125. MIDI.sendNoteOn(note + i, 0, midiCh);
  126. // Serial.print("button off >> ");
  127. // Serial.println(i);
  128. }
  129. buttonPState[i] = buttonCState[i];
  130. }
  131. }
  132. }
  133. }
  134.  
  135. ////////////////////////////////////////////
  136. // MIDI IN
  137. void handleControlChange(byte channel, byte number, byte value) { // channel, CC, value
  138. counter = value;
  139. //return value;
  140. /* if (aLastState == 0){
  141. digitalWrite(ledPin, HIGH);
  142.  
  143. }else{
  144. digitalWrite(ledPin, LOW);
  145. }
  146. */
  147.  
  148. }
  149.  
  150.  
  151. void handleNoteOn(byte channel, byte number, byte value) { // channel, note, velocity
  152.  
  153.  
  154. }
  155.  
  156.  
  157. void handleNoteOff(byte channel, byte number, byte value) { // channel, note, velocity
  158.  
  159.  
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement