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!