Author: Not specified Language: c
Description: Not specified Timestamp: 2018-04-13 15:57:53 +0000
View raw paste Reply
  1.  
  2. //Rotary Stuff
  3. int outA[] =  {4,   2,    8,    9,    20,   19,   30,   27};
  4. int outB[] =  {5,   3,    10,   23,   11,   32,   12,   28};
  5. int state[] = {0,   0,    0,    0,    0,    0,    0,    0};
  6. int lasts[] = {0,   0,    0,    0,    0,    0,    0,    0};
  7. int values[] = {100, 100,  100,  100,  100,  100,  100,  100};
  8.  
  9. int numKnobs = 8;
  10.  
  11. //Slider Stuff
  12. int sliderPins[] = {A0, A3, A2, A1};
  13. int lastslider[] = {0,  0,  0,  0};
  14.  
  15. int slidermin = 0;
  16. int slidermax = 1024;
  17. int numSliders = 4;
  18.  
  19. //Button Stuff
  20.  
  21. //Midi Stuff
  22.  
  23. const int channel = 1;
  24.  
  25. void setup() {
  26.  
  27.   for (int i = 0; i < numKnobs; i++)
  28.   {
  29.     pinMode (outA[i], INPUT_PULLUP);
  30.     pinMode (outB[i], INPUT_PULLUP);
  31.     lasts[i] = digitalRead(outA[i]);
  32.   }
  33.  
  34.   Serial.begin (250000);
  35.   usbMIDI.begin();
  36. }
  37.  
  38. void loop() {
  39.  
  40.   for (int i = 0; i < numSliders; i++)
  41.     doSlider(i);
  42.  
  43.   for (int i = 0; i < numKnobs; i++)
  44.     doKnobs(i);
  45.  
  46. }
  47.  
  48. void doSlider(int sliderNum) {
  49.   int sliderVal = analogRead(sliderPins[sliderNum]);
  50.  
  51.   if (sliderVal >= lastslider[sliderNum] + 5 || sliderVal <= lastslider[sliderNum] - 5) {
  52.     Serial.print(sliderNum);
  53.     Serial.print(" - ");
  54.     Serial.println(1 - max(0, min(1, ((float)sliderVal - (float)slidermin) / ((float)slidermax - (float)slidermin))));
  55.     lastslider[sliderNum] = sliderVal;
  56.   }
  57. }
  58.  
  59. void doKnobs(int knobNum)
  60. {
  61.   state[knobNum] = digitalRead(outA[i]);
  62.   if (state[knobNum] != lasts[knobNum]) {
  63.  
  64.     Serial.print(knobNum);
  65.     Serial.print(" ");
  66.     Serial.print(values[knobNum]);
  67.     Serial.print(" ");
  68.  
  69.  
  70.     if (digitalRead(outB[knobNum]) != state[knobNum]) {
  71.       Serial.println(" Up");
  72.       values[knobNum] = min(127, values[knobNum] + 3);
  73.     } else {
  74.       Serial.println(" Down");
  75.       values[knobNum] = max(0, values[i] - 3);
  76.     }
  77.  
  78.     usbMIDI.sendControlChange(knobNum, values[knobNum], channel);
  79.   }
  80.   lasts[knobNum] = state[knobNum]; a
  81. }
View raw paste Reply