Adding a dead zone to a analog imput on arduino Leonardo

Hey there!, good morning/night for all , i’m trying to make a custom joystick controller who is composed of 3 analog sticks (which i’m using from some old ps2/pc broken joysticks) , 1 potentiometer, and some switches , everything is working good, but i need to make a deadzone for the analog and potentiometer because with a minimal movement , just a little they lost the center, the sketch i’m doing is this , i’m using the joystick library from Mheironimus but i can’t find something to help me to do this, and i searched in google but no luck for me in my case, btw i’m a newbie on programing on arduino, so if someone knows the answer, can tell me the answer and the steps to do it so i can do it myself in the future? Greetings from Argentina!

#include <Joystick.h>




 
Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID,JOYSTICK_TYPE_GAMEPAD,
  16, 0,                  // Button Count, Hat Switch Count
  true, true, true,     // X and Y, Z Axis
  true, true, true,   //  Rx, Ry,  Rz
  false, true,          // rudder / throttle
  false, false, false);  // accelerator / brake /steering

void setup() {
  pinMode(0, INPUT_PULLUP);
  pinMode(1, INPUT_PULLUP);
  pinMode(2, INPUT_PULLUP);
  pinMode(3, INPUT_PULLUP);
  pinMode(4, INPUT_PULLUP);
  pinMode(5, INPUT_PULLUP);
  pinMode(6, INPUT_PULLUP);
  pinMode(7, INPUT_PULLUP);
  pinMode(8, INPUT_PULLUP);
  pinMode(9, INPUT_PULLUP);
  pinMode(10, INPUT_PULLUP);
  pinMode(11, INPUT_PULLUP);
  pinMode(13, INPUT_PULLUP);
  pinMode(14, INPUT_PULLUP);
  pinMode(15, INPUT_PULLUP);
  pinMode(16, INPUT_PULLUP);

Joystick.setXAxisRange(-127, 127);
Joystick.setYAxisRange(-127, 127);
Joystick.setZAxisRange(-127, 127);
Joystick.setRxAxisRange(-127, 127);
Joystick.setRyAxisRange(-127, 127);
Joystick.setRzAxisRange(-127, 127);
Joystick.setThrottleRange(-127, 127);
  // Initialize Joystick Library
  Joystick.begin();
}

// defining the total [#] of buttons and their pins
const int ButtonToPinMap[16] = {0,1,2,3,4,5,6,7,8,9,10,11,13,14,15,16};

int lastButtonState[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};

void loop() {

  for (int index = 0; index < 16; index++)
  {
    int currentButtonState = !digitalRead(ButtonToPinMap[index]);
    if (currentButtonState != lastButtonState[index])
    {
      Joystick.setButton(index, currentButtonState);
      lastButtonState[index] = currentButtonState;
    }
  }
Joystick.setXAxis(map(analogRead(A3),0,1023,-127,127));
Joystick.setYAxis(map(analogRead(A1),0,1023,-127,127));
Joystick.setZAxis(map(analogRead(A2),0,1023,-127,127));
Joystick.setRxAxis(map(analogRead(A0),0,1023,-127,127));
Joystick.setRyAxis(map(analogRead(A4),0,1023,-127,127));
Joystick.setRzAxis(map(analogRead(A5),0,1023,-127,127));
Joystick.setThrottle(map(analogRead(A11),0,1023,-127,127));

  delay(50);
}

Thanks you in advance

when you do Joystick.setXAxis(map(analogRead(A3),0,1023,-127,127));you are actually mapping the whole range from 0 to 1023 to -127… 127. So you don’t create an opportunity for a dead zone.

so you need to capture first the analog value and check if it’s within some arbitrary defined dead range. if it is, set the joystick value to 0 otherwise, let it work as expected.

const int deadRangeLow  = 512-50; // arbitrary range
const int deadRangeHigh = 512+50; // arbitrary range
int v = analogRead(A3); 
if ((v > deadRangeLow) && (v < deadRangeHigh)) Joystick.setXAxis(0); // deadzone around center value
else  Joystick.setXAxis(map(v,0,1023,-127,127));

proceed in the same way with the others potentiometers

Ummm… Doesn’t that mean the joystick will suddenly “jump” to +/- 12 when it gets out of the dead zone?

Maybe that’s what the OP wants, maybe not…

const int deadRangeLow  = 512-50; // arbitrary range
const int deadRangeHigh = 512+50; // arbitrary range
int v = analogRead(A3); 
if (v < deadRangeLow)
  Joystick.setXAxis(map(v,0,deadRangLow,-127,0));
else if (v > deadRangeHi)
{
  Joystick.setXAxis(map(v,deadRangHigh,1023,0,127));
}
else
{
  Joystick.setXAxis(0); // deadzone around center value
}

very fair point, that will make it smoother indeed.

Note that I took an arbitrary large dead zone - the center position in most joystick would be oscillating within ±2 around a mid point usually so the "jump" would be not really visible. (but that's not an excuse, your code is better).

testing both codes, and they're working great! Thanks a lot! much appreciated, i have one more question if both of you don't mind, may i ask where did you learn ? i would like to learn aswell

Again, thank you both, have a good week

(btw, i'm interested of learn programing, so this maybe can be a good start, Greetings)

Joystick.setXAxis(map(analogRead(A3),0,1023,-127,127));

Rather than directly using the built-in ‘map()’ function you could define a custom function:

int readAxis(byte pin)
{
  int v = analogRead(pin);

  if (v < deadRangeLow)
    return map(v ,0, deadRangeLow, -127, 0);

  if (v > deadRangeHi)
    return map(v, deadRangeHigh, 1023, 0, 127));

  return 0;
}

Then you can just use:

Joystick.setXAxis(readAxis(A3));

If you ever need to change the mapping you only need to do it in one place.

Kevert:
testing both codes, and they're working great! Thanks a lot! much appreciated, i have one more question if both of you don't mind, may i ask where did you learn ? i would like to learn aswell

35+ years of writing software in one form or another. Not been playing with Arduino for long though. :wink:

There are some great tutorials on here. Start with them, and start small. Work up to larger projects. It may seem boring, but it's the only way to learn anything in life.

Not criticising - But it seems a lot of users start by "copying and pasting" sketches. Nothing wrong with that per se, and it gets you started quickly. Unfortunately it also means you don't acquire the knowledge you need to make modifications. And a large sketch often appears too complex to understand for a beginner. It's a catch 22 situation.