Simple Midi CC assignment question.

Status
Not open for further replies.

bobo

Member
Hi all,

I've got a very simple question that I'm struggling to find the answer too. I've probably already read it but I'm very new to code, so I probably misunderstood! I've built a simple midi controller, 8 analog pots and 6 buttons. All working ok, but I'd like to assign the pots to specific cc numbers. I can't figure out how to do this. I understand how to do it for the buttons on digital pins, but not the analog pots. I've put the code I've used below.

Sorry if this is very obvious! Again I'm new to this and trying to learn.

Thanks in advance.


Code:
#include <Bounce.h>

// define how many pots are active up to number of available analog inputs
#define analogInputs 8
// make arrays for input values and lagged input values
int inputAnalog[analogInputs];
int iAlag[analogInputs];
// make array of cc values
int ccValue[analogInputs];
// index variable for loop
int i;
int ccs[] = {1,11,7,30,25,64,3,4};

// cc values for buttons
int cc_off = 0;
int cc_on = 65;
int cc_super = 127;

// map buttons to cc for button
int cc0 = 51;
int cc1 = 52;
int cc2 = 53;
int cc3 = 54;
int cc4 = 55;
int cc5 = 56;


Bounce button0 = Bounce(0, 3);
Bounce button1 = Bounce(1, 3);
Bounce button2 = Bounce(2, 3);
Bounce button3 = Bounce(3, 3);
Bounce button4 = Bounce(4, 3);
Bounce button5 = Bounce(5, 3);






void setup() {
  // MIDI rate
  Serial.begin(31250);
  pinMode(0, INPUT_PULLUP);
  pinMode(1, INPUT_PULLUP);
  pinMode(2, INPUT_PULLUP);
  pinMode(3, INPUT_PULLUP);
  pinMode(4, INPUT_PULLUP);
  pinMode(5, INPUT_PULLUP);
  pinMode(6, INPUT_PULLUP);
  pinMode(7, INPUT_PULLUP);
  pinMode(8, INPUT_PULLUP);
  pinMode(9, INPUT_PULLUP);
  pinMode(10, INPUT_PULLUP);
  pinMode(11, INPUT_PULLUP);
}

void loop() {
  // loop trough active inputs for knobs
  for (i=0;i<analogInputs;i++){
    // read current value at i-th input
    inputAnalog[i] = analogRead(i);
    // if magnitude of difference is 8 or more...
    if (abs(inputAnalog[i] - iAlag[i]) > 7){
      // calc the CC value based on the raw value
      ccValue[i] = inputAnalog[i]/8;
      // send the MIDI
      usbMIDI.sendControlChange(i, ccValue[i], 1);
      // set raw reading to lagged array for next comparison
      iAlag[i] = inputAnalog[i];
    }
  delay(5); // limits MIDI messages to reasonable number
  }
  
  // Push Button code
  button0.update();
  button1.update();
  button2.update();
  button3.update();
  button4.update();
  button5.update();

 
   if (button0.fallingEdge())
  {
    usbMIDI.sendControlChange(cc0, cc_on, 3);
  }
  if (button1.fallingEdge())
  {
    usbMIDI.sendControlChange(cc1, cc_on, 3);
  }
  if (button2.fallingEdge())
  {
    usbMIDI.sendControlChange(cc2, cc_on, 3);
  }
  if (button3.fallingEdge())
  {
    usbMIDI.sendControlChange(cc3, cc_on, 3);
  }
  if (button4.fallingEdge())
  {
    usbMIDI.sendControlChange(cc4, cc_on, 3);
  }
  if (button5.fallingEdge())
  {
    usbMIDI.sendControlChange(cc5, cc_on, 3);
  }

   
  if (button0.risingEdge())
  {
    usbMIDI.sendControlChange(cc0, cc_off, 3);
  }
  if (button1.risingEdge())
  {
    usbMIDI.sendControlChange(cc1, cc_off, 3);
  }
  if (button2.risingEdge())
  {
    usbMIDI.sendControlChange(cc2, cc_off, 3);
  }
  if (button3.risingEdge())
  {
    usbMIDI.sendControlChange(cc3, cc_off, 3);
  }
  if (button4.risingEdge())
  {
    usbMIDI.sendControlChange(cc4, cc_off, 3);
  }if (button5.risingEdge())
  {
    usbMIDI.sendControlChange(cc5, cc_off, 3);
  }
    // MIDI Controllers should discard incoming MIDI messages.
  while (usbMIDI.read()) {
  }
}
 
I'd say your problem is here...
usbMIDI.sendControlChange(i, ccValue, 1);

You should be getting an indexed value from an array holding the CC assignments. ... I think it should be:
usbMIDI.sendControlChange(ccs, ccValue, 1);
 
Last edited:
Can I ask where this code is from... I searched the forum for "iAlag" and there are many instances with this error but strangely it seems to go unnoticed in many of the threads....

If there is a common source where people are finding this code I would like to see it fixed or the error notated if possible.
 
Last edited:
Ok looks like this is from Phillip Cunningham's github controller code and it survived my 2012 suggestions for adding proper hysteresis (albeit in amateurish code). ...that's why I recognise some of my code and comments... I was worried I was the source of the problem but I merely didn't see it either.... now it's all over the net.

Looks like someone added the ccs array, maybe when it got merged with the button code, but didn't actually add it to the send command.
 
Hi Oddson,

Yeah, originally I followed that code, but was getting some problems in Logic with it disconnecting. So I then just followed the bounce library guide and figured it out between the two, and a lot of reading, how to write the code I used. I think the code I used originally didn't have the bit I've put below on the end. Soon as I added that in it worked fine. As for the cc assignments, in all honesty I've read so much this last few week trying to learn this stuff, that I have no idea where I got it from! More than likely a thread somewhere on here. Sorry I can't be more accurate!

Thanks again for your help, so much to take in as a complete beginner, really appreciate you taking the time!

Code:
  while (usbMIDI.read()) {
  }
}
 
Status
Not open for further replies.
Back
Top