Building a Button Box and can't quite figure out where I went wrong!

Hello everyone! I am Building a Button Box and can't quite figure out where I went wrong! Wiring or code or both! LUL, Below I will share wiring diagram and the code.

I am using a Arduino Micro.

15 push button momentary switches

2 on off toggles switches SPST

1 on off on SPDT

The switch used in the upper left is not a part the circuit with the Micro. It is a separate 12v circuit that only powers 2 LEDs. These are independent of everything else.

So the issue I am having is when I press any momentary switch on the top row they work in widows USB Game Controllers window. However all the rest of the switches repot on the same assigned button.

Not to sure how else to fix this issue. As you can see I am using the Matrix Method to program the buttons. If I need to use a different Arduino I can or what have I missed with the code or buttons.

Code.

#include <Joystick.h>
#include <Key.h>
#include <Keypad.h>

byte inputs[] = {A3, A4, A5, 7, 4, 6, 5, 11, A0, 12};
const int inCount = sizeof(inputs) / sizeof(inputs[0]);
byte outputs[] = {A1, 9, 3, 8, 10};
const int outCount = sizeof(outputs) / sizeof(outputs[0]);

#define NUMBUTTONS 19
#define NUMROWS 5
#define NUMCOLS 10

byte buttons[NUMROWS][NUMCOLS] = {
  {A1, 9, 3, 8, 10},
  {A3, A4, A5, 7, 4, 6, 5, 11, A0, 12},
};

byte rowPins[NUMROWS] = {A1, 9, 3, 8, 10};
byte colPins[NUMCOLS] = {A3, A4, A5, 7, 4, 6, 5, 11, A0, 12};

Keypad RevButtonBox1 = Keypad( makeKeymap(buttons), rowPins, colPins, NUMROWS, NUMCOLS);

Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID,
                   JOYSTICK_TYPE_JOYSTICK, 18, 0,
                   false, false, false, false, false, false,
                   false, false, false, false, false);

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


void loop() {
  CheckAllButtons();
  delay(0);
}


void CheckAllButtons(void) {
  if (RevButtonBox1.getKeys())
  {
    for (int i = 0; i < LIST_MAX; i++)
    {
      if ( RevButtonBox1.key[i].stateChanged )
      {
        switch (RevButtonBox1.key[i].kstate)
        {
          case PRESSED:
          case HOLD:
            Joystick.setButton(RevButtonBox1.key[i].kchar, 1);
            break;
          case RELEASED:
          case IDLE:
            Joystick.setButton(RevButtonBox1.key[i].kchar, 0);
            break;
        }
      }
    }

  }
}

Many thanks and any Help would be greatly appreciated!
Rev.

1 Like

Here is a picture of the final design.

1 Like

Wiring Diagram

1 Like

Hello and good morning
Did you run some tutorials to learn how the switch matrix works?

1 Like

I did but it has been a bit confusing for me.

Still confused on why this is not working! Do I need to ad a resistor to the switches? Any help would be appreciated!! Thanks!

Could it be that I am using a Micro?

You have not set up your input and output pins in setup{}

Going directly from idea to finished article may be OK for an expert, but if you dont understand how its working its better to begin simple and develop to the finished item.

What is the source of your code - did you write it yourself or from an online resource?
Why do you have 10 inputs and 5 outputs = 50 combinations?

Hey Thanks for your Reply John!

I wrote this from what I have learned thus far. This was designed with many combinations for future expansion switches.

Now with that said, I see how I totally missed the setup and I am now embarrassed! LUL

After correcting the code I am still having the same issue. Here is the code:

#include <Joystick.h>
#include <Key.h>
#include <Keypad.h>

byte inputs[] = {A3, A4, A5, 7, 4, 6, 5, 11, A0, 12};
const int inCount = sizeof(inputs) / sizeof(inputs[0]);
byte outputs[] = {A1, 9, 3, 8, 10};
const int outCount = sizeof(outputs) / sizeof(outputs[0]);

#define NUMBUTTONS 19
#define NUMROWS 5
#define NUMCOLS 10

byte buttons[NUMROWS][NUMCOLS] = {
  {A1, 9, 3, 8, 10},
  {A3, A4, A5, 7, 4, 6, 5, 11, A0, 12},
};

byte rowPins[NUMROWS] = {A1, 9, 3, 8, 10};
byte colPins[NUMCOLS] = {A3, A4, A5, 7, 4, 6, 5, 11, A0, 12};

Keypad RevButtonBox1 = Keypad( makeKeymap(buttons), rowPins, colPins, NUMROWS, NUMCOLS);

Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID,
                   JOYSTICK_TYPE_JOYSTICK, 18, 0,
                   false, false, false, false, false, false,
                   false, false, false, false, false);

void setup() { 
  Joystick.begin();
  
  pinMode(rowPins, INPUT);
  pinMode(colPins, OUTPUT);
}
void loop() {
  CheckAllButtons();
  delay(0);
}


void CheckAllButtons(void) {
  if (RevButtonBox1.getKeys())
  {
    for (int i = 0; i < LIST_MAX; i++)
    {
      if ( RevButtonBox1.key[i].stateChanged )
      {
        switch (RevButtonBox1.key[i].kstate)
        {
          case PRESSED:
          case HOLD:
            Joystick.setButton(RevButtonBox1.key[i].kchar, 1);
            break;
          case RELEASED:
          case IDLE:
            Joystick.setButton(RevButtonBox1.key[i].kchar, 0);
            break;
        }
      }
    }

  }
}

Many thanks!

How it works:
As I understand this page, the library does the pin setup for you.
It sets all column pins as INPUT_PULLUP which will make them all read HIGH.
The ROW pins are set as outputs, and each in turn is taken LOW.
If a switch connects a row to a column the reading for that COLUMN will be LOW when the corresponding ROW pin goes LOW.

Your setup() may be causing issues so I'd suggest you remove these two lines

 pinMode(rowPins, INPUT);
  pinMode(colPins, OUTPUT);

You need to simplify your program to check for issues.
I'd suggest you copy the code for the 4*4 keypad from the tutorial and see if it works on your setup - without changing your hardware.

Thanks John will give this a go and get back to you Thanks for pointing me in the right direction!!

Hello John,
Using the 4x4 keypad sketch had zero effect. No key presses worked in any application nor in calculator. In my sketch above, I can get the top row of buttons (Column pin 10, with Rows pins 6, 5, 11, 7, 4) working fine but everything below it will only return the same button press. (verified through Game Controllers windows setup) For example; when I press the first white key from the left it returns the 7th button on the Game Controllers matrix. Second white key will return the 6th button, third the 12th key etc. Yet any other key press from that row and below will return the 1st key! In the picture below.


My question would be if column 10 works with rows 6, 5, 11, 7 and 4, why would they not work with rows
9, 3, 8, A5, A4, A3, 12. I am not sure if Button one is correct for A0 and A1 but I am sure that is what it is supposed to be! I am also using this button box as a in game programable switch box and the simulator, unfortunately, will not read it as a controller without the joystick library.

Once again many thanks, Rev

You have a complex code and need to simplify to find the error. We dont know if that lies in your hardware connections, your code for the matrix, or the joystrick interface.

The first step is to test the hardware which you can do (to an extent) with the example code. We arent using the joystick interface at that point.
If you connect your button box to the arduino IDE to load the example program you can use the serial monitor to show the results. As in the example. If that works you can test the response to the different buttons.

void loop(){
  char key = keypad.getKey();

  if (key){
    Serial.println(key);
  }
}

Once you know that is working you can go on to find if the joystick interface to the PC has issues. Or perhaps you can also test that seperately using an example program.