Advertisement
seby20

Untitled

Dec 19th, 2018
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.75 KB | None | 0 0
  1. #include <MIDI.h>
  2. MIDI_CREATE_DEFAULT_INSTANCE();
  3.  
  4.  
  5.  
  6.  
  7. // Setting up the counter
  8. int reading = 0;
  9. int lowest = 0;
  10. int highest = 127;
  11. int changeamnt = 1;
  12. int ledPin = 13;
  13.  
  14. // Timing for polling the encoder
  15. unsigned long currentTime;
  16. unsigned long lastTime;
  17.  
  18.  
  19. // Pin definitions
  20. const int pinA = 8;
  21. const int pinB = 9;
  22.  
  23. // Storing the readings
  24.  
  25. boolean encA;
  26. boolean encB;
  27. boolean lastA = false;
  28.  
  29. byte midiCh = 1; // MIDI channel to be used
  30. byte note = 36; // Lowest MIDI note
  31. byte cc = 1; // Lowest MIDI CC
  32.  
  33. void setup() {
  34. pinMode(ledPin, OUTPUT);
  35. // set the two pins as inputs with internal pullups
  36. pinMode(pinA, INPUT_PULLUP);
  37. pinMode(pinB, INPUT_PULLUP);
  38. // Set up the timing of the polling
  39. currentTime = millis();
  40. lastTime = currentTime;
  41. // Start the serial monitor for debugging
  42. MIDI.begin();
  43. MIDI.turnThruOff();
  44. Serial.begin(9600);
  45. MIDI.setHandleControlChange(handleControlChange); // Listens to control change
  46. //MIDI.setHandleNoteOn(handleNoteOn); // Listens to note ons
  47. //MIDI.setHandleNoteOff(handleNoteOff); // Listens to note offs
  48. }
  49.  
  50.  
  51. void loop()
  52. {
  53. if(lastTime <= (currentTime ))
  54. {
  55. MIDI.setHandleControlChange(handleControlChange); // re enable the call back function
  56. MIDI.read();
  57. }
  58.  
  59.  
  60. // Read elapsed time
  61. currentTime = millis();
  62. // Check if it's time to read
  63. if(currentTime >= (lastTime + 5))
  64. {
  65. MIDI.disconnectCallbackFromType(midi::ControlChange);
  66.  
  67. // read the two pins
  68. encA = digitalRead(pinA);
  69. encB = digitalRead(pinB);
  70. // check if A has gone from high to low
  71. if ((!encA) && (lastA))
  72. {
  73.  
  74. // check if B is high
  75. if (encB)
  76. {
  77.  
  78.  
  79. // clockwise
  80. if (reading + changeamnt <= highest)
  81. {
  82. reading = reading + changeamnt;
  83. }
  84. }
  85. else
  86. {
  87. // anti-clockwise
  88. if (reading - changeamnt >= lowest)
  89. {
  90. reading = reading - changeamnt;
  91. }
  92. }
  93. // Output reading for debugging
  94. MIDI.sendControlChange(cc, reading, midiCh);
  95.  
  96.  
  97.  
  98. //Serial.println(reading);
  99. }
  100. // store reading of A and millis for next loop
  101. lastA = encA;
  102. lastTime = currentTime;
  103.  
  104.  
  105. }
  106.  
  107. }
  108.  
  109. ///////////////////////////////////////////
  110. // MIDI IN
  111. void handleControlChange(byte channel, byte number, byte value) { // channel, CC, value
  112. reading = value;
  113.  
  114. // Serial.println(value);
  115. /* if (value != 0){
  116. digitalWrite(ledPin, HIGH);
  117. }else
  118. digitalWrite(ledPin, LOW);
  119. */
  120. }
  121.  
  122.  
  123. void handleNoteOn(byte channel, byte number, byte value) { // channel, note, velocity
  124.  
  125.  
  126. }
  127.  
  128.  
  129. void handleNoteOff(byte channel, byte number, byte value) { // channel, note, velocity
  130.  
  131.  
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement