Arduino Joy stick

Hello!.. i trying to make a joystick work in games etc..
but it wont work even i have calibrated it etc
help?
Its a 3 axis analog joystick

And its flickering alot on the calibration menu and not reconicing in Farming sim

Video
Code:

//Arduino Joystick 2.0 Library, by MHeironimus (https://github.com/MHeironimus)
//Beginners Guide, by Daniel Cantore
//Example Code (Oct 2020), with in-depth commenting 

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

//Define and Allocate Input Pins to memorable names
#define joyX A0
#define joyZ A1
#define joyRY A3
#define joyButton1 2
//Initializing Axis as Integers, at a 0 default value
int zAxis_ = 0; 
int RxAxis_ = 0;  
int RyAxis_ = 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;


//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(0x15, JOYSTICK_TYPE_JOYSTICK, 1, 0,false,false,true,true,true,false,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);


  //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

RxAxis_ = analogRead(A0);  
RxAxis_ = map(RxAxis_,0, 1023, 0, 255);
 Joystick.setRxAxis(RxAxis_);  
  
zAxis_ = analogRead(A1);
 zAxis_ = map(zAxis_,0, 1023, 0, 255);
 Joystick.setZAxis(zAxis_);

 RyAxis_ = analogRead(A3);
 RyAxis_ = map(RyAxis_,0, 1023, 0, 255);
 Joystick.setRyAxis(RyAxis_);

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


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

Hi, @NorCraftGaming

Can you please post a circuit diagram of your project please?
Include power supplies, component names and pin labels.

Can you please post link to specs/data of your joystick.

Thanks.. Tom... :smiley: :+1: :coffee: :australia:

1 Like

Its a Arduino Leonardo with just a usb cable to pc..

pin A0 Is X Axis
pin A1 Is z Axis
pin A3 Is Ry Axis(Rotation)
And Digital input 2 is the button.. that is working atleast

But circuit diagram uhh.. how? paint? lol
And use This HERE To check the data on joystick all i can find

You just need to know how they are connected, so you can use Paint, Gimp, or even handwriting.

I think the variable register is not connected Vcc and GND.
You should connect each end of the variable register to Vcc and GND like below.

Aha... every Potensiometer is connected to GND and they are all connected to 5V :slight_smile:
and middle is connected to Analog inputs atleast.. I get it to work in like Assetto Corsa to drive a car doh so thats why i can't understand why i cant get it to work in a game like Farming sim...

Sorry. I want to know your circuit.

Also you can print Serial.print() to check analogRead's output before output joystick value.
I recommend this to check your circuit.

Changed up my code and now it works lol.. i dont know what i did but as i dont understand a thing of Arduino coding .. what ever :slight_smile:

:slight_smile:

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


#define joyX A0
#define joyY A1
#define joyZ A3
#define joyButton1 2
int xAxis_ = 0; 
int yAxis_ = 0;  
int zAxis_ = 0;

int lastButton1State = 0;




Joystick_ Joystick(0x03, 0x04, 1, 0,true,true,true,false,false,false,false,false,false,false,false);

const bool initAutoSendState = true;

void setup() {
    Serial.begin(9600);
  pinMode(joyButton1, INPUT_PULLUP);



  Joystick.begin();
}

void loop() {

  
xAxis_ = analogRead(A1);  
 Joystick.setXAxis(xAxis_);  
  
zAxis_ = analogRead(A3);
 Joystick.setZAxis(zAxis_);

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



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

delay(10);
}

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