Send midi pitch bend with a joystick

hi everybody,

I try to send midi pitch bend with a joystick it moves but always down and i don’t understand why. At middle no moves it’s okay.

This is my code:

#include <MIDI.h>


MIDI_CREATE_DEFAULT_INSTANCE();

int lastAnalogValue;
void setup()
{
   Serial.begin(31250);
    MIDI.begin (MIDI_CHANNEL_OMNI);           
}

void loop()
{


  int analogValue = analogRead(A0);
  

  if ((analogValue-lastAnalogValue) > 1 || (analogValue-lastAnalogValue) < -1) 
  
  {

    if (analogValue != lastAnalogValue && analogValue > 516) {

      MIDI.sendPitchBend(analogValue*(-8), 1);
      lastAnalogValue = analogValue;
    }
  

    if (analogValue != lastAnalogValue && analogValue < 508) {

      MIDI.sendPitchBend(analogValue* (8) , 1);
      lastAnalogValue = analogValue;
    }
     }
     
      }

Verify first that your joystick does give you a full range between 0 and 1023 and 512 in the middle, when idle

If so then try this code

void loop() {
  int analogValue = analogRead(A0) - 512; // 0 at mid point
  if ( abs(analogValue-lastAnalogValue) > 1)  { // have we moved enough to avoid analog jitter?
    if ( abs(analogValue) > 4) { // are we out of the central dead zone?
      MIDI.sendPitchBend(8*analogValue, 1); // or -8 depending which way you want to go up and down 
      lastAnalogValue = analogValue;
    }
  }
}

Note that you can pass something between -8192 (maximum downwards bend) and 8191 (max upwards bend) and center value is 0. As we have a analogValue Between -512 and +511, multiplying by 8 just gives you half the range. You could multiply by 16 if you want

(Typed here from my mobile so totally untested)

1 Like

It works perfect and thank you the advice (multiply)

Good ~ have fun !!