Joystick gaming

Hey,
I'm trying to make a joystick to play flight games with(with arduino leonardo). I did a little research and found the Joystick 2.0 library from MHeironimus.

I then found a code and changed it up a bit to my preferences. Now I have a few questions. I succeeded to let the joysticks work in a steam game but if I turn the joystick one way, I'ts way more sensitive then the other way (this is with both axis). Then my second question is (maybe something stupid) but what is meant by Z axis? If I plug in my PlayStation controller it seems that one joystick is just x and y axis and the other is Z axis and Z rotation, on the code explanation there was also a Z axis and rotation so I programmed my second joystick as it too. I also would like to know what the difference is between a normal axis and rotation.

here is my code

//Arduino Joystick 2.0 Library, by MHeironimus (https://github.com/MHeironimus)

//Initial Definitions and Setup
//Libary Inclusion
#include <Joystick.h>

//Define and Allocate Input Pins to memorable names
#define joyX A0
#define joyY A1
#define joyZ A2
#define joyRz A3

#define joyButton1 9
#define joyButton2 8


//Initializing Axis as Integers, at a 0 default value
int xAxis_ = 0;
int yAxis_ = 0;
int zAxis_ = 0;
int RzAxis_ = 0;


//Setting up Buttons
//Updating a static variable gives greater stability than reading directly from the digital pin.
//Giving Default Values to the Buttons for later use
  int lastButton1State = 0;
  int lastButton2State = 0;


//Defining the Joystick
//The Joystick is defined in the following setup:
//Joystick(Joystick HID ID, Joystick Type, Button Count, Hat Switch Count, Include X, Include Y, Include Z, Include Rx, Include Ry, Include Rz, Include Rudder, Include Throttle, Include Accelerator, Include Brake, Include Steering
//Joystick HID ID: A Hex value identifier for HID Device Recognition (default: 0x03). DO NOT USE 0x01 or 0x02
//Joystick type: Define the type of joystick from the types supported. Types: DEFAULT Joystick (0x04 or JOYSTICK_TYPE_JOYSTICK), Gamepad (0x05 or JOYSTICK_TYPE_GAMEPAD), Multi-Axis Controller (0x08 or JOYSTICK_TYPE_MULTI_AXIS)
//Button Count: Number of Buttons shown to HID system (default: 32)
//Hat Switch Count: Number of Hat Switches, max 2. (default:2)
//Include X Axis: Determines whether the X axis is avalible for used by the HID system, defined as a bool value (default:true)
//Include Y Axis: Determines whether the Y axis is avalible for used by the HID system, defined as a bool value (default:true)
//Include Z Axis: Determines whether the Z axis is avalible for used by the HID system, defined as a bool value (default:true)
//Include Rx Axis: Determines whether the X Rotational axis is avalible for used by the HID system, defined as a bool value (default:true)
//Include Ry Axis: Determines whether the Y Rotational axis is avalible for used by the HID system, defined as a bool value (default:true)
//Include Rz Axis: Determines whether the Z Rotational axis is avalible for used by the HID system, defined as a bool value (default:true)
//Include Rudder: Determines whether a Rudder axis is avalible for used by the HID system, defined as a bool value (default:true)
//Include Throttle: Determines whether a Throttle axis is avalible for used by the HID system, defined as a bool value (default:true)
//Include Accelerator: Determines whether an Accelerator axis is avalible for used by the HID system, defined as a bool value (default:true)
//Include Brake: Determines whether a Brake axis is avalible for used by the HID system, defined as a bool value (default:true)
//Include Steering: Determines whether a Steering axis is avalible for used by the HID system, defined as a bool value (default:true)

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

//Set Auto Send State
//Enables Auto Sending, allowing the controller to send information to the HID system, rather than waiting to be asked.
const bool initAutoSendState = true;

void setup() {
  //Initialize Buttons
  //Buttons set up between Digital Pin and Ground, following pin allocations from earlier on
  pinMode(joyButton1, INPUT_PULLUP);
  pinMode(joyButton2, INPUT_PULLUP);

  //Start Joystick - Needed to start the Joystick function libary
  Joystick.begin();
}

void loop() {
  
  //Axis Reading during Runtime
  //Setting Read functions for each axis and parsing correctly. The X axis will be used as an example for explanation

  //Reading the X Axis analog pin to the xAxis_ variable for processing
  xAxis_ = analogRead(joyX);
  //Mapping the X Axis data from a 0-1023 to 0-255 range for a smoother action
  xAxis_ = map(xAxis_,0,903,0,255);
  //Set the Joystick X Axis value as the new, smoother, value
  Joystick.setXAxis(xAxis_);

  yAxis_ = analogRead(joyY);
  yAxis_ = map(yAxis_,0,903,0,255);
  Joystick.setYAxis(yAxis_);

  RzAxis_ = analogRead(joyRz);
  RzAxis_ = map(RzAxis_,0,1023,0,255);
  Joystick.setRzAxis(RzAxis_);

  zAxis_ = analogRead(joyZ);
  zAxis_ = map(zAxis_,0,1023,0,255);
  Joystick.setZAxis(zAxis_);
  


  
  //Button Reading during Runtime
  //Setting Read functions for each button, using a state value for memory. Button 1 will be used as an example for explanation

  //Reading the current Button digital pin to the Current Button State for processing
  int currentButton1State = !digitalRead(joyButton1);
  //If loop - Check that the button has actually changed.
  if (currentButton1State != lastButton1State){
    //If the button has changed, set the specified HID button to the Current Button State
    Joystick.setButton(0, currentButton1State);
    //Update the Stored Button State
    lastButton1State = currentButton1State;
  }

    int currentButton2State = !digitalRead(joyButton2);
  if (currentButton2State != lastButton2State){
    Joystick.setButton(1, currentButton2State);
    lastButton2State = currentButton2State;
  }
 

//Pole Delay/Debounce
//To reduce unessecary processing, the frequency of the reading loop is delayed. The value(in ms) can be changed to match requirement
delay(10);
}

Rotation is just that: "Real" joysticks (a.k.a. flight sticks) can often be twisted sideways in order to control yaw.

That's also the reason for the weird X/Y/Z/RZ configuration. On a joystick, you often have the two lateral axes for pitch and roll (X and Y), rotation of the stick for yaw (RZ), and a separate thrust lever (Z). The game pad with two analog sticks with two axis each, is a more recent (PS2-ish?) invention and many platforms still stick with the X/Y/Z/RZ terminology even when it doesn't match the actual hardware.

Regarding the asymmetric behavior, are you maybe using non-linear potentiometers?

I have no idea, of they are linear or non linear, it's this one I'm using Joystick module, I'm planning on taking that cap of and 3D printing a flight joystick on it.. thanks for the info, did I program my second joystick right by doing the z axis and z rotation then?
Update: I'm very confused, in windows control panel it looks like the joysticks are linear and just working fine, but I don't know for sure and I don't know either if windows is seeing the really high data like 1023 and really low like 0, is there a way to check if it's linear? It's hard to say if you just look at serial data in your serial monitor. But what I do see is that the data if it's 0 or something lower it's sent way quicker and with less delay in between, can this be the reason or is this just always.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.