Input on pullup pins not registering correctly

I’m attempting to build a Bluetooth arcade stick using an ESP32 and the Arduino IDE, using the following project as a basis. BleGamepad.cpp and BleGamepad.h have been modified in order to separate the axis and hat inputs, as I only need the DPAD and BUTTON functions. This is the code I’ve written so far:

/*
 * A simple sketch that maps a single pin on the ESP32 to a single button on the controller
 */

#include <BleGamepad.h>   // https://github.com/lemmingDev/ESP32-BLE-Gamepad

BleGamepad bleGamepad("Neo Geo X Stick", "SNK", 100);

int pins[8] = {15, 2, 0, 4, 16, 17, 5, 18};
byte i;

void setup() {
  for (i = 0; i < 8; i++) {
    pinMode(pins[i], INPUT_PULLUP);
  }
  bleGamepad.begin();
}

void loop() 
{
  if(bleGamepad.isConnected()) 
  {
    if(digitalRead(15) == LOW)
      {
        bleGamepad.press(BUTTON_1);
      }
    else
      {
        bleGamepad.release(BUTTON_1);
      }
    }
  {
    if(digitalRead(2) == LOW)
      {
        bleGamepad.press(BUTTON_2);
      }
    else
      {
        bleGamepad.release(BUTTON_2);
      }
    }
  {
    if(digitalRead(0) == LOW)
      {
        bleGamepad.press(BUTTON_3);
      }
    else
      {
        bleGamepad.release(BUTTON_3);
      }
    }
  {
    if(digitalRead(4) == LOW)
      {
        bleGamepad.press(BUTTON_4);
      }
    else
      {
        bleGamepad.release(BUTTON_4);
      }
  }
  {
    if(digitalRead(16) == LOW)
      {
       bleGamepad.setHat(DPAD_RIGHT);
      }
    else
      {
        bleGamepad.setHat(DPAD_CENTERED);
      }
}
 {
    if(digitalRead(17) == LOW)
      {
       bleGamepad.setHat(DPAD_LEFT);
      }
    else
      {
        bleGamepad.setHat(DPAD_CENTERED);
      }
}
 {
    if(digitalRead(5) == LOW)
      {
       bleGamepad.setHat(DPAD_UP);
      }
    else
      {
        bleGamepad.setHat(DPAD_CENTERED);
      }
}
 {
    if(digitalRead(18) == LOW)
      {
       bleGamepad.setHat(DPAD_DOWN);
      }
    else
      {
        bleGamepad.setHat(DPAD_CENTERED);
      }
}
}

The issue I’m having is that the DPAD inputs are not registering correctly; instead of a momentary press when pulled low, it will take a second or so for button to register as pressed and then flicker between high and low. The BUTTON inputs work fine and the DPAD input will also work fine as long as only one pin is assigned to a direction; as soon as I try adding another direction the issues occur. I suspect the issue might be related to me using multiple instances of bleGamepad.setHat(DPAD_CENTERED); as when I remove the else function, the buttons will all register fine, but will obviously stay selected until a different directional input is pulled low, however I don’t know what sort of workaround I could use so any help would be much appreciated.

You use

    if(digitalRead(15) == LOW)

Where is the debouncing logic? Every button dances up and down multiple times upon being activated.

There are two issue on your code:

All above issues will be solved easily if you use this button library

I realised that using bleGamepad.setHat(DPAD_CENTERED) on each pin would obviously always try to return the controller to the central position of the DPAD as it would be pulled high whenever any of the other directional input were low, which was the cause of the flickering mentioned. I fixed it using the following if/else construction - is there anything particularly wrong with this?

{
    if(digitalRead(16) == LOW)
      {
       bleGamepad.setHat(DPAD_RIGHT);
      }
    else if(digitalRead(17) == LOW)
      {
        bleGamepad.setHat(DPAD_LEFT);
      }
    else if(digitalRead(5) == LOW)
      {
        bleGamepad.setHat(DPAD_UP);
      }
    else if(digitalRead(18) == LOW)
      {
        bleGamepad.setHat(DPAD_DOWN);
      }
    else
      {
        bleGamepad.setHat(DPAD_CENTERED);
      }

I will look into debouncing in order to get the code working better, but my main goal for the time being was to have something that did what I intended it to, which I can then debug as necessary.

Does the code do what You want? If not, what differs?