Can you make a keypad without a button matrix?

Hey guys,

I'm making a button box to connect via USB to my computer and I was wondering if it were possible to do this WITHOUT using a button matrix?

This is from the keypad exampels and shows a button matrix using rows and columns

const byte ROWS = 4; //four rows
const byte COLS = 3; //three columns
char keys[ROWS][COLS] = {
  {'1','2','3'},
  {'4','5','6'},
  {'7','8','9'},
  {'*','0','#'}
};
byte rowPins[ROWS] = {5, 4, 3, 2}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {8, 7, 6}; //connect to the column pinouts of the keypad

As an example I just want to assign 1 button to 1 pin like you would normally. Here is a bit from the "Button example".

const int buttonPin = 2;
int buttonState = 0;    

void setup() {
  pinMode(buttonPin, INPUT);
}
     
void loop() {
  buttonState = digitalRead(buttonPin)

}

Is there a way to do this, and have windows interpret it as a single button on a game controller?

I have used keypads that have a resistor ladder circuit connected to an Arduino analog pin. The analog value returned relates to the key being pressed. I think there were 16 possible keys. Using multiple keys at the same time was not possible nor desired in my program.

Paul

sonofeevil:
Is there a way to do this

Sure, there are any number of ways to do this, the simplest being connect each and every button to an individual pin.

However this...

, and have windows interpret it as a single button on a game controller?

...makes me think we are dealing with an XY Problem here.

How the keyboard is implemented on the Arduino (be that matrix or otherwise) has absolutely zero impact on how the computer will interpret it.

pcbbc:
However this…makes me think we are dealing with an XY Problem here.

You are entirely correct, I changed my approach earlier to using the joystick library.

In particular I’m using the JoystickButton example. Which seems like a far better way to do what I am after.

However a new problem I’ve run in to is using the analog pins A0-A5 whenever I choose one of those it always shows the button as being pressed constantly even with nothing connected.

#include <Joystick.h>

Joystick_ Joystick;

void setup() {
 // Initialize Button Pins
 pinMode(9, INPUT_PULLUP);
 pinMode(10, INPUT_PULLUP);
 pinMode(11, INPUT_PULLUP);
 pinMode(12, INPUT_PULLUP);
 pinMode(13, INPUT_PULLUP);
 pinMode(A0, INPUT_PULLUP);
 pinMode(A1, INPUT_PULLUP);
 pinMode(A2, INPUT_PULLUP);


 // Initialize Joystick Library
 Joystick.begin();
}

// Constant that maps the phyical pin to the joystick button.
const int pinToButtonMap = 9;

// Last state of the button
int lastButtonState[8] = {0,0,0,0,0,0,0,0};

void loop() {

 // Read pin values
 for (int index = 0; index < 8; index++)
 {
   int currentButtonState = !digitalRead(index + pinToButtonMap);
   if (currentButtonState != lastButtonState[index])
   {
     Joystick.setButton(index, currentButtonState);
     lastButtonState[index] = currentButtonState;
   }
 }

 delay(50);
}

Another approach would be to read all your button states into one register. Bit 0 = button 0, bit 1 = button 1....... That way you read all your button states with one instruction into one variable then use the results by testing bits.
If (button_state = 00001001) button 0 and 3 pressed do............ else or set up a switch function

code_user:
Another approach would be to read all your button states into one register. Bit 0 = button 0, bit 1 = button 1....... That way you read all your button states with one instruction into one variable then use the results by testing bits.
If (button_state = 00001001) button 0 and 3 pressed do............ else or set up a switch function

Thanks for the tip, I ended up using the joystick library, it had some nice provisions for individual buttons, ran into a new set of problems with rotary encoders. Don't suppose you've got any experience with those and could help? ha ha

Your welcome....... its a common method in Verilog FPGA programming. I only have reading experience with encoders but coincidentally I am working on converting a dead weather station to Arduino and after I get the wind speed rpm coded I have to code in the optical rotary encoder on the wind vane. I'll check in with you when I get to it and see where we are at. Take care.............