PC Game Controller Joystick- No negative Axis. -- Solved

Hi everyone,

I'm very new to the programming side of electronics and have been getting help from a friend to get the code for a Flight Sim joystick controller working. The issue is that when I operate the Joystick Axis (X, Y, or Z) it will not output the negative Axis value.

Absolutely any help or advice would be amazing. Below is the current code I'm using.

Edit** I am using a Leonardo Board


#include <Joystick.h>

// Constant that maps the physical pin to the joystick button.
const int button = 0;

// Constant that maps the physical analogue pin to the joystick axis
const int X = A0;
const int Y = A1;
const int Z = A2;
Joystick_ Joystick;

// Last state of the button
int lastButtonState[13] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};

void setup()
{
// Initialize Button Pins
pinMode(button, 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(12, INPUT_PULLUP);

// Initialise analogue inputs
pinMode(X, INPUT);
pinMode(Y, INPUT);
pinMode(Z, INPUT);

// Set analogue pullups
analogWrite(X, HIGH);
analogWrite(Y, HIGH);
analogWrite(Z, HIGH);

// Initialize Joystick Library
Joystick.begin();

// Set axis ranges
Joystick.setXAxisRange(-127, 127);
Joystick.setYAxisRange(-127, 127);
Joystick.setZAxisRange(-127, 127);
}

void loop()
{
// Read pin values
for (int index = 0; index < 13; index++)
{
int currentButtonState = !digitalRead(index + button);
if (currentButtonState != lastButtonState[index])
{
Joystick.setButton(index, currentButtonState);
lastButtonState[index] = currentButtonState;
}
}

int xAxisState = analogRead(X); // Reads the input from the analogue input
xAxisState -= 0;
Joystick.setXAxis(xAxisState); // Sets the x axis to the value of the analogue input, currently not outputting a negative axis value

int yAxisState = analogRead(Y);
yAxisState -= 0;
Joystick.setYAxis(yAxisState);

int zAxisState = analogRead(Z);
zAxisState -= 0;
Joystick.setZAxis(zAxisState);

delay(50);
}

Try getting rid of

  // Set axis ranges
  Joystick.setXAxisRange(-127, 127);
  Joystick.setYAxisRange(-127, 127);
  Joystick.setZAxisRange(-127, 127);

if I remember correctly that’s to state what the input range of the set?Axis() will be and not the output which is somewhat standard those days for HID devices and described in the descriptor

You probably could try to send directly the value between 0 and 1023 as you do (the default) and the library will map that in the HID to the standard joysticks min and max as defined in the code

#define JOYSTICK_AXIS_MAXIMUM 32767
#define JOYSTICK_SIMULATOR_MINIMUM -32767

Sadly that did not seem to help, all that did was lock the axis pointer towards the -x/-y location with minimal movement towards +x/+y

** Edit-

I have found the solution to the issue. The controller once programmed, required calibration to recognize the movement of the axis properly.

Thank you for your help though. I did learn a little more about programming because of it. :slight_smile:

Good!