Sending MidiNoteOn and PitchBend

So I am tyring to send a pitch bend at the same time as sending a MidiNoteOn. If I am not pressing the button that will send the midiNoteOn, and move the pitch bend potentiometer, it works I can see the number change in serial). however If I have the pitch bend bent and press the button, it seems to override the pitchbend - which no longer seems to get sent. Do I need to send them one after the other, or alternatively somehow? I am using the midi.h library

You set the range first, then you send the note on and then the bend message.

OK so I tried the same thing with modulation using this function (because the library doesnt have it):

void writeModulation(int val)

and it works - if I place it where I had pitch bending. So I am wondering whether the error that PaulS picked up on in another post is causing me the problem - the reason i say this is I have a piece of software that allows me to see the midi messages the computer is receiving. When I use the modulation wheel, it works, however the pitch bend suggests somethign else - a Poly note, whatever that is. So i was wondering if you could help me write a function like the above for Pitchbend. The reason I am struggling is because it wants two data bytes, for pitch bending, and Im not sure how to put the two together For channel 1, the first byte should be E0

11100000= E0= 224 Chan 1 Pitch Wheel Control Pitch Wheel LSB (0-127) Pitch Wheel MSB (0-127)

but the second two bits - I am not sure how to form them from a potentiometer value?

I am not sure how to form them from a potentiometer value?

A pot will only give you a 10 bit value, but the pitch bend needs two 7 bit values, that is 14 bits. Therefore you have to pack the 7 most significant bits of the pot into the MSB and the 3 least significant bits into the LSB if you want to get the full range from the pot.

MSB = (pot >> 3) & 0x7F;
LSB = (pot & 0x7) << 4;