I am having a small issue with my code. This works but one of my buttons are backwards (pin2). pin 2 is a hall effect sensor that needs to be open with magnet near and closed while magnet is away. pin 3 is a hall effect sensor working correctly. pin 4 is a button working correctly. If someone could help me fix it so all buttons work correctly. Thank you.
#include <Joystick.h>
Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID,JOYSTICK_TYPE_GAMEPAD,
3, 0, // Button Count, Hat Switch Count
false, false, false, // X and Y, but no Z Axis
false, false, false, // No Rx, Ry, or Rz
false, false, // No rudder or throttle
false, false, false); // No accelerator, brake, or steering
void setup() {
// Initialize Button Pins
pinMode(2, INPUT_PULLUP);
pinMode(3, INPUT_PULLUP);
pinMode(4, INPUT_PULLUP);
// Initialize Joystick Library
Joystick.begin();
}
// Constant that maps the phyical pin to the joystick button.
const int pinToButtonMap = 2;
// Last state of the button
int lastButtonState[3] = {0,0,0};
void loop() {
// Read pin values
for (int index = 0; index < 3; index++)
{
int currentButtonState = !digitalRead(index + pinToButtonMap);
if (currentButtonState != lastButtonState[index])
{
Joystick.setButton(index, currentButtonState);
lastButtonState[index] = currentButtonState;
}
}
delay(50);
}