Using joystick in a game

Hello!

I am quite new to arduino, especially with joysticks. I just recently got my hands on the arduino leonardo board, so I tried to make a joystick for a flight simulator. I downloaded the joystick library from GitHub - MHeironimus/ArduinoJoystickLibrary: An Arduino library that adds one or more joysticks to the list of HID devices an Arduino Leonardo or Arduino Micro can support..

The joystick appears in the control panel, but only the buttons work, where the joystick doesn't. When I move the joystick nothing happens... When I look in the raw data that the program gives you(when you try to callibrate the game controller under the control panel) it gives me a static -32767 and -32767.

I have conected my joystick X axis to A0 and Y axis to A1 and the button to D2.

Here is the code:

/*
 * tinkerBOY's usb_gamepad v1.0
 * 
 */

#include <Joystick.h>

#define PINS 14
#define ENABLE_ANALOG1 false
int X1 = A0;
int Y1 = A1;

Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID, JOYSTICK_TYPE_GAMEPAD, PINS, 0, 
  true, true, false, false, false, false, false, false, false, false, false);

class CButton {
  public:
  int pin = NULL;
  int lastState = 0;
  
  CButton(int p) {
    pin = p;
  }
};

CButton Buttons[PINS] ={0,1,2,3,4,5,6,7,8,9,10,16,14,15};

void setup() {
  for(int i=0 ; i<PINS ;i++) {
    pinMode(Buttons[i].pin, INPUT_PULLUP);
  }

  Joystick.begin();
  if (ENABLE_ANALOG1) {
    Joystick.setXAxisRange(-512, 512);
    Joystick.setYAxisRange(-512, 512);
  }
}

void JButtonStates() {
  if (ENABLE_ANALOG1) {
    Joystick.setXAxis(analogRead(X1) - 512);
    Joystick.setYAxis(analogRead(Y1) - 512);
  }
  
  for (int i = 0; i < PINS; i++) {
    int currentState = !digitalRead(Buttons[i].pin);
    
    if (currentState != Buttons[i].lastState) {
      Joystick.setButton(i, currentState);
      Buttons[i].lastState = currentState;
    }
  }  
}

void loop() {
  JButtonStates();
  delay(50);
}

Thank you all in advance!

your (kinda useless) image

I have conected my joystick X axis to A0 and Y axis to A1 and the button to D2.

what about GND ?

I have connected GND to the joystick GND pin and the VCC to 5V.

Then please post detailed photographs of your wiring scheme.

just saw this

CButton Buttons[PINS] ={[color=red]0,1[/color],2,3,4,5,6,7,8,9,10,16,14,15};

--> can you try without using pin 0 and 1 ?

Hopefully this will help. Sorry for doing it in MS paint.

your image

I removed the 0,1 from the line CButton Buttons[PINS] ={0,1,2,3,4,5,6,7,8,9,10,16,14,15};. But i get an error.

(deleted)

Thank you!

The joystick now works! Thank you to everyone that replyed to my post.

Can't wait to use it in a game :slight_smile:

spycatcher2k:
Put the 0 and 1 back - the Leonardo does not use these pins for serial connection.

I read somewhere

Serial: 0 (RX) and 1 (TX). Used to receive (RX) and transmit (TX) TTL serial data using the ATmega32U4 hardware serial capability. Note that on the Leonardo, the Serial class refers to USB (CDC) communication; for TTL serial on pins 0 and 1, use the Serial1 class.

so indeed fine for CDC communication (but 0 and 1 still used for Serial connection on Serial1)