Hi all,
We (a friend and I) are building a game controller. We want to use toggle switches to control games (because we will use toggle switches, when toggled, the 'key' keeps being pressed until released).
As for now, we have made a prototype setup with Arduino Leonardo and a 2x2 keyboard matrix.
The problem we are facing, is that when one button is pressed, other buttons don't work anymore.
For example: button 1 - 4 are working. But when button 1 is pressed, buttons 2 - 4 don't register anything anymore. This works for all button combinations.
Setup details
Arduino Leonardo (ATmega32u4-chip)
Solderless Breadboard with 4 buttons (with diodes preventing ghosting).
A 2 by 2 matrix, (two rows (digital input 3 and 4), two columns (digital input 6 and 7)).
Please see the attached picture for the setup.
Code
#include <Keypad.h>
#include <Keyboard.h>
const byte ROWS = 2; //two rows
const byte COLS = 2; //two columns
char keys[ROWS][COLS] = {
{'1','2'},
{'3','4'}
};
byte rowPins[ROWS] = {3, 4}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {6, 7}; //connect to the column pinouts of the keypad
//initialize an instance of class NewKeypad
Keypad customKeypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
void setup(){
Serial.begin(9600);
}
void loop(){
char customKey = customKeypad.getKey();
if (customKey){
//Keyboard.write("key");
Serial.println(customKey); }
if (customKey == '1'){
Keyboard.write ('A');
}
if (customKey == '2'){
Keyboard.write ('B');
}
if (customKey == '3'){
Keyboard.write ('C');
}
if (customKey == '4'){
Keyboard.write ('D');
}
}
Question
Can anybody help us to make our code work so that whilst one button is being pressed, a one-time signal will be forwarded to the pc (as keyboard write), another button can also be pressed and forward that respective keypress to the pc.