Joystick deadzone and matrix buttons

Hello everyone, arduino beginner here!

Could use some guidance form my fs joystick project. Im making joystick and buttonbox but im having some difficulties to set a deadzone for my joystick. I made the joystick my self with 3D printer so it is little wobly and the potentiometers don't turn as much as normal analog tumbstick. There is 3 buttons in joystick that are working. I want also add 12 button for the buttonbox using matrix 4x3. Using arduino micro.

I made it so far that it is working but i wasn't able to include the deadzone, any tips for code? It isnt really working in game, hard to map when there is no deadzone. Calibrated it through w10 gaming controller setup.

Also could take tips for code to make 4x3 matrix base for buttonbox.

#include <Joystick.h>



#define joyX A2
#define joyY A3
#define joyRZ A0
#define joyThrottle A1
#define joyButton1 2
#define joyButton2 3
#define joyButton3 4
//#define numMatxButtons 12
//#define numRows 4
//#define numCols 3



int xAxis_ = 0;
int yAxis_ = 0;
int rzAxis_ = 0;
int throttle_ = 0;

//joystick buttons
  int lastButton1State = 0;
  int lastButton2State = 0;
  int lastButton3State = 0;



Joystick_ Joystick(0x12, JOYSTICK_TYPE_JOYSTICK, 15, 0,true,true,false,false,false,true,false,true,false,false,false);

const bool initAutoSendState = true;

void setup() {
  
  pinMode(joyButton1, INPUT_PULLUP);
  pinMode(joyButton2, INPUT_PULLUP);
  pinMode(joyButton3, INPUT_PULLUP);

  Joystick.begin();
}

void loop() {
  

  xAxis_ = analogRead(joyX);
  Joystick.setXAxis(xAxis_);

  yAxis_ = analogRead(joyY);
  Joystick.setYAxis(yAxis_);

  rzAxis_ = analogRead(joyRZ);
  rzAxis_ = map(rzAxis_,0,490,0,410);
  Joystick.setRzAxis(rzAxis_);

  throttle_ = analogRead(joyThrottle);
  throttle_ = map(throttle_,0,490,0,410);
  Joystick.setThrottle(throttle_);
  

  int currentButton1State = !digitalRead(joyButton1);
  if (currentButton1State != lastButton1State){
    Joystick.setButton(1, currentButton1State);
    lastButton1State = currentButton1State;
  }

    int currentButton2State = !digitalRead(joyButton2);
  if (currentButton2State != lastButton2State){
    Joystick.setButton(2, currentButton2State);
    lastButton2State = currentButton2State;
  }
    int currentButton3State = !digitalRead(joyButton3);
  if (currentButton3State != lastButton3State){
    Joystick.setButton(3, currentButton3State);
    lastButton3State = currentButton3State;
  }


delay(10);
}

What is input range that needs a dead zone?

How shoukd the deadzone work?

Hers assuming the input is 0..1023, and the output should start form zero at the ends of the deadzone and go to -255 on one end and 255 on the other:

  switch (inputValue) {
  case 0 ... 450 :
    outputVakue = map(inputValue, 0, 450, -255, 0);

    break;

  case 574 ... 1023 :
    outputVakue = map(inputValue, 574, 1023, 0, 255):

    break;

  default :
    outputVakue = 0;

    break;
  }

Figure that out and fix it so it does what you want.

a7

show which one

upd

#include <Joystick.h>

#define joyX A2
#define joyY A3
#define joyRZ A0
#define joyThrottle A1
#define joyButton1 2
#define joyButton2 3
#define joyButton3 4
//#define numMatxButtons 12
//#define numRows 4
//#define numCols 3
//joystick buttons
bool lastButton1State = 0;
bool lastButton2State = 0;
bool lastButton3State = 0;

Joystick_ Joystick(0x12, JOYSTICK_TYPE_JOYSTICK, 15, 0, true, true, false, false, false, true, false, true, false, false, false);

void setup() {
  pinMode(joyButton1, INPUT_PULLUP);
  pinMode(joyButton2, INPUT_PULLUP);
  pinMode(joyButton3, INPUT_PULLUP);

  Joystick.begin();
}

void loop() {
  int xA=analogRead(joyX) - 512;
  if(abs(xA)>20)Joystick.setXAxis(xA);   // with 40 deadzone
  Joystick.setYAxis(analogRead(joyY) - 512);
  Joystick.setRzAxis(analogRead(joyRZ) - 512);
  Joystick.setThrottle(analogRead(joyThrottle));
  Joystick.setButton(1, !digitalRead(joyButton1));
  Joystick.setButton(2, !digitalRead(joyButton2));
  Joystick.setButton(3, !digitalRead(joyButton3));

  delay(10);
}

JoyRZ and Joythrottle are the ones that need deadzone. The potentiometer input is 0..1023 but in joystick they dont go all the way there. so max and min are somewhere 260...680.

Joystick never returns in same position so it shoud have deadzone in values 430..480.

JoyRZ and joyThrottle inputs need deadzone. joyX and joyY works perfectly without.

where? for reference, i made for x and y middle position of joystick as 0, so each move from center increase the value in positive or negative direction. but throttle is normally 0 centered, have no negative range, is not it?
show joystick/potentiometer for RZ and Throttle.

So did you figure out the code posted in #2 and fix it so it does what you want?

What trouble are you having understanding it, or are we done with the deadzone problem?

a7