getting MSB and LSB from midi CC

Hey all!
I'm trying to receive MSB and LSB from a midi device over 2 separate midi messages and i cant seem to figure out how to do this. the messages should come as follows

Message1:
0xb0, 0x00, MSB

Message2:
0xb0, 0x20, LSB

I cant seem to figure out how to first filter midi for only messages of this kind, then store each value.
any help would be appreciated.

What have you tried? Post your code that is not doing what you want (using code tags - read the sticky posts at the top of the forum) and explain what is happening vs. what you desire and people can help you.

This is what i've gotten, there probably is a better way to do this and i'm not entirely sure it works yet.

int checkMIDI(){
  byte msg1[6];
  byte velocity1;
  byte velocity2;
  int faderPosition;
    if (Serial.available()){
      Serial.readBytes(msg1,6);              //read bytes and send to array msg1, expecting a length of 6 but array is unlimited
        if ((msg1[0] == 0xb0) && (msg1[1] == 0x00)){ // take 1st MIDI message check if control change and 0x00
          byte velocity1 = msg1[2];                 //if passed, saves velocity stored in 3rd byte
        }
        if ((msg1[3] == 0xb0) && (msg1[4] == 0x20)){  // same process for 2nd MIDI message but with 0x20
          byte velocity2 = msg1[5];
        }
        int faderPosition = ((velocity1<<7) + velocity2); //int variable for MSB LSB addition
    }
    return faderPosition;                       // remember to initialize using variable assignment with function call
}

You are declaring local instances of velocity1, velocity2 and faderPosition inside your if() statements. These variables have nothing to do with the ones you declare at the top of the function (even though they have the same name).

You need to remove the 'int' in front when you are just doing the assignment.

Typically, when combining MSB + LSB, you shift MSB by 8 bits, not 7 (unless you know the upper bit is always 0)

blh64:
Typically, when combining MSB + LSB, you shift MSB by 8 bits, not 7 (unless you know the upper bit is always 0)

If I recall correctly, MIDI uses the lower 7 bits of a byte for the value and the top bit to indicate that there's more bytes (it encodes integers with a variable-length encoding, somewhat similar to UTF-8). That might be why they're using 7 rather than 8 bits to combine MSB and LSB.

Yes Chris midi uses 2x 7 bit messages in this case! I'm just wondering if theres a way to do this involving the arduino midi library rather than using the serial write, my only concern using serial.read like i am is if a host sends a message either longer or short than 3 bytes that will throw the whole thing out of whack,

I believe i can do something like :

if (MIDI.read()  && MIDI.getChannel() == 1 && MIDI.type == ControlChange && MIDI.getData1 == 0x00)
{ 
myMSB = MIDI.getData2 << 7
}

But then the question is how do I read the next line of midi while still in this function?
or do i just do:

if (MIDI.read()  && MIDI.getChannel() == 1 && MIDI.type == ControlChange && MIDI.getData1 == 0x00)
{ 
myMSB = MIDI.getData2 << 7;
} 
else if (MIDI.read()  && MIDI.getChannel() == 1 && MIDI.type == ControlChange && MIDI.getData1 == 0x20)
{ 
myLSB = MIDI.getData2;
} 

static int value = myMSB + myLSB;