Pro micro/ sx1509 button box question

Hi, im building a little button box project that uses a pro micro and has 37 buttons and 4 rotaries. Instead of using 2 pro micros i bought a sx1509 expander, i can build and wire the box but am struggling with the code.
I planned on using a 6x7 matrix for the buttons, my question is how to code the expander so that when a button in the matrix is pressed the computer recognises this for e.g as a button press would in the joystick library? Hopefully that makes some sense, appreciate any help i have little experience with code, thanks.

Hi, looks like you skipped reading the forum instructions. Please go do that.

Hi, thanks for the reply, it was late last night when I posted that so I have probably written the question wrong. I haven't got any code written yet to post I have just been playing with the examples in the sparkfun sx1509 library and couldn't get it to work.

My question is is it possible to use a button matrix on the sx1509 with the pro micro and code it so that a button press will be recognised as a game controller button press that can be used to assign a key on the game?

Yes, that is possible.

If you still require assistance with coding, or hardware, see reply #2.
I pulled it up for you, in case you can't find it:

Ok thanks that's good to know, now I just need to try and figure out how to make it work, I did read the instructions but thought I posted in the correct section and didn't have any code to share yet and I know how to wire it so wasn't looking for any help there, many thanks anyways.

Sure, no problem. It does seem a mystery though, you say you know how to wire it, but the example sketches don't work. That seems odd, doesn't it?

I can wire a matrix and i can get the keypad example to work on the serial monitor it prints the keys what im pressing, what i cant do so far is to get it to work on the game if i try and assign a key it does nothing

Have you read the forum instructions that I linked to, yet?

Hi, yes read them

Did you miss the part about posting code?

You have an expander circuit so please post a wiring diagram of that too. Also requested in forum guidelines.

No I just didn't have any to post at the time, I was looking to see if it would work and then try and figure it out for myself but am totally useless at the code side of things, this is what I have so far

#include <Joystick.h>
#include <Wire.h>
#include <SparkFunSX1509.h>

const byte SX1509_ADDRESS = 0x3E;
SX1509 io;

#define KEY_ROWS 2
#define KEY_COLS 1

char keyMap[KEY_ROWS][KEY_COLS] = {
  { '1' },
  { '2' }
};

Joystick_ Joystick;

void setup() {
  Wire.begin();
  Joystick.begin();

  if (io.begin(SX1509_ADDRESS) == false) {
    Serial.println("Failed to communicate. Check wiring and address of SX1509.");
    while (1)
      ;  // If we fail to communicate, loop forever.
  }
  unsigned int sleepTime = 256;
  byte scanTime = 2;
  byte debounceTime = 1;
  io.keypad(KEY_ROWS, KEY_COLS, sleepTime, scanTime, debounceTime);
}

void loop() {
  unsigned int keyData = io.readKeypad();
  if (keyData != 0)  // If a key was pressed:
  {
    byte row = io.getRow(keyData);
    byte col = io.getCol(keyData);
    char key = keyMap[row][col];
    Joystick.setButton(KeyData, 1)
  }
}

the Joystick.setButton part at the bottom is wrong, I just don't know how to get a button press to show as a button press from a game controller

Thank you for the code. It's a start. We also need a wiring diagram of the expander and the matrix. Pen and paper is fine.

here's a rough sketch of how it will be wired, thanks for your help

Thanks, it's a step. However, it forces you to sit with pinout diagrams of the "EXPANDER" and the "PRO MICRO", so you can figure out what, for example, pin 4 on the expander corresponds to. A useful schematic has meaningful labels on the pins, like the I2C pins you labelled.

What happens when you run the code you posted?Does it print, "Failed to communicate. Check wiring and address of SX1509."?

The Pro Micro does not have pull up resistors on the I2C lines. Does your expander board have them?

I just don't know how to get a button press to show as a button press from a game controller

This is not how to give a problem report. We want to see what happened, not what didn't happen, or what you don't know what to do.

Please post links to all the hardware you have, the expander, the MCU board. It would be helpful if you could also post images of the switch matrix.

Sparkfun has a nice hookup guide for the SX1509 and their library.

https://learn.sparkfun.com/tutorials/sx1509-io-expander-breakout-hookup-guide

start with their example and test if you see button presses at all.

Hi, thanks for replying, i have done the keypad example and temporarily wired it to 2 buttons on a breadboard and it works, it shows the key pressed as 1 or 2 depending on what button was pressed, i can't understand how to make a button press be recognised by the game, if i open the windows test usb game controller nothings happening

It works as far as printing the keys pressed on the serial monitor

the API says:

remember, you get a char from your keypad matrix.

so as a first test you could do something like

if (key == '1') Joystick.setButton(1, 1) // press the joystick button 1

this should activate the joystick button 1

Thank you so much, that has made a button press be recognised in the game controller test as button 2 being pressed, if i change .setButton(1, 1) to .setButton(0, 1) it shows as button 1, it is staying on though even though i've released the button its showing as still pressed, thanks again for your help it's really appreciated, been trying to do this this for 4 days now

you can release your (second) button with setButton(1,0)

either by time (for example remember the millis an "delete" after for example 50ms) or if you get another value (also 0) from keyData.

That depends on what your button should do. It might be that a keyboard keystroke might be easier...

and don't get confused with the counting of your buttons ... the PC Window shows 1..32, the parameter takes 0..31 ... have fun :wink: