Joystick as a controller


#1

Hi I’m new and i got a question: how to code my ps2 joystick for binding in games?for example axis x=w


#2

Hi

I’m no expert, but since nobody else has replied yet, here’s what I know.

You’re in luck because circuito.io has the PS2 joystick component, AND it has test code. See https://www.circuito.io/app?components=512,11021,611984 which is just an Arduino Uno and a PS2 joystick.

If you switch from the design to the code, you’ll see test code that shows the values from the joystick. The code uses a joystick library to make it simpler to get the X and Y position of the joystick.

Joystick joystick(JOYSTICK_PIN_VRX,JOYSTICK_PIN_VRY,JOYSTICK_PIN_SW);

after which you can get the X and Y position and whether the switch (button) is pressed with these statements:

int joystickX =  joystick.getX();
int joystickY =  joystick.getY();
int joystickSW =  joystick.getSW();

How you use the joystick position in a game is up to you. If you need help with that, please explain what your games does, or how you want joystick movement to affect the game.

Hope that helps
Bernhard


#3

game is about planes so everything that i need is wasd and im trying to add some buttons or switches, thanks here’s a link for similar thing and is it possible to do on arduino uno?


#4

Hi Klot

KSP FTW!

Yes, it’s possible to do this from an Arduino. You can pretend a key was pressed with the Arduino KeyboardWrite method: https://www.arduino.cc/en/Reference/KeyboardWrite

You can read more about the keyboard here: https://www.arduino.cc/reference/en/language/functions/usb/keyboard/

Your code would set limits on the joystick, and whenever the joystick X is less than a certain value, send A, to the right, send D, etc.

Is that enough for you to go on?