Help with programing a 4x4 button matrix

Im working on a new button box for a flight sim. Im using an arduino leonardo and a 4x4 matrix. I want to set it up as a joystick instead of a keypad.

Code compiles fine, uploads fine, it shows up in my device manager as "Arduino Leonardo" with the joystick icon. But when I open up the properties, it shows only 12 inputs (should be 16), and none of the inputs light up when any button is pressed.

Here's the code:

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

const byte ROWS = 4;
const byte COLS = 4;
char Keys [ROWS][COLS] = {
  {0,1,2,3},
  {4,5,6,7},
  {8,9,10,11},
  {12,13,14,15},
};
byte rowPins[ROWS] = {8,9,10,11};
byte colPins[COLS] = {2,3,4,5};
char customKey = 0;

Keypad customKeypad = Keypad( makeKeymap(Keys), rowPins, colPins, ROWS, COLS); 
Joystick_ Buttons(0x03,0x05,12,false,false,false,false,false,false,false,false,false,false,false);
void setup(){
  Serial.begin(9600);
  Buttons.begin();
}

void loop(){
  customKey = customKeypad.getKey();
  if (customKey){
    Serial.println(customKey);
    if(customKey == '0'){
      Buttons.pressButton(0);
      delay(100);
      Buttons.releaseButton(0);
    }
    if(customKey == '1'){
      Buttons.pressButton(1);
      delay(100);
      Buttons.releaseButton(1);
    }
    if(customKey == '2'){
      Buttons.pressButton(2);
      delay(100);
      Buttons.releaseButton(2);
    }
    if(customKey == '3'){
      Buttons.pressButton(3);
      delay(100);
      Buttons.releaseButton(3);
    }
    if(customKey == '4'){
      Buttons.pressButton(4);
      delay(100);
      Buttons.releaseButton(4);
    }
    if(customKey == '5'){
      Buttons.pressButton(5);
      delay(100);
      Buttons.releaseButton(5);
    }
    if(customKey == '6'){
      Buttons.pressButton(6);
      delay(100);
      Buttons.releaseButton(6);
    }
    if(customKey == '7'){
      Buttons.pressButton(7);
      delay(100);
      Buttons.releaseButton(7);
    }
    if(customKey == '8'){
      Buttons.pressButton(8);
      delay(100);
      Buttons.releaseButton(8);
    }
    if(customKey == '9'){
      Buttons.pressButton(9);
      delay(100);
      Buttons.releaseButton(9);
    }
    if(customKey == '10'){
      Buttons.pressButton(10);
      delay(100);
      Buttons.releaseButton(10);
    }
    if(customKey == '11'){
      Buttons.pressButton(11);
      delay(100);
      Buttons.releaseButton(11);
    }
    if(customKey == '12'){
      Buttons.pressButton(12);
      delay(100);
      Buttons.releaseButton(12);
    }
    if(customKey == '13'){
      Buttons.pressButton(13);
      delay(100);
      Buttons.releaseButton(14);
    }
    if(customKey == '14'){
      Buttons.pressButton(14);
      delay(100);
      Buttons.releaseButton(14);
    }
    if(customKey == '15'){
      Buttons.pressButton(15);
      delay(100);
      Buttons.releaseButton(15);
    }
  }
}

Any help/feedback would be great! Thanks!

You are looking for the character '0' here, and this won't work as a character.:

if(customKey == '15'){

Here, put the characters you want to test

char Keys [ROWS][COLS] = {
  {'A', 'B', 'C', 'D'},
// and so forth E, F, G, H
// and  I J K L and always like

  {'M', 'N',  'O', 'P'},
};

Too lazy and Lucy is sitting on my lap "helping" but mimic the syntax of your original, then test if the returned value is one of those characters.

if (customKey == 'M') {

The characters in your map don't matter, you could use abcdefghijklmnop or any characters you want, the point is each key returns one of them, and you test that to see what key it was.

On a twelve key keypad, ppl use '0', '1' and so forth, and put '#' and '*' in there in the right place, but they still have to test against the character, not the number:

if (customKey == '#') { // code for the hash mark key

Study the examples, get clear on the difference between a number 7 and a character '7'. You might run into the concept of the ASCII chart.

HTH

Oh, of course, now Lucy's off my lap worrying the SO about din-din,

a7

  • Not going to ask who Lucy is or what Lucy is.
    :scream_cat:

Lap fungus:

a7

2 Likes

I guess that's because of this line:

Joystick_ Buttons(0x03,0x05,12,false,false,false,false,false,false,false,false,false,false,false);

That got my available inputs up to 32 in the game controller properties window, but im still not getting any button pushes to register

Post. The. Code. We have no idea what's in front of you now.

a7

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

const byte ROWS = 4;
const byte COLS = 4;
char Keys [ROWS][COLS] = {
  {0,1,2,3},
  {4,5,6,7},
  {8,9,10,11},
  {12,13,14,15},
};
byte rowPins[ROWS] = {8,9,10,11};
byte colPins[COLS] = {2,3,4,5};
char customKey = 0;

Keypad customKeypad = Keypad( makeKeymap(Keys), rowPins, colPins, ROWS, COLS); 
Joystick_ Buttons(0x03,0x05,32,false,false,false,false,false,false,false,false,false,false,false);
void setup(){
  Serial.begin(9600);
  Buttons.begin();
}

void loop(){
  customKey = customKeypad.getKey();
  if (customKey){
    Serial.println(customKey);
    if(customKey == '0'){
      Buttons.pressButton(0);
      delay(100);
      Buttons.releaseButton(0);
    }
    if(customKey == '1'){
      Buttons.pressButton(1);
      delay(100);
      Buttons.releaseButton(1);
    }
    if(customKey == '2'){
      Buttons.pressButton(2);
      delay(100);
      Buttons.releaseButton(2);
    }
    if(customKey == '3'){
      Buttons.pressButton(3);
      delay(100);
      Buttons.releaseButton(3);
    }
    if(customKey == '4'){
      Buttons.pressButton(4);
      delay(100);
      Buttons.releaseButton(4);
    }
    if(customKey == '5'){
      Buttons.pressButton(5);
      delay(100);
      Buttons.releaseButton(5);
    }
    if(customKey == '6'){
      Buttons.pressButton(6);
      delay(100);
      Buttons.releaseButton(6);
    }
    if(customKey == '7'){
      Buttons.pressButton(7);
      delay(100);
      Buttons.releaseButton(7);
    }
    if(customKey == '8'){
      Buttons.pressButton(8);
      delay(100);
      Buttons.releaseButton(8);
    }
    if(customKey == '9'){
      Buttons.pressButton(9);
      delay(100);
      Buttons.releaseButton(9);
    }
    if(customKey == '10'){
      Buttons.pressButton(10);
      delay(100);
      Buttons.releaseButton(10);
    }
    if(customKey == '11'){
      Buttons.pressButton(11);
      delay(100);
      Buttons.releaseButton(11);
    }
    if(customKey == '12'){
      Buttons.pressButton(12);
      delay(100);
      Buttons.releaseButton(12);
    }
    if(customKey == '13'){
      Buttons.pressButton(13);
      delay(100);
      Buttons.releaseButton(14);
    }
    if(customKey == '14'){
      Buttons.pressButton(14);
      delay(100);
      Buttons.releaseButton(14);
    }
    if(customKey == '15'){
      Buttons.pressButton(15);
      delay(100);
      Buttons.releaseButton(15);
    }
  }
}

My bad, all i did was update the line PaulRB said to update, changing a "12" to a "32"

I think what was intended was to change 12 to 16. But I am not sure.

You can use character constants or regular numbers for the keymap, but not zero, as zero is the value returned when no key is pressed. A special value.

Character constants look like 'X', not 'XX'. One character in single quotes. You can't use '14' like a character.

Use

char Keys [ROWS][COLS] = {
  {'0','1','2','3'},
  {'4','5','6','7'},
  {'8','9','A','B'},
  {'C','D,'E','F'},
};

and your tests could look like

    if(customKey == '9'){
      Buttons.pressButton(9);
      delay(100);
      Buttons.releaseButton(9);
    }
    if(customKey == 'A'){
      Buttons.pressButton(10);
      delay(100);
      Buttons.releaseButton(10);
    }
    if(customKey == 'B'){

I or anyone can share a few tricks to cut down on the 16 if statements - they will work, so try to get them to do. Then we can make lotsa that code more compact.

Or just...

If you can make sense of this

char Keys [ROWS][COLS] = {
  {1, 2, 3, 4},
  {5, 6, 7, 8},
  {9, 10, 11, 12},
  {13, 14, 15, 16},
};

and this

void loop(){
  customKey = customKeypad.getKey();
  if (customKey) {
    customKey--;    // fix customKey to be 0 .. 15
    Buttons.pressButton(customKey);
    delay(100);
    Buttons.releaseButton(customKey);
  }
}

you will see that the keypad can return a number which if fixed can be used directly.

I can't test this just now, but I think it is correct or close enough so it will soon be fixed by someone.

And it is never too early to learn the difference between 7 and '7' and "7". Keep an eye out for them.

Welcome to computer programming.

HTH

a7

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.