Making a custom Midi Controller

Shouldn’t be that hard right?

These were my goals going into it, seemed pretty doable, as this was after the whole XY grid incident, I figured “why not, it’s just a bunch of values.”

I ended up hardcoding everything :(, I got humbled real fast, after beating my head against the problem for what felt like months, what you witness underneath is the hellspawn of a midi controller that erupted from my defeat. I’m being dramatic, its just not pretty code.


float AVG9 = 0; float prevpot9 = 0;
float AVG8 = 0; float prevpot8 = 0;
float AVG7 = 0; float prevpot7 = 0;
float AVG6 = 0; float prevpot6 = 0;

int thresh = 3;

int state0 = 1; int prevb0 = 0; int sticky0 = 1; int times0 = 0;
int state1 = 1; int prevb1 = 0; int sticky1 = 1; int times1 = 0;
int state2 = 1; int prevb2 = 0; int sticky2 = 1; int times2 = 0;
int state3 = 1; int prevb3 = 0; int sticky3 = 1; int times3 = 0;
int state4 = 1; int prevb4 = 0; int sticky4 = 1; int times4 = 0;
int state5 = 1; int prevb5 = 0; int sticky5 = 1; int times5 = 0; 
int state6 = 1; int prevb6 = 0; int sticky6 = 1; int times6 = 0; 
int state7 = 1; int prevb7 = 0; int sticky7 = 1; int times7 = 0;

void setup() {
  // analog 4 pot
  pinMode(A9, INPUT);   pinMode(A8, INPUT);  pinMode(A7, INPUT);  pinMode(A6, INPUT);
  // digital for button
  pinMode(0, INPUT);   pinMode(1, INPUT);  pinMode(2, INPUT);  pinMode(3, INPUT);  pinMode(4, INPUT);  pinMode(5, INPUT);  pinMode(6, INPUT);  pinMode(7, INPUT);

  Serial.begin(9500);
}

void loop() {

float pot9 = analogRead(A9);  float pot8 = analogRead(A8);  float pot7 = analogRead(A7);  float pot6 = analogRead(A6);

// smoothing
AVG9 = (pot9* 0.1) + (AVG9 * 0.9);   AVG8 = (pot8* 0.1) + (AVG8 * 0.9);  AVG7 = (pot7* 0.1) + (AVG7 * 0.9);   AVG6 = (pot6* 0.1) + (AVG6 * 0.9); 

if (abs(prevpot9 - AVG9) > thresh) {
        // float pog = map(AVG, 606.0, 953.0, 0.0, 127.0);
        Serial.println(AVG9);
        usbMIDI.sendControlChange(1, AVG9/8, 1);
        prevpot9 = AVG9;  } delay(1);
if (abs(prevpot8 - AVG8) > thresh) {
        // float pog = map(AVG, 606.0, 953.0, 0.0, 127.0);
        Serial.println(AVG8);
        usbMIDI.sendControlChange(2, AVG8/8, 1);
        prevpot8 = AVG8; } delay(1);
if (abs(prevpot7 - AVG7) > thresh) {
        // float pog = map(AVG, 606.0, 953.0, 0.0, 127.0);
        Serial.println(AVG7);
        usbMIDI.sendControlChange(3, AVG7/8, 1);
        prevpot7 = AVG7; }  delay(1);
if (abs(prevpot6 - AVG6) > thresh) {
        // float pog = map(AVG, 606.0, 953.0, 0.0, 127.0);
        Serial.println(AVG6);
        usbMIDI.sendControlChange(4, AVG6/8, 1);
        prevpot6 = AVG6; } delay(1);
state0 = digitalRead(0);  state1 = digitalRead(1);  state2 = digitalRead(2);  state3 = digitalRead(3);  
state4 = digitalRead(4);  state5 = digitalRead(5);  state6 = digitalRead(6);  state7 = digitalRead(7);
 // button moment
if (state0 != prevb0) {
    prevb0 = state0;
    times0 = times0 + 1;
    if(times0 % 2 == 0){
      if(sticky0 == 1){sticky0 = sticky0 - 1;}
      else if(sticky0 == 0){sticky0 = sticky0 + 1;} }
    Serial.println(sticky0);
    Serial.println(times0);
    float map0 = map(sticky0, 0, 1, 0, 1024);
    usbMIDI.sendControlChange(5, map0, 1); //control, value, channel
                                             // 8 buttons, 4 pots
  } delay(1);
if (state1 != prevb1) {
    prevb1 = state1;
    times1 = times1 + 1;
    if(times1 % 2 == 0){
      if(sticky1 == 1){sticky1 = sticky1 - 1;}
      else if(sticky1 == 0){sticky1 = sticky1 + 1;}  }
    Serial.println(sticky1);
    Serial.println(times1);
    float map1 = map(sticky1, 0, 1, 0, 1024);
    usbMIDI.sendControlChange(6, map1, 1); //control, value, channel
                                             // 8 buttons, 4 pots
  } delay(1);
if (state2 != prevb2) {
    prevb2 = state2;
    times2 = times2 + 1;
    if(times2 % 2 == 0){
      if(sticky2 == 1){sticky2 = sticky2 - 1;}
      else if(sticky2 == 0){sticky2 = sticky2 + 1;}  }
    Serial.println(sticky2);
    Serial.println(times2);
    float map2 = map(sticky2, 0, 1, 0, 1024);
    usbMIDI.sendControlChange(7, map2, 1); //control, value, channel
                                             // 8 buttons, 4 pots
  } delay(1);
if (state3 != prevb3) {
    prevb3 = state3;
    times3 = times3 + 1;
    if(times3 % 2 == 0){
      if(sticky3 == 1){sticky3 = sticky3 - 1;}
      else if(sticky3 == 0){sticky3 = sticky3 + 1;}  }
    Serial.println(sticky3);
    Serial.println(times3);
    float map3 = map(sticky3, 0, 1, 0, 1024);
    usbMIDI.sendControlChange(8, map3, 1); //control, value, channel
                                             // 8 buttons, 4 pots
  } delay(1);
if (state4 != prevb4) {
    prevb4 = state4;
    times4 = times4 + 1;
    if(times4 % 2 == 0){
      if(sticky4 == 1){sticky4 = sticky4 - 1;}
      else if(sticky4 == 0){sticky4 = sticky4 + 1;}  }
    Serial.println(sticky4);
    Serial.println(times4);
    float map4 = map(sticky4, 0, 1, 0, 1024);
    usbMIDI.sendControlChange(9, map4, 1); //control, value, channel
                                             // 8 buttons, 4 pots
  } delay(1);
if (state5 != prevb5) {
    prevb5 = state5;
    times5 = times5 + 1;
    if(times5 % 2 == 0){
      if(sticky5 == 1){sticky5 = sticky5 - 1;}
      else if(sticky5 == 0){sticky5 = sticky5 + 1;}  }
    Serial.println(sticky5);
    Serial.println(times5);
    float map5 = map(sticky5, 0, 1, 0, 1024);
    usbMIDI.sendControlChange(10, map5, 1); //control, value, channel
                                             // 8 buttons, 4 pots
  } delay(1);
if (state6 != prevb6) {
    prevb6 = state6;
    times6 = times6 + 1;
    if(times6 % 2 == 0){
      if(sticky6 == 1){sticky6 = sticky6 - 1;}
      else if(sticky6 == 0){sticky6 = sticky6 + 1;}  }
    Serial.println(sticky6);
    Serial.println(times6);
    float map6 = map(sticky6, 0, 1, 0, 1024);
    usbMIDI.sendControlChange(11, map6, 1); //control, value, channel
                                             // 8 buttons, 4 pots
  } delay(1);
if (state7 != prevb7) {
    prevb7 = state7;
    times7 = times7 + 1;
    if(times7 % 2 == 0){
      if(sticky7 == 1){sticky7 = sticky7 - 1;}
      else if(sticky7 == 0){sticky7 = sticky7 + 1;}  }
    Serial.println(sticky7);
    Serial.println(times7);
    float map7 = map(sticky7, 0, 1, 0, 1024);
    usbMIDI.sendControlChange(12, map7, 1); //control, value, channel
                                             // 8 buttons, 4 pots
  } delay(1); 
}