Forráskód Böngészése

Mux as object, not tested yet

oloturia 2 hónapja
szülő
commit
4b2b52c760

+ 174 - 0
mixxx_controller_with_mux/Controllers.cpp

@@ -0,0 +1,174 @@
+#include "Controllers.h"
+
+// MIDI
+
+// First parameter is the event type (0x09 = note on, 0x08 = note off).
+// Second parameter is note-on/note-off, combined with the channel.
+// Channel can be anything between 0-15. Typically reported to the user as 1-16.
+// Third parameter is the note number (48 = middle C).
+// Fourth parameter is the velocity (64 = normal, 127 = fastest).
+
+// First parameter is the event type (0x0B = control change).
+// Second parameter is the event type, combined with the channel.
+// Third parameter is the control number number (0-119).
+// Fourth parameter is the control value (0-127).
+
+void noteOn(byte channel, byte pitch, byte velocity) {
+  midiEventPacket_t noteOn = {0x09, 0x90 | channel, pitch, velocity};
+  #ifdef DEBUG
+    Serial.print("Note on:");
+    Serial.print(channel);
+    Serial.print(",");
+    Serial.print(pitch);
+    Serial.print(",");
+    Serial.println(velocity);
+  #endif
+  MidiUSB.sendMIDI(noteOn);
+}
+
+void noteOff(byte channel, byte pitch, byte velocity) {
+  midiEventPacket_t noteOff = {0x08, 0x80 | channel, pitch, velocity};
+   #ifdef DEBUG
+    Serial.print("Note off:");
+    Serial.print(channel);
+    Serial.print(",");
+    Serial.print(pitch);
+    Serial.print(",");
+    Serial.println(velocity);
+  #endif
+  MidiUSB.sendMIDI(noteOff);
+}
+
+void controlChange(byte channel, byte control, byte value) {
+  midiEventPacket_t event = {0x0B, 0xB0 | channel, control, value};
+  #ifdef DEBUG
+    Serial.print("Control change:");
+    Serial.print(channel);
+    Serial.print(",");
+    Serial.print(control);
+    Serial.print(",");
+    Serial.println(value);
+  #endif
+  MidiUSB.sendMIDI(event);
+}
+
+// Constructor and methods for analog controls and buttons (encoders use a different library)
+
+AnalogControl::AnalogControl(int pin, byte effect, int middle) {
+  this->pin = pin;
+  this->effect = effect;
+  this->middle = middle;
+  pinMode(pin,INPUT);
+}
+
+void AnalogControl::checkStatus() {
+  value = analogRead(pin);
+  if (value < 0+STICKYNESS){
+      value = 0;
+  } else if (value > 1023-STICKYNESS) {
+      value = 1023;
+  } else if (value > middle-STICKYNESS && value < middle+STICKYNESS) {
+      value = middle;
+  }
+  if ( abs(value - prev_value) > DEAD_ZONE ){
+    controlChange(1,effect, map(value,0,1023,0,127) );
+    prev_value = value;
+    MidiUSB.flush();
+  }
+}
+
+DigitalButton::DigitalButton(int pin, byte effect, byte effect_toggle, byte effect_long, byte effect_long_toggle) {
+  this->effect = effect;
+  this->effect_toggle = effect_toggle;
+  this->effect_long = effect_long;
+  this->effect_long_toggle = effect_long_toggle;
+  this->pin = pin;
+  pinMode(pin,INPUT_PULLUP);
+}
+
+
+void DigitalButton::checkStatus() {
+  if (digitalRead(pin) == LOW && pressed == false) {
+      pressed = true;
+      long_pressed = false;
+      debounce = millis();
+    }
+    if (pressed && (millis() - debounce <= 10) && digitalRead(pin) == HIGH) {
+      pressed = false;
+      return;
+    }
+    if (pressed && (millis() - debounce > 10) && digitalRead(pin) == HIGH) {
+        if (long_pressed == true) {
+          long_pressed = false;
+        } else if (effect_toggle == 0) {
+          controlChange(1,effect,1);
+        } else if (toggled) {
+          controlChange(1,effect,1);
+          toggled = false;
+        } else {
+          controlChange(1,effect_toggle,1);
+          toggled = true;
+        }
+      MidiUSB.flush();        
+      pressed = false;
+    }
+    if (pressed && (millis() - debounce > LONG_INTERVAL) && (digitalRead(pin) == LOW) && (long_pressed == false)) {
+      long_pressed = true;
+      if (effect_long_toggle == 0) {
+        controlChange(1,effect_long,1);
+      } else if (long_toggled) {
+        controlChange(1,effect_long,1);
+        long_toggled = false;     
+      } else {
+        controlChange(1,effect_long_toggle,1);
+        long_toggled = true;              
+      }
+      MidiUSB.flush();
+    }
+}
+
+Mux::Mux(int pinSIG, int pinS0, int pinS1, int pinS2, int pinS3, int effects[16], int mid_deadzone[16]) {
+  this->pinSIG = pinSIG;
+  this->pinS0 = pinS0;
+  this->pinS1 = pinS1;
+  this->pinS2 = pinS2;
+  this->pinS3 = pinS3;
+  pinMode(pinSIG,INPUT);
+  pinMode(pinS0,OUTPUT);
+  pinMode(pinS1,OUTPUT);
+  pinMode(pinS2,OUTPUT);
+  pinMode(pinS3,OUTPUT);
+  for (int i = 0; i < 16; i++) {
+    this->effects[i] = effects[i];
+    this->mid_deadzone[i] = effects[i];
+  }
+}
+
+void Mux::checkStatus(byte selector){
+  digitalWrite(pinS0, bitRead(selector,0));
+  digitalWrite(pinS1, bitRead(selector,1));
+  digitalWrite(pinS2, bitRead(selector,2));
+  digitalWrite(pinS3, bitRead(selector,3));  
+  int value = analogRead(pinSIG);
+
+  if (value < STICKYNESS) {
+    value = 0;
+  } else if (value > 1023-STICKYNESS) {
+    value = 1023;
+  } else if (value > mid_deadzone-STICKYNESS && value < mid_deadzone+STICKYNESS) {
+    value = mid_deadzone;
+  }
+
+  if (abs(value - prev_value[selector]) > DEAD_ZONE ) {
+    byte midi_value = map(value,0,1023,0,127);
+    controlChange(1,effects[selector],midi_value);
+    #ifdef DEBUG
+      Serial.print("Mux ");
+      Serial.print(effects[selector]);
+      Serial.print(", ");
+      Serial.println(value);
+    #endif
+    prev_value[selector] = value;
+    MidiUSB.flush();
+  }
+}

+ 75 - 0
mixxx_controller_with_mux/Controllers.h

@@ -0,0 +1,75 @@
+#ifndef CONTROLLERS_H
+#define CONTROLLERS_H
+#include <EncoderButton.h>
+#include "Arduino.h"
+#include "MIDIUSB.h"
+
+// Uncomment this line if you want pin and values printed on the serial, remember to open a console because it waits until a connection is established.
+//#define DEBUG
+
+// Analog controls (sliders and potentiometers), the deadzone is the smallest increment that the control must have to trigger the effect.
+#define DEAD_ZONE 15
+
+// Stickyness is a deazone that lies around extreme positions (0-1023) and the middle (511, but trimmable) of readings.
+#define STICKYNESS 30
+
+// Time, in ms, needed for a long press
+#define LONG_INTERVAL 1000
+
+void noteOn(byte channel, byte pitch, byte velocity);
+void noteOff(byte channel, byte pitch, byte velocity);
+void controlChange(byte channel, byte control, byte value);
+
+class AnalogControl {
+  private:
+    int pin;
+    byte effect;
+    int middle;
+    int value;
+    int prev_value;
+  public:
+    AnalogControl(int pin, byte effect, int middle);
+    void checkStatus();
+};
+
+class DigitalButton {
+  private:
+    int pin;
+    bool pressed;
+    long debounce;
+    long countdown;
+    byte effect;
+    byte effect_toggle;
+    bool toggled;
+    byte effect_long;
+    byte effect_long_toggle;
+    bool long_toggled;
+    bool long_pressed;
+  public:
+    DigitalButton(int pin, byte effect, byte effect_toggle, byte effect_long, byte effect_long_toggle);
+    void checkStatus();
+};
+
+
+struct RotaryEncoderControl {
+  EncoderButton rotary_encoder;
+  byte effect;
+};
+
+class Mux {
+  private:
+    int pinSIG;
+    int pinS0;
+    int pinS1;
+    int pinS2;
+    int pinS3;
+    int value[16];
+    int prev_value[16];
+    int effects[16];
+    int mid_deadzone[16];
+  public:
+    Mux(int pinSIG, int pinS0, int pinS1, int pinS2, int pinS3, int effects[16], int mid_deadzone[16]);
+    void checkStatus(byte selector);
+};
+
+#endif

+ 71 - 325
mixxx_controller_with_mux/mixxx_controller_with_mux.ino

@@ -1,6 +1,4 @@
-#include "Arduino.h"
-#include "MIDIUSB.h"
-#include <EncoderButton.h>
+#include "Controllers.h"
 
 /* This is a sketch that it transforms your Arduino Leonardo or Leonardo like (i.e. boards with atmega32u4) in a MIDI console for interfacing
  * with Mixxx (https://mixxx.org). Perhaps you can also use an UNO but you have to sacrifice some pins in order to add a MIDI interface to your Arduino.
@@ -12,219 +10,95 @@
  * This version allow the use of multiplexers for analog inputs, this feature is not tested yet.
  * If you have doubts, use mixxx_controller.ino instead.
  * 
- * Last revision 17-jan-2023
+ * Last revision 27-mar-2024
  */
 
-// Uncomment this line if you want pin and values printed on the serial, remember to open a console because it waits until a connection is established.
- #define DEBUG
-
-// Analog controls (sliders and potentiometers), the deadzone is the smallest increment that the control must have to trigger the effect.
-const int dead_zone = 10;
-
-// Stickyness is a deazone that lies around extreme positions (0-1023) and the middle (511, but trimmable) of readings.
-const int stickyness = 30;
-
-struct analogControl {
-  const int pin;
-  int value;
-  int prev_value;
-  const byte effect;
-  const int middle;
-};
+/* You can alter some library wide parameters in Controllers.h
+ * DEBUG enables serial printing of MIDI messages
+ * DEAD_ZONE contains the smallest increment that the control must have to trigger the effect.
+ * STICKYNESS is a deazone that lies around extreme positions (0-1023) and the middle of readings.
+ * TIME_INTERVAL is the time, in ms, needed for a long press
+*/
 
+// Pin definition
+// Analog knobs and sliders
 /* Connect linear potentiometers (sliders and knobs) to Analog Inputs on your board.
  * On Leonardo, from A0 to A5 are on the left (with USB on top)
- * A6, A7, A8, A9, A10 and A11 are digital pins:
- *  4,  6,  8,  9,  10,     12.
+ * A6, A7, A8, A9, A10, A11 are digital pins:
+ *  4,  6,  8,  9,  10,  12.
  *  
  * Change the first value accordingly, the last one is the MIDI effect that is sent to
  * Mixxx, every control needs to have a unique number.
+ * 
+ * MID-DEADZONE is the middle of the controller. Usually is 511, but it's trimmable.
 */
 
-//If you aren't using any analog controls, comment these lines.
-/*
-const int analog_controls_pins[] = {A0,A1,A2,A3,A4,A5,A7,A10,A11};
-const int analog_controls_effects[] = {7,8,9,1,2,3,4,5,6};
+// Define every control as AnalogControl(PIN,EFFECT,MID-DEADZONE)
+AnalogControl analog_controls[] = {
+//  AnalogControl(A0,7,520),
 
-analogControl analog_controls[] = {
-  analogControl {analog_controls_pins[0],0,0,analog_controls_effects[0],511},
-  analogControl {analog_controls_pins[1],0,0,analog_controls_effects[1],520},
-  analogControl {analog_controls_pins[2],0,0,analog_controls_effects[2],520},
-  
-  analogControl {analog_controls_pins[3],0,0,analog_controls_effects[3],511},
-  analogControl {analog_controls_pins[4],0,0,analog_controls_effects[4],511},
-  analogControl {analog_controls_pins[5],0,0,analog_controls_effects[5],511},
-  
-  analogControl {analog_controls_pins[6],0,0,analog_controls_effects[6],511},
-  analogControl {analog_controls_pins[7],0,0,analog_controls_effects[7],511},
-  analogControl {analog_controls_pins[8],0,0,analog_controls_effects[8],511},
 };
-const int active_analog_controls = sizeof(analog_controls)/sizeof(analog_controls[0]);
-*/
-//These lines disable analog controls if uncommented.
-analogControl analog_controls[] = {}; 
-const int active_analog_controls = 0;
 
+int active_analog_controls = sizeof(analog_controls)/sizeof(analog_controls[0]);
+
+// Rotary encoders
 /* Rotary Encorders need two pins. 
- * clk should be an interrupts. On Leonardo interrupts are on pin 0, 1, 2, 3 and 7.
+ * clk should be an interrupt. On Leonardo interrupts are on pin 0, 1, 2, 3 and 7.
  * dt are digital pins
- * effect_fd and effect_bk have to be unique
+ * effect and effect+1 must be unique
+ * the +1 is reserved for anticlockwise  
  */ 
-struct rotaryEncoder {
-  const int clk;
-  const int dt;
-  volatile int val;
-  volatile int val_changed;
-  EncoderButton eb;
-  const int effect_fd;
-  const int effect_bk;
-};
-
-//Comment these lines if you don't have any rotary encoders.
-const int rot_clk_pins[] = {2,3,0};
-const int rot_dt_pins[] = {4,5,1};
-const int rot_effect_fd[] = {10,12,18};
-const int rot_effect_bk[] = {11,13,19};
 
-rotaryEncoder rotary_encoders[] = {
- {rot_clk_pins[0],rot_dt_pins[0],0,false,EncoderButton (rot_clk_pins[0],rot_dt_pins[0]),rot_effect_fd[0],rot_effect_bk[0]},
- {rot_clk_pins[1],rot_dt_pins[1],0,false,EncoderButton (rot_clk_pins[1],rot_dt_pins[1]),rot_effect_fd[1],rot_effect_bk[1]},
- {rot_clk_pins[2],rot_dt_pins[2],0,false,EncoderButton (rot_clk_pins[2],rot_dt_pins[2]),rot_effect_fd[2],rot_effect_bk[2]},
+// Define every control as {EncoderButton(CLK,DT), EFFECT}
+RotaryEncoderControl rotary_encoders[] = {
+  //{EncoderButton(2,4),10},
+  
 };
-const int active_encoders = sizeof(rotary_encoders)/sizeof(rotary_encoders[1]);
 
-//These lines disable rotary encoders if uncommented.
-//rotaryEncoder rotary_encoders[] = {};
-//const int active_encoders = 0;
+const int active_encoders = sizeof(rotary_encoders)/sizeof(rotary_encoders[1]);
 
+// Digital buttons
 /* Digital buttons are connected to a digital pin. If you use toggle, long press or long press toggle rembember that the MIDI message byte has to be unique.
- * long_inteval is the interval of long clicks, toggle and long_toggle (for long presses) send two different messages for odd and even strokes.
- * If effect is 0 the toggle, long press or long press toggle is disabled. 
+ * toggle effect and long toggle effect (for long presses) send two different messages for odd and even strokes.
+ * If effect is 0 the toggle, long press or long press toggle is disabled.
  */
-const int long_interval = 1000;
-
-struct digitalButton  {
-  const int pin;
-  bool pressed;
-  long debounce;
-  long countdown;
-  const byte effect;
-  const byte effect_toggle;
-  bool toggled;
-  const byte effect_long;
-  const byte effect_long_toggle;
-  bool long_toggled;
-  bool long_pressed;
-};
 
+// Define every control as DigitalButton(PIN,PRESS EFFECT, PRESS TOGGLE EFFECT (0=disabled), LONG PRESS EFFECT, LONG PRESS TOGGLE EFFECT (0=disabled)
+DigitalButton buttons[] = {
+//  DigitalButton( 9,14,0, 0, 0),
 
-//Comment these lines if you don't use switches.
-const int button_pins[] = {9,11,8,7,13};
-const int button_effect[] = {14,15,16,17,20};
-const int button_effect_toggle[] = {0,0,0,0,0};
-const int button_effect_long[] {0,0,23,24,25};
-const int button_effect_toggle_long[] {0,0,0,0,26};
-digitalButton switches[] = {
-  {button_pins[0],false,0,0,button_effect[0],button_effect_toggle[0],false,button_effect_long[0],button_effect_toggle_long[0],false,false},
-  {button_pins[1],false,0,0,button_effect[1],button_effect_toggle[1],false,button_effect_long[1],button_effect_toggle_long[1],false,false},
-  {button_pins[2],false,0,0,button_effect[2],button_effect_toggle[2],false,button_effect_long[2],button_effect_toggle_long[2],false,false},
-  {button_pins[3],false,0,0,button_effect[3],button_effect_toggle[3],false,button_effect_long[3],button_effect_toggle_long[3],false,false},
-  {button_pins[4],false,0,0,button_effect[4],button_effect_toggle[4],false,button_effect_long[4],button_effect_toggle_long[4],false,false}
 };
-const int active_switches = sizeof(switches)/sizeof(switches[0]);
-
-//These lines disables digital buttons.
-//digitalButton switches[] = {};
-//const int active_switches = 0;
+const int active_buttons = sizeof(buttons)/sizeof(buttons[0]);
+
+//Callback function for the encoders
+void eb_Encoder(EncoderButton& eb) {
+ int inc = eb.increment();
+ if (inc > 0) {
+  controlChange(1,eb.userId()+1,1);
+ } else {
+  controlChange(1,eb.userId(),1);
+ }
+ MidiUSB.flush();
+}
 
 /* Multiplexers allow multiple analog inputs using only 1 analog input and 4 digital outputs.
  * pinSIG is the input pin for the signal, pinS0 to pinS3 are the digital outputs that select
  * the analog channel that will be connected with SIG.
  * 
  */
-struct mux{
-  const int pinSIG;
-  const int pinS0;
-  const int pinS1;
-  const int pinS2;
-  const int pinS3;
-};
 
-struct mux_selector{
-  const mux ph_mux;
-  const byte selector;
-  int value;
-  int prev_value;
-  const byte effect;
-  const int middle;
-};
+// Define every Multiplexer as 
+// (SIGNAL PIN, S0, S1, S2, S3, EFFECT (array of 16), MIDDLE DEADZONE (array of 16) ) 
 
-//every multiplexer needs 1 pin for reading the analog signal and 4 digital pins for selecting the channel
-const int mux_signal[] = {A0};
-const int mux_pins[][4] = {{4,5,6,7}};
+int mux0[16] = { 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25};
+int midzones0[16] = {520,520,520,520,520,520,520,520,520,520,520,520,520,520,520,520};
 
-mux muxes[] = {
-  {mux_signal[0],mux_pins[0][0],mux_pins[0][1],mux_pins[0][2],mux_pins[0][3]}
+Mux mux_inputs[] = {
+  Mux(A0, 4, 5, 6, 7, mux0, midzones0)
 };
-const int active_muxes = sizeof(muxes)/sizeof(muxes[0]);
-
-//for every multiplexer, up to 16 different analog controllers are allowed, remember that effects need to be unique
-const int selectors[] = {0,1,2,3,4,5,6,7,8};
-const int mux_effects[] = {7,8,9,1,2,3,4,5,6};
-const int mux_middle[] = {511,520,520,511,511,511,511,511};
-
-mux_selector mux_inputs[] = {
-  mux_selector {muxes[0], selectors[0], 0, 0,mux_effects[0],mux_middle[0]},
-  mux_selector {muxes[0], selectors[1], 0, 0,mux_effects[1],mux_middle[1]},
-  mux_selector {muxes[0], selectors[2], 0, 0,mux_effects[2],mux_middle[2]},
-  mux_selector {muxes[0], selectors[3], 0, 0,mux_effects[3],mux_middle[3]},
-  mux_selector {muxes[0], selectors[4], 0, 0,mux_effects[4],mux_middle[4]},
-  mux_selector {muxes[0], selectors[5], 0, 0,mux_effects[5],mux_middle[5]},
-  mux_selector {muxes[0], selectors[6], 0, 0,mux_effects[6],mux_middle[6]},
-  mux_selector {muxes[0], selectors[7], 0, 0,mux_effects[7],mux_middle[7]},
-  mux_selector {muxes[0], selectors[8], 0, 0,mux_effects[8],mux_middle[8]},
-};
-
-const int active_mux_inputs = sizeof(mux_selector)/sizeof(mux_selector[0]);
-
-
-// MIDI
-byte midi_value;
-
-// First parameter is the event type (0x09 = note on, 0x08 = note off).
-// Second parameter is note-on/note-off, combined with the channel.
-// Channel can be anything between 0-15. Typically reported to the user as 1-16.
-// Third parameter is the note number (48 = middle C).
-// Fourth parameter is the velocity (64 = normal, 127 = fastest).
-
-// First parameter is the event type (0x0B = control change).
-// Second parameter is the event type, combined with the channel.
-// Third parameter is the control number number (0-119).
-// Fourth parameter is the control value (0-127).
-
-void noteOn(byte channel, byte pitch, byte velocity) {
-  midiEventPacket_t noteOn = {0x09, 0x90 | channel, pitch, velocity};
-  MidiUSB.sendMIDI(noteOn);
-}
-
-void noteOff(byte channel, byte pitch, byte velocity) {
-  midiEventPacket_t noteOff = {0x08, 0x80 | channel, pitch, velocity};
-  MidiUSB.sendMIDI(noteOff);
-}
 
-void controlChange(byte channel, byte control, byte value) {
-  midiEventPacket_t event = {0x0B, 0xB0 | channel, control, value};
-  MidiUSB.sendMIDI(event);
-}
+const int active_mux_inputs = sizeof(mux_inputs)/sizeof(mux_inputs[0]);
 
-int eb_Encoder(EncoderButton& eb) {
-  for (int i = 0; i < active_encoders; i++) {
-    if (&eb == &rotary_encoders[i].eb){
-      rotary_encoders[i].val = eb.increment();
-      rotary_encoders[i].val_changed = true;
-    }
-  }
-}
 
 void setup() {
   #ifdef DEBUG
@@ -233,162 +107,34 @@ void setup() {
     Serial.println("RDY");
   #endif
 
-  for (int i = 0; i < active_muxes; i++) {
-    pinMode(muxes[i].pinS0,INPUT);
-    pinMode(muxes[i].pinS1,INPUT);
-    pinMode(muxes[i].pinS2,INPUT);
-    pinMode(muxes[i].pinS3,INPUT);
-  }
-
-  for (int i = 0; i < active_switches; i++) {
-    pinMode(switches[i].pin,INPUT_PULLUP);
-  }
-
   for (int i = 0; i < active_encoders; i++) {
-    rotary_encoders[i].eb.setEncoderHandler(eb_Encoder);
-  }
+    rotary_encoders[i].rotary_encoder.setUserId(rotary_encoders[i].effect);
+    rotary_encoders[i].rotary_encoder.setEncoderHandler(eb_Encoder);
+  }   
 }
 
 void loop() {
-  // Analog controls check.
+
+  // Analog controls check
   for (int i = 0; i < active_analog_controls; i++) {
-    analog_controls[i].value = analogRead(analog_controls[i].pin);
-    if (analog_controls[i].value < 0+stickyness){
-        analog_controls[i].value = 0;
-    } else if (analog_controls[i].value > 1023-stickyness) {
-        analog_controls[i].value = 1023;
-    } else if (analog_controls[i].value > analog_controls[i].middle-stickyness && analog_controls[i].value < analog_controls[i].middle+stickyness) {
-        analog_controls[i].value = analog_controls[i].middle;
-    }
-    if ( abs(analog_controls[i].value - analog_controls[i].prev_value) > dead_zone ){
-      midi_value = map(analog_controls[i].value,0,1023,0,127);
-      controlChange(1,analog_controls[i].effect,midi_value);
-      #ifdef DEBUG
-        Serial.print("Analog ");
-        Serial.print(analog_controls[i].effect);
-        Serial.print(",");      
-        Serial.println(analog_controls[i].value);
-      #endif
-      analog_controls[i].prev_value = analog_controls[i].value;
-      MidiUSB.flush();
-    }
+    analog_controls[i].checkStatus();
   }
 
-  // Analog signal from multiplexer check.
-  for (int i = 0; i < active_mux_inputs; i++) {
-    digitalWrite(mux_inputs[i].ph_mux.pinS0,bitRead(mux_inputs[i].selector,0));
-    digitalWrite(mux_inputs[i].ph_mux.pinS1,bitRead(mux_inputs[i].selector,1));
-    digitalWrite(mux_inputs[i].ph_mux.pinS2,bitRead(mux_inputs[i].selector,2));
-    digitalWrite(mux_inputs[i].ph_mux.pinS3,bitRead(mux_inputs[i].selector,3));
-    mux_inputs[i].value = analogRead(mux_inputs[i].ph_mux.pinSIG);
-    if (mux_inputs[i].value < 0+stickyness){
-      mux_inputs[i].value = 0;
-    } else if (mux_inputs[i].value > 1023-stickyness) {
-      mux_inputs[i].value = 1023;
-    } else if (mux_inputs[i].value > mux_inputs[i].middle-stickyness && mux_inputs[i].value < mux_inputs[i].middle+stickyness) {
-      mux_inputs[i].value = mux_inputs[i].middle;
-    }
-    if ( abs(mux_inputs[i].value - mux_inputs[i].prev_value) > dead_zone ){
-      midi_value = map(mux_inputs[i].value,0,1023,0,127);
-      controlChange(1,mux_inputs[i].effect,midi_value);
-      #ifdef DEBUG
-        Serial.print("Mux ");
-        Serial.print(mux_inputs[i].effect);
-        Serial.print(", ");
-        Serial.println(mux_inputs[i].value);
-      #endif
-      mux_inputs[i].prev_value = mux_inputs[i].value;
-      MidiUSB.flush();
-    }
-    
+  // Buttons check
+  for (int i = 0; i < active_buttons; i++) {
+    buttons[i].checkStatus();
   }
-  // Rotary encoders check.
+  
+  // Rotary encoders check
   for (int i = 0; i < active_encoders; i++) {
-    rotary_encoders[i].eb.update();  
-    if(rotary_encoders[i].val_changed) {
-      if(rotary_encoders[i].val < 0){
-        controlChange(1,rotary_encoders[i].effect_fd,1);
-      } else {
-        controlChange(1,rotary_encoders[i].effect_bk,1);
-      }
-      rotary_encoders[i].val_changed = false;
-      MidiUSB.flush();
-      #ifdef DEBUG
-        Serial.print(i);
-        Serial.print(" enc=");
-        Serial.println(rotary_encoders[i].val);
-      #endif
-    }
+    rotary_encoders[i].rotary_encoder.update();
   }
-
-  // Switches check.
-  for (int i = 0; i < active_switches; i++) {
-    if (digitalRead(switches[i].pin) == LOW && switches[i].pressed == false) {
-      switches[i].pressed = true;
-      switches[i].long_pressed = false;
-      switches[i].debounce = millis();
-      #ifdef DEBUG
-        Serial.print("Press SW ");
-        Serial.println(switches[i].pin);
-      #endif DEBUG
-    }
-    if (switches[i].pressed && (millis() - switches[i].debounce <= 10) && digitalRead(switches[i].pin) == HIGH) {
-      switches[i].pressed = false;
-      continue;
-    }
-    if (switches[i].pressed && (millis() - switches[i].debounce > 10) && digitalRead(switches[i].pin) == HIGH) {
-        if (switches[i].long_pressed == true) {
-          switches[i].long_pressed = false;
-        } else if (switches[i].effect_toggle == 0) {
-          controlChange(1,switches[i].effect,1);
-          #ifdef DEBUG
-            Serial.print("Release SW ");
-            Serial.println(switches[i].pin);
-          #endif
-        } else if (switches[i].toggled) {
-          controlChange(1,switches[i].effect,1);
-          switches[i].toggled = false;
-          #ifdef DEBUG
-            Serial.print("Release SW - toggle 0 ");
-            Serial.println(switches[i].pin);
-          #endif
-        } else {
-          controlChange(1,switches[i].effect_toggle,1);
-          switches[i].toggled = true;
-          #ifdef DEBUG
-            Serial.print("Release SW - toggle 1 ");
-            Serial.println(switches[i].pin);
-          #endif
-        }
-
-      MidiUSB.flush();        
-      switches[i].pressed = false;
-    }
-    if (switches[i].pressed && (millis() - switches[i].debounce > long_interval) && (digitalRead(switches[i].pin) == LOW) && (switches[i].long_pressed == false)) {
-      switches[i].long_pressed = true;
-      if (switches[i].effect_long_toggle == 0) {
-        controlChange(1,switches[i].effect_long,1);
-        #ifdef DEBUG
-          Serial.print("Long press SW ");
-          Serial.println(switches[i].pin);
-        #endif
-      } else if (switches[i].long_toggled) {
-        controlChange(1,switches[i].effect_long,1);
-        switches[i].long_toggled = false;
-        #ifdef DEBUG
-          Serial.print("Long press SW - toggle 0 ");
-          Serial.println(switches[i].pin);
-        #endif        
-      } else {
-        controlChange(1,switches[i].effect_long_toggle,1);
-        switches[i].long_toggled = true;
-        #ifdef DEBUG
-          Serial.print("Long press SW - toggle 1 ");
-          Serial.println(switches[i].pin);
-        #endif                
-      }
-      MidiUSB.flush();
-      
+  
+  // Analog signal from multiplexer check.
+  for (int i = 0; i < active_mux_inputs; i++) {
+    for (int act = 0; act < 16; act++) {
+      mux_inputs[i].checkStatus(act);
     }
   }
+ 
 }