Hi I'm building a button box for sim racing with a Pro Micro and I need to use a port expander which I already have a couple of MCP23017 from another project.
I'm using this Joystick 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.
While the other project went well, this time it's giving me a problem.
Basically while the buttons wired directly to the arduino are working without issues, the one I wired to the MCP for testing it's like it's always being pressed.
I tried also not using pullup resistor without any improvement and since it's been a while from the last time I used arduino I a little lost and cannot figure out what I'm missing to make this work as expected.
I'll leave the code and some screenshots of the wiring and the windows controller settings
In the wiring screenshot are missing two additional buttons that are wired to the 4 and 5 pins, but they're working without issues.
Also I want to specify (Maybe it's a relevant thing or maybe not) that in the final version of this project the MCP pins will be wired to a buttons matrix while arduino pins will be wired to the encoders and potentiometers
Edit: I Tried it with my other project which involved the port expander with buttons and leds on it and works flawlessly.
#include <Adafruit_MCP23017.h>
#include <Joystick.h>
#define BRAKE A3
#define GEAR_UP 4
#define GEAR_DOWN 5
#define MCP_BUTTON1 15
Adafruit_MCP23017 mcp;
Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID, JOYSTICK_TYPE_MULTI_AXIS, 32, 0, false, false, false, false, false, false, false, false, false, true, true);
const bool initAutoSendState = true;
int lastGearUpState = 0;
int lastGearDownState = 0;
int lastButton1State = 0;
int brake_ = 0;
void setup() {
pinMode(GEAR_UP, INPUT_PULLUP);
pinMode(GEAR_DOWN, INPUT_PULLUP);
mcp.begin(0);
mcp.pinMode(MCP_BUTTON1, INPUT_PULLUP);
Joystick.begin();
}
void loop() {
brake_ = analogRead(BRAKE);
brake_ = map(brake_, 0, 1023, 0, 255);
Joystick.setBrake(brake_);
int currentLastGearUpState = !digitalRead(GEAR_UP);
if(currentLastGearUpState != lastGearUpState)
{
Joystick.setButton(0, currentLastGearUpState);
lastGearUpState = currentLastGearUpState;
}
int currentLastGearDownState = !digitalRead(GEAR_DOWN);
if(currentLastGearDownState != lastGearDownState)
{
Joystick.setButton(1, currentLastGearDownState);
lastGearDownState = currentLastGearDownState;
}
int currentLastButton1State = !mcp.digitalRead(MCP_BUTTON1);
if(currentLastButton1State != lastButton1State)
{
Joystick.setButton(2, currentLastButton1State);
lastButton1State = currentLastButton1State;
}
delay(10);
}
MCP23017 Datasheet: https://cdn-shop.adafruit.com/datasheets/mcp23017.pdf
Pins Connections:
-Arduino VCC to:
- MCP VDD (pin 9)
- RESET with 10k Ohm Resistor (pin 18)
-Arduino GND to:
- MicroSwitch Common Pin
- MCP VSS (pin 10)
*MCP Pins A0,A1,A2 for I2C address (Pins 15,16,17)
-Arduino Pin 2 (SDA) to:
- MCP SDA (Pin 13)
-Arduino Pin 3 (SCL) to:
- MCP SCL (Pin 12)
-MCP Pin GPB7 (Pin 8) to:
- MicroSwitch NO (Normally Open) pin