<SOLVED> Arduino Micro 2 analogs + 8 buttons ShiftIn + 9 buttons Dinputs

Hello.

I'm trying to make a gamepad with 2 analogs and 17 buttons using a leonardo/micro.

First 8 buttons are driven by 74HC165 using 4 pins.
The rest are driven by arduino´s digital inputs (4-button DPAD + 4 triggers and 1 back button)

I have very little knowledge about coding. I've used ARDUINO JOYSTICK LIBRARY v2 to come up with following code.
I have tried both examples of Gamepad and ShiftIn library and separatedly it works. I just cant figure out how to mix those two together.

Validating the code gives me this error (4th line from the bottom):

error: 'joystick' was not declared in this scope

   joystick.setButton(i, shift.state(i));            // just press or release the button 

   ^

exit status 1
'joystick' was not declared in this scope

I hope someone can give me some guidence.

link to library: 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.

code:

// The digital pins  are grounded when they are pressed.
//
// Pin 0 = UP               - BUTTON 14
// Pin 1 = DOWN          - BUTTON 15
// Pin 2 = LEFT             - BUTTON 16
// Pin 3 = RIGHT           - BUTTON 17
// Pin 4 = RTRIGGER1     - BUTTON 9
// Pin 5 = RTRIGGER2    - BUTTON 10
// Pin 6 = LTRIGGER2    - BUTTON 11
// Pin 7 = LTRIGGER1    - BUTTON 12
// Pin 9 = BACKBUTTON - BUTTON 13
//
// Pin 10 = 74HC165 - SCK / CP           8 buttons from 1-6 and select+start
// Pin 14 = 74HC165 - CE / CHIP ENABLE
// Pin 15 = 74HC165 - Q7 / MISO
// Pin 16 = 74HC165 - PL / LOAD
//
// Pin A0 = L ANALOG JOYSTICK x
// Pin A1 = L ANALOG JOYSTICK y
// Pin A2 = R ANALOG JOYSTICK x
// Pin A3 = R ANALOG JOYSTICK y
//--------------------------------------------------------------------

#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(16, 14, 15, 10);  // 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); 
}

joystick != Joystick

arduino_new:
joystick != Joystick

Thank you for your quick answer, but could you give me some more explanation please?

EDIT:

ok, I can see the difference... tried and it works...

Thanks!!

One more problem:

second analog stopped working in its full range motion.

it only works:

X axis - moving physically from center to left position showing in windows motion from 0 - 100%
Y axis - moving physically from center to bottom position showing in windows motion from 0 - 100%

(Ive tried calibrating it, yes)

in code same as analog 1...

any clues?

EDIT:

got it. Forgot to define range of second one.

Joystick.setRxAxisRange(-512, 512);
Joystick.setRyAxisRange(-512, 512);

SOLVED!

HonasV:
One more problem:

SOLVED!

I'm struggling to figure out this very problem, did you ever figure out the issue, I tried your code above, but I can not get it to work... Do you have completed code or any suggestions?

My problem is in this section:

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