using 74HC165 to allow 32 buttons for Joystick library

Hi guys i am new to arduino and want to use joystick library for my first project. However i am facing some issues with both the code and wiring. I am using 74HC165 Shift register so i can have more inputs.

The problem i am facing is that when i connect all my switches the game controller showed all 8 buttons are lighted up. The code i am using is a code that i found online but couldn’t fix the problem.

This is the code:

#include <ShiftIn.h>
#include <Joystick.h>

ShiftIn<1> shift; // use one shift register

//Joystick_ joystick;

Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID,JOYSTICK_TYPE_GAMEPAD,
17, 0, // Button Count, Hat Switch Count
true, true, false, // X and Y, but no Z Axis
true, true, false, // No Rx, Ry, or Rz
false, false, // No rudder or throttle
false, false, false); // No accelerator, brake, or steering

void setup() {

shift.begin(7, 4, 5, 6); // set pins of the shift register

// Initialize Button Pins
pinMode(0, INPUT_PULLUP);
pinMode(1, INPUT_PULLUP);
pinMode(2, INPUT_PULLUP);
pinMode(3, INPUT_PULLUP);
pinMode(4, INPUT_PULLUP);
pinMode(5, INPUT_PULLUP);
pinMode(6, INPUT_PULLUP);
pinMode(7, INPUT_PULLUP);
pinMode(9, INPUT_PULLUP);

// Initialize Joystick Library
Joystick.begin();
Joystick.setXAxisRange(-512, 512);
Joystick.setYAxisRange(-512, 512);
}

void loop() {

if (analogRead(A0) < 500)
{Joystick.setXAxis(analogRead(A0)-512);}
else if (analogRead(A0) > 524)
{Joystick.setXAxis(analogRead(A0)-511);}
else
{Joystick.setXAxis(0);}

if (analogRead(A1) < 500)
{Joystick.setYAxis(analogRead(A1)-512);}
else if (analogRead(A1) > 524)
{Joystick.setYAxis(analogRead(A1)-511);}
else
{Joystick.setYAxis(0);}

if (analogRead(A2) < 500)
{Joystick.setRxAxis(analogRead(A2)-512);}
else if (analogRead(A2) > 524)
{Joystick.setRxAxis(analogRead(A2)-511);}
else
{Joystick.setRxAxis(0);}

if (analogRead(A3) < 500)
{Joystick.setRyAxis(analogRead(A3)-512);}
else if (analogRead(A3) > 524)
{Joystick.setRyAxis(analogRead(A3)-511);}
else
{Joystick.setRyAxis(0);}

if (digitalRead(0) == HIGH)
{Joystick.setButton(13, LOW);}
else
{Joystick.setButton(13, HIGH);}

if (digitalRead(1) == HIGH)
{Joystick.setButton(14, LOW);}
else
{Joystick.setButton(14, HIGH);}

if (digitalRead(2) == HIGH)
{Joystick.setButton(15, LOW);}
else
{Joystick.setButton(15, HIGH);}

if (digitalRead(3) == HIGH)
{Joystick.setButton(16, LOW);}
else
{Joystick.setButton(16, HIGH);}

if (digitalRead(4) == HIGH)
{Joystick.setButton(8, LOW);}
else
{Joystick.setButton(8, HIGH);}

if (digitalRead(5) == HIGH)
{Joystick.setButton(9, LOW);}
else
{Joystick.setButton(9, HIGH);}

if (digitalRead(6) == HIGH)
{Joystick.setButton(10, LOW);}
else
{Joystick.setButton(10, HIGH);}

if (digitalRead(7) == HIGH)
{Joystick.setButton(11, LOW);}
else
{Joystick.setButton(11, HIGH);}

if (digitalRead(8) == HIGH)
{Joystick.setButton(12, LOW);}
else
{Joystick.setButton(12, HIGH);}

if (shift.update()) {
for (int i = 0; i < shift.getDataWidth(); i++)
Joystick.setButton(i, shift.state(i)); // just press or release the button
}
delay(10);
}

Please follow the advice on posting code in Read this before posting a programming question

In particular note the advice to Auto format code in the IDE and using code tags when posting code here

  pinMode(0, INPUT_PULLUP);
  pinMode(1, INPUT_PULLUP);

It is generally not a good idea to use pins 0 and 1 as they are used by the Serial interface

UKHeliBob:
It is generally not a good idea to use pins 0 and 1 as they are used by the Serial interface

As the OP is using the joystick library, hopefully they are using a leonardo, pro micro or other native-usb type of Arduino. So might be OK to use pins 0, 1.

PaulRB:
As the OP is using the joystick library, hopefully they are using a leonardo, pro micro or other native-usb type of Arduino. So might be OK to use pins 0, 1.

True

omegatigrr:
if (digitalRead(0) == HIGH)
{Joystick.setButton(13, LOW);}
else
{Joystick.setButton(13, HIGH);}

Shorter:

 Joystick.setButton(13, !digitalRead(0));