Advertisement
Guest User

Untitled

a guest
Jan 14th, 2020
364
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.77 KB | None | 0 0
  1. #include<MIDI.h>
  2. MIDI_CREATE_DEFAULT_INSTANCE();
  3. #include<CapacitiveSensor.h>
  4. #include <LiquidCrystal.h>
  5.  
  6. #define ControlChange 0xB0
  7. #define NoteOn 0x90
  8.  
  9. //Arduino Pin Assignments
  10. const int motorDown    = 5;   //H-Bridge control to make the motor go down
  11. const int motorUp      = 6;   //H-Bridge control to make the motor go up
  12. const int wiper        = A0;   //Position of fader relative to GND (Analog 0)
  13. const int touchSend    = 4;   //Send pin for Capacitance Sensing Circuit (Digital 7)
  14. const int touchReceive = 2;   //Receive pin for Capacitance Sensing Circuit (Digital 8)
  15. const int channelLeft = 12;     // the number of the channelLeft (Digital 12) pin
  16. const int channelRight = 13;     // the number of the channelRight (Digital 13) pin
  17.  
  18. //Variables
  19. double   faderMax        = 0;     //Value read by fader's maximum position (0-1023)
  20. double   faderMin        = 0;     //Value read by fader's minimum position (0-1023)
  21. int      faderChannel    = 0;     //Value from 1-8
  22. int      chLeftBtn       = 0;
  23. int      chRightBtn      = 0;
  24. int      faderPositionLoop;
  25. bool     touched         = false; //Is the fader currently being touched?
  26. bool     positionUpdated = false; //Since touching, has the MIDI position been updated?
  27. String ascii = "";
  28. String oldAscii = "";
  29. bool compare = true;
  30. //LCD pins
  31. LiquidCrystal lcd(52, 50, 46, 44, 42, 40);
  32.  
  33. byte channel1LCD[]  {0xf0, 0x00, 0x00, 0x66, 0x05, 0x00, 0x10};
  34. CapacitiveSensor touchLine = CapacitiveSensor(touchSend, touchReceive);
  35.  
  36. void setup() {
  37.   // put your setup code here, to run once:
  38.   Serial.begin(31250);
  39.  
  40.   MIDI.begin(MIDI_CHANNEL_OMNI);  //Receive messages on all MIDI channels
  41.   MIDI.turnThruOff();             //We don't need MIDI through for this
  42.   MIDI.setHandleSystemExclusive(handle_sysex);
  43.   pinMode (motorUp, OUTPUT);
  44.   pinMode (motorDown, OUTPUT);
  45.  
  46.   calibrateFader();
  47.  
  48. }
  49.  
  50. void loop() {
  51.  
  52. int newPosition;
  53.   // update physical fader position
  54.   checkTouch();                    // check if phyisical fader has been touched
  55.   if (!positionUpdated) {           // if it has update midi to protools
  56.     updateFaderMidi();
  57.     newPosition = (((float)(analogRead(wiper) - faderMin) / (faderMax - faderMin)) * 16383);
  58.     updateFader(faderPositionLoop);
  59.     positionUpdated = true;
  60.   }
  61.   else {
  62.     faderPositionLoop = checkMIDI(); // run midi check and return fader position from protools
  63.     updateFader(faderPositionLoop);      // update physical fader with midi info
  64.  
  65.   }
  66.  
  67. }
  68.  
  69.  
  70. void calibrateFader() {
  71.   //Send fader to the top and read max position
  72.   digitalWrite(motorUp, HIGH);
  73.   delay(1000);
  74.   digitalWrite(motorUp, LOW);
  75.   faderMax = analogRead(wiper) - 5;
  76.  
  77.  
  78.   //Send fader to the bottom and read min position
  79.   digitalWrite(motorDown, HIGH);
  80.   delay(1000);
  81.   digitalWrite(motorDown, LOW);
  82.   faderMin = analogRead(wiper) + 5;
  83.  
  84. }
  85.  
  86.  
  87. // check for incoming midi signals
  88. int checkMIDI() {
  89.   static byte velocity1 = 0;
  90.   static byte velocity2 = 0;
  91.   static int faderPosition = 0;
  92.   static int oldMidi = 0;
  93.   if (MIDI.read()  && MIDI.getChannel() == 1 && MIDI.getType() == ControlChange && MIDI.getData1() == 0x00) // check for MSB
  94.   {
  95.     velocity1 = MIDI.getData2();
  96.   }
  97.   if (MIDI.read()  && MIDI.getChannel() == 1 && MIDI.getType() == ControlChange && MIDI.getData1() == 0x20)  // Check for LSB
  98.   {
  99.     velocity2 = MIDI.getData2();
  100.   }
  101.   faderPosition = (((velocity1 << 7) + velocity2) >> 4); //bitwise math to combine MSB and LSB
  102.  
  103.   return faderPosition ;   // remember to initialize using variable assignment with function call
  104. }
  105.  
  106.  
  107.  
  108. void updateFader(int fposition) {
  109.   if (fposition < analogRead(wiper) - 10 && fposition > faderMin && !touched) {
  110.     digitalWrite(motorDown, HIGH);
  111.     while (fposition < analogRead(wiper) - 10 && !touched) {};  //Loops until motor is done moving
  112.     digitalWrite(motorDown, LOW);
  113.   }
  114.   else if (fposition > analogRead(wiper) + 10 && fposition < faderMax && !touched) {
  115.     digitalWrite(motorUp, HIGH);
  116.     while (fposition > analogRead(wiper) + 10 && !touched) {}; //Loops until motor is done moving
  117.     digitalWrite(motorUp, LOW);
  118.   }
  119. }
  120.  
  121. void checkTouch() {
  122.   int movedFader;
  123.   //For the CapacitiveSensor comparison below,
  124.   //700 is arbitrary and may need to be changed
  125.   //depending on the fader cap used (if any).
  126.  
  127.   if (!touched && touchLine.capacitiveSensor(30) >= 3000) {
  128.     touched = true;
  129.  
  130.     //Send MIDI Touch On Message
  131.     MIDI.send(ControlChange, 0x0f, 0x00, 0x01);
  132.     MIDI.send(ControlChange, 0x2f, 0x40, 0x01);
  133.     digitalWrite(motorUp, LOW);
  134.     digitalWrite(motorDown, LOW);
  135.   }
  136.   else if (touched && touchLine.capacitiveSensor(30) < 3000) {
  137.     touched = false;
  138.  
  139.     MIDI.send(ControlChange, 0x0f, 0x00, 0x01);
  140.     MIDI.send(ControlChange, 0x2f, 0x00, 0x01);
  141.   }
  142.  
  143.   if (touched) {
  144.     positionUpdated = false;
  145.   }
  146. }
  147.  
  148. void updateFaderMidi() {
  149.   static int oldValue = 0;                        // set old value to 0 only for first time
  150.   int velocity = faderPosition();
  151.  
  152.   if (oldValue != velocity) {
  153.     byte faderMSB = (velocity >> 7) ;               // get MSB value in 7 bit
  154.     MIDI.send(ControlChange, 0x00, faderMSB, 0x01); // send MSB value
  155.  
  156.     byte faderLSB = (velocity & 0x7f);               // get LSB value
  157.     MIDI.send(ControlChange, 0x20, faderLSB, 0x01); // send new value
  158.     oldValue = velocity;
  159.  
  160.   }
  161. }
  162.  
  163. //Returns a MIDI pitch bend value for the fader's current position
  164. //Cases ensure that there is a -infinity (min) and max value despite possible math error
  165. int faderPosition() {
  166.   int fposition = analogRead(wiper);
  167.  
  168.   int returnValue = 0;
  169.  
  170.   if (fposition <= faderMin) {
  171.     returnValue = 0;
  172.   }
  173.   else if (fposition >= faderMax) {
  174.     returnValue = 16383;
  175.   }
  176.   else {
  177.     returnValue = ((float)(fposition - faderMin) / (faderMax - faderMin)) * 16383;
  178.   }
  179.  
  180.   return returnValue;
  181.  
  182. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement