Controlling joystick on x/input leo with ps/2 mouse

Hello All and Thankyou in advance for your time, I am Currently trying to get my leonardo w/Xinput library to receive input for either joystick from accompanying ps/2 library and mouse. I Can get both to work, but i cant quite figure out how to get them going together, I seriously appreciate your wisdom as i understand it takes years to get there, and im not trying to piggyback but i feel as if i need some hardened input as the forums i search arent really clicking with me.
Im using the example code from x/input gampad pins. and the example code from the ps/2 library, the librarys and there code follow accordingly


#include <XInput.h>

// Setup
const boolean UseLeftJoystick = false; // set to true to enable left joystick
const boolean InvertLeftYAxis = false; // set to true to use inverted left joy Y

const boolean UseRightJoystick = false; // set to true to enable right joystick
const boolean InvertRightYAxis = false; // set to true to use inverted right joy Y

const boolean UseTriggerButtons = true; // set to false if using analog triggers

const int ADC_Max = 1023; // 10 bit

// Joystick Pins
const int Pin_LeftJoyX = A0;
const int Pin_LeftJoyY = A1;
const int Pin_RightJoyX = A2;
const int Pin_RightJoyY = A3;

// Trigger Pins
const int Pin_TriggerL = A4;
const int Pin_TriggerR = A5;

// Button Pins
const int Pin_ButtonA = 0;
const int Pin_ButtonB = 1;
const int Pin_ButtonX = 2;
const int Pin_ButtonY = 3;

const int Pin_ButtonLB = 4;
const int Pin_ButtonRB = 5;

const int Pin_ButtonBack = 6;
const int Pin_ButtonStart = 7;

const int Pin_ButtonL3 = 8;
const int Pin_ButtonR3 = 9;

// Directional Pad Pins
const int Pin_DpadUp = 10;
const int Pin_DpadDown = 11;
const int Pin_DpadLeft = 12;
const int Pin_DpadRight = 13;

void setup() {
// If using buttons for the triggers, use internal pull-up resistors
if (UseTriggerButtons == true) {
pinMode(Pin_TriggerL, INPUT_PULLUP);
pinMode(Pin_TriggerR, INPUT_PULLUP);
// If using potentiometers for the triggers, set range
else {
XInput.setTriggerRange(0, ADC_Max);

// Set buttons as inputs, using internal pull-up resistors
pinMode(Pin_ButtonA, INPUT_PULLUP);
pinMode(Pin_ButtonB, INPUT_PULLUP);
pinMode(Pin_ButtonX, INPUT_PULLUP);
pinMode(Pin_ButtonY, INPUT_PULLUP);

pinMode(Pin_ButtonLB, INPUT_PULLUP);
pinMode(Pin_ButtonRB, INPUT_PULLUP);

pinMode(Pin_ButtonBack, INPUT_PULLUP);
pinMode(Pin_ButtonStart, INPUT_PULLUP);

pinMode(Pin_ButtonL3, INPUT_PULLUP);
pinMode(Pin_ButtonR3, INPUT_PULLUP);

pinMode(Pin_DpadUp, INPUT_PULLUP);
pinMode(Pin_DpadDown, INPUT_PULLUP);
pinMode(Pin_DpadLeft, INPUT_PULLUP);
pinMode(Pin_DpadRight, INPUT_PULLUP);

XInput.setJoystickRange(0, ADC_Max); // Set joystick range to the ADC
XInput.setAutoSend(false); // Wait for all controls before sending


void loop() {
// Read pin values and store in variables
// (Note the “!” to invert the state, because LOW = pressed)
boolean buttonA = !digitalRead(Pin_ButtonA);
boolean buttonB = !digitalRead(Pin_ButtonB);
boolean buttonX = !digitalRead(Pin_ButtonX);
boolean buttonY = !digitalRead(Pin_ButtonY);

boolean buttonLB = !digitalRead(Pin_ButtonLB);
boolean buttonRB = !digitalRead(Pin_ButtonRB);

boolean buttonBack = !digitalRead(Pin_ButtonBack);
boolean buttonStart = !digitalRead(Pin_ButtonStart);

boolean buttonL3 = !digitalRead(Pin_ButtonL3);
boolean buttonR3 = !digitalRead(Pin_ButtonR3);

boolean dpadUp = !digitalRead(Pin_DpadUp);
boolean dpadDown = !digitalRead(Pin_DpadDown);
boolean dpadLeft = !digitalRead(Pin_DpadLeft);
boolean dpadRight = !digitalRead(Pin_DpadRight);

// Set XInput buttons
XInput.setButton(BUTTON_A, buttonA);
XInput.setButton(BUTTON_B, buttonB);
XInput.setButton(BUTTON_X, buttonX);
XInput.setButton(BUTTON_Y, buttonY);

XInput.setButton(BUTTON_LB, buttonLB);
XInput.setButton(BUTTON_RB, buttonRB);

XInput.setButton(BUTTON_BACK, buttonBack);
XInput.setButton(BUTTON_START, buttonStart);

XInput.setButton(BUTTON_L3, buttonL3);
XInput.setButton(BUTTON_R3, buttonR3);

// Set XInput DPAD values
XInput.setDpad(dpadUp, dpadDown, dpadLeft, dpadRight);

// Set XInput trigger values
if (UseTriggerButtons == true) {
// Read trigger buttons
boolean triggerLeft = !digitalRead(Pin_TriggerL);
boolean triggerRight = !digitalRead(Pin_TriggerR);

// Set the triggers as if they were buttons
XInput.setButton(TRIGGER_LEFT, triggerLeft);
XInput.setButton(TRIGGER_RIGHT, triggerRight);
else {
// Read trigger potentiometer values
int triggerLeft = analogRead(Pin_TriggerL);
int triggerRight = analogRead(Pin_TriggerR);

// Set the trigger values as analog
XInput.setTrigger(TRIGGER_LEFT, triggerLeft);
XInput.setTrigger(TRIGGER_RIGHT, triggerRight);

// Set left joystick
if (UseLeftJoystick == true) {
int leftJoyX = analogRead(Pin_LeftJoyX);
int leftJoyY = analogRead(Pin_LeftJoyY);

// White lie here… most generic joysticks are typically
// inverted by default. If the “Invert” variable is false
// then we’ll take the opposite value with ‘not’ (!).
boolean invert = !InvertLeftYAxis;

XInput.setJoystickX(JOY_LEFT, leftJoyX);
XInput.setJoystickY(JOY_LEFT, leftJoyY, invert);

// Set right joystick
if (UseRightJoystick == true) {
int rightJoyX = analogRead(Pin_RightJoyX);
int rightJoyY = analogRead(Pin_RightJoyY);

boolean invert = !InvertRightYAxis;

XInput.setJoystickX(JOY_RIGHT, rightJoyX);
XInput.setJoystickY(JOY_RIGHT, rightJoyY, invert);

// Send control data to the computer

PS/2 Mouse

#include <PS2Mouse.h>
#define MOUSE_DATA 5
#define MOUSE_CLOCK 6



  • Setup
    void setup()


  • Main program loop
    void loop()
    int16_t data[3];;
    Serial.print(data[0]); // Status Byte
    Serial.print(data[1]); // X Movement Data
    Serial.print(data[2]); // Y Movement Data

Use some free pins for the mouse. Pin 5 and 6 are occupied by the other device.

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