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);
}