Hi guys.
I'm building a midi controller and for that i built a midi joystick.

At the moment the serial monitor reports this for pitchbend when i have the joystick in neutral position after giving it a slight touch and go:

Code:
Joystick Pitchbend pitchraw=512
Joystick Pitchbend Pitch=16
Joystick Pitchbend pitchraw=511
Joystick Pitchbend Pitch=0
Joystick Pitchbend pitchraw=510
Joystick Pitchbend Pitch=-17
It doesn't really stay in 0.

How could i smooth it so that it stays in zero when released?

play with this?

Code:
 
   if (abs(pitch - pitchLag) > 0 && pitchUpdate > MIDIdelay ) {
This is my full code for the joystick:

Code:
// include the ResponsiveAnalogRead library for analog smoothing
#include <ResponsiveAnalogRead.h>
#include <Bounce.h>  // Bounce library makes button change detection easy
// ******CONSTANT VALUES******** - customize code behaviour here!


// SET THESE SIX VALUES!
const int pitchPin = 0; // PIN numbers ** MUST CHANGE!
const int modPin = 1;
const int modPin2 = 1;
const int pitchMaxRaw = 1015; // Max reading with full bend up... as raw 10-bit value
const int modMaxRaw = 1022; // Max reading full mod down


const int channel = 1; // MIDI channel
const int MIDIdelay = 5; // will update MIDI only if this many milliseconds have passed
//******VARIABLES***********
// data variables and a lagged copy to compare before updating MIDI value
int pitch;
int mod;
int mod2;
int pitchRaw;
int modRaw;
int modRaw2;
int pitchLag;
int modLag;
int modLag2;
elapsedMillis pitchUpdate;
elapsedMillis modUpdate;
elapsedMillis modUpdate2;


// ititialize the ReponsiveAnalogRead objects
ResponsiveAnalogRead readPitch = {pitchPin, true};
ResponsiveAnalogRead readMod = {modPin, true};
ResponsiveAnalogRead readMod2 = {modPin2, true};


void setup() {
  pinMode(1, INPUT_PULLUP);
  pinMode(2, INPUT_PULLUP);
 
}


void loop() 


{
 delay(100);


  getAnalogData();
  while (usbMIDI.read()) {
  }
}






//************ANALOG SECTION**************
void getAnalogData() {
  readPitch.update();
  if (readPitch.hasChanged()) {
    pitchRaw = readPitch.getValue();
    Serial.print("Joystick Pitchbend pitchraw=");
    Serial.println(pitchRaw);
    // remap to output data and send if changed and MIDIdelay has lapsed since last update for that parameter
    pitch = map(pitchRaw, 2, 1020, -8192, 8192);
    pitch = max(pitch, -8192); // need this now that the bottom isn't zero
    pitch = min(pitch, 8192); // cap to avoid overflow
    if (abs(pitch - pitchLag) > 0 && pitchUpdate > MIDIdelay ) {
      pitchLag = pitch;
      usbMIDI.sendPitchBend(pitch, channel);
      Serial.print("Joystick Pitchbend Pitch=");
      Serial.println(pitch);
      pitchUpdate = 0;
    }
  }




  readMod.update();
  if (readMod.hasChanged()) {
    modRaw = readMod.getValue();
    // remap to output data and send if changed and MIDIdelay has lapsed since last update for that parameter
    mod = map(modRaw, 516, 1022, 0, 127); // N.B. - map does not limit values
    mod = max(mod, 0); // minimum could overflow is modRaw is greater than calabrated -This way Y Center-Down will only send from 0 to 127 values
    mod = min(mod,127);
    if (mod != modLag && modUpdate > MIDIdelay ) { // resolution reduction should be enough wi
      modLag = mod;
      usbMIDI.sendControlChange(1, mod, channel); // CC = 1 is mod wheel in standard MIDI
      modUpdate = 0;
    }
  }
  readMod2.update();
  if (readMod2.hasChanged()) {
    modRaw2 = readMod2.getValue();
    // remap to output data and send if changed and MIDIdelay has lapsed since last update for that parameter
    mod2 = map(modRaw2, 3, 503, 0, 127); // N.B. - map does not limit values
    mod2 = max(mod2, 0); // minimum could overflow if modRaw2 is greater than calibrated
    mod2 = min(mod2, 127); // This way Y Center-Up will only send from 0 till 127 values
    mod = min(mod2,127);
    if (mod2 != modLag2 && modUpdate2 > MIDIdelay ) { // resolution reduction should be enough wi
      modLag2 = mod2;
      usbMIDI.sendControlChange(2, mod2, channel); // CC = 2 is Breath Control in standard MIDI -In this case it is 
      modUpdate2 = 0;
    }
  {
  }
  }
  }
The rest of the project (wrong thread name):
https://forum.pjrc.com/threads/44582...y-switch-State