Hello.
I am struggling for a few weeks now to code a 20 button series and to use a joystick library with a daisy-chained shift register to allow for that many inputs on an Arduino pro micro. (leonardo) to use as HID device.
Could anyone guide me or help me troubleshoot this code?
This is the code I was able to get so far.
#include <ShiftIn.h>
#include <Joystick.h>
ShiftIn<3> shift; // use one shift register
//Joystick_ joystick;
//Setting my Joystick Left MFCD with 20 buttons(Jostick ID"0x15, Type, N#of buttons, N#of Axis)
Joystick_ Joystick(0x15, JOYSTICK_TYPE_GAMEPAD, 20, 0, false, false, false, false, false,false,false,false,false,false,false);
void setup() {
// Initialize Button Pins
pinMode(8, INPUT_PULLUP);
pinMode(9, INPUT_PULLUP);
pinMode(10, INPUT_PULLUP);
pinMode(16, INPUT_PULLUP);
// declare pins: pLoadPin, clockEnablePin, dataPin, clockPin
shift.begin(8, 9, 10, 16); // set pins of the shift register
// Initialize Button Pins
// Initialize Joystick Library
Joystick.begin();
}
void loop() {
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(12, LOW);}
else
{Joystick.setButton(12, HIGH);}
if (digitalRead(5) == HIGH)
{Joystick.setButton(11, LOW);}
else
{Joystick.setButton(11, HIGH);}
if (digitalRead(6) == HIGH)
{Joystick.setButton(10, LOW);}
else
{Joystick.setButton(10, HIGH);}
if (digitalRead(7) == HIGH)
{Joystick.setButton(9, LOW);}
else
{Joystick.setButton(9, HIGH);}
if (digitalRead(8) == HIGH)
{Joystick.setButton(5, LOW);}
else
{Joystick.setButton(5, HIGH);}
if (digitalRead(9) == HIGH)
{Joystick.setButton(6, LOW);}
else
{Joystick.setButton(6, HIGH);}
if (digitalRead(10) == HIGH)
{Joystick.setButton(7, LOW);}
else
{Joystick.setButton(7, HIGH);}
if (digitalRead(11) == HIGH)
{Joystick.setButton(8, LOW);}
else
{Joystick.setButton(8, HIGH);}
if (digitalRead(12) == HIGH)
{Joystick.setButton(1, LOW);}
else
{Joystick.setButton(1, HIGH);}
if (digitalRead(13) == HIGH)
{Joystick.setButton(2, LOW);}
else
{Joystick.setButton(2, HIGH);}
if (digitalRead(14) == HIGH)
{Joystick.setButton(3, LOW);}
else
{Joystick.setButton(3, HIGH);}
if (digitalRead(15) == HIGH)
{Joystick.setButton(4, LOW);}
else
{Joystick.setButton(4, HIGH);
if (digitalRead(16) == HIGH)
{Joystick.setButton(17, LOW);}
else
{Joystick.setButton(17, HIGH);}
if (digitalRead(17) == HIGH)
{Joystick.setButton(18, LOW);}
else
{Joystick.setButton(18, HIGH);}
if (digitalRead(18) == HIGH)
{Joystick.setButton(19, LOW);}
else
{Joystick.setButton(19, HIGH);}
if (digitalRead(19) == HIGH)
{Joystick.setButton(20, LOW);}
else
{Joystick.setButton(9, HIGH);}
if (digitalRead(20) == HIGH)
{Joystick.setButton(10, LOW);}
else
{Joystick.setButton(10, 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);
}
I thank you in advance to help me troubleshoot this code.