Die Kombinierung eines Arduino Leonardo mit einer Joystick bibliothek und 2 bis 3 MCP23017

Einen Leonardo habe ich nicht und die Arduino Joystick Library von MHeironimus kenne ich nicht, daher sind meine Ausführungen rein theoretisch.

Bei der Joystick Library finde ich das Beispiel JoystickButton, und dort die Zeile

Joystick.setButton(index, currentButtonState);

Bei 32 Tasten müßte 'index' Werte von 0 bis 31 annehmen, also

Joystick.setButton(0, buttonState0);
...
Joystick.setButton(31, buttonState31);

Für den MCP23017 empfehle ich Dir die Adafruit_MCP23017_Arduino_Library. Die 32 Taster könntest Du an zwei ICs anschließen:

#include <Joystick.h>
Joystick_ Joystick;

#include <Wire.h>
#include <Adafruit_MCP23017.h>
Adafruit_MCP23017 mcp[2];

void mcpMode(const uint8_t pin, const uint8_t modus)
{
  uint8_t ic = pin / 16;
  if (modus == INPUT_PULLUP)
  {
    mcp[ic].pinMode(0x0F & pin, INPUT);
    mcp[ic].pullUp(0x0F & pin, HIGH);
  } else {
    mcp[ic].pinMode(0x0F & pin, modus);
  }
}

bool mcpRead(const uint8_t pin)
{
  return mcp[pin / 16].digitalRead(0x0F & pin);
}

void setup() {
  mcp[0].begin( );  // Adresse 0x20
  mcp[1].begin(1);  // Adresse 0x21
  for (byte pin = 0; pin < 32; pin++)
  {
    mcpMode(pin, INPUT_PULLUP);
  }
}

const byte ANZAHLTASTER = 32;
bool lastButtonState[ANZAHLTASTER] = {0};

void loop() {

  // Read pin values
  for (byte index = 0; index < ANZAHLTASTER; index++)
  {
    bool currentButtonState = mcpRead(index);
    if (currentButtonState != lastButtonState[index])
    {
      Joystick.setButton(index, currentButtonState);
      lastButtonState[index] = currentButtonState;
    }
  }

  delay(50);
}

Ohne Gewähr, ungetestet als Anregung.

Du könntest auch MCP23S17 für SPI in Erwägung ziehen.