Hi, I have no knowledge in electronics or coding, I want to build a button box for a flight sim, I will be using a KeeYees Pro Micro, toggle switches that are ON-OFF-ON, ON-OFF and momentary push buttons. I made this diagram of a button matrix with diodes to prevent ghosting and modified the code from a tutorial on youtube. I would like to know if I need to change things. Thanks a lot for your help !
#include <Keypad.h>
#include <Joystick.h>
//DEFINITIONS
#define ENABLE_PULLUPS
#define NUMBUTTONS 23 //replace "?"with number of buttong you are using
#define NUMROWS 8 //replace "?" with number of rows you have
#define NUMCOLS 4 //replace "?" with number of columns you have
//BUTTON MATRIX
//first change number of rows and columns to match your button matrix,
//then replace all "?" with numbers (starting from 0)
byte buttons[NUMROWS][NUMCOLS] = {
{0, 1, 2, 3},
{4, 5, 6, 7},
{8, 9, 10, 11},
{12, 13, 14, 15},
{16, 17, 18, 19},
{20, 21, 22, 23},
{24, 25, 26, 27},
{28, 29, 30, 31}
};
//BUTTON MATRIX PART 2
byte rowPins[NUMROWS] = {9, 8, 7, 6, 5, 4, 3, 2}; //change "?" to the pins the rows of your button matrix are connected to
byte colPins[NUMCOLS] = {A0, A1, A2, A3}; //change "?" to the pins the rows of your button matrix are connected to
Keypad buttbx = Keypad( makeKeymap(buttons), rowPins, colPins, NUMROWS, NUMCOLS);
//JOYSTICK SETTINGS
Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID,
JOYSTICK_TYPE_JOYSTICK,
23, //number of buttons
0, //number of hat switches
//Set as many axis to "true" as you have potentiometers for
false, // y axis
false, // x axis
false, // z axis
false, // rx axis
false, // ry axis
false, // rz axis
false, // rudder
false, // throttle
false, // accelerator
false, // brake
false); // steering wheel
const int numReadings = 20;
int readings[numReadings]; // the readings from the analog input
int index = 0; // the index of the current reading
int total = 0; // the running total
int currentOutputLevel = 0;
void setup() {
Joystick.begin();
for (int thisReading = 0; thisReading < numReadings; thisReading++) {
readings[thisReading] = 0;
}
}
void loop() {
CheckAllButtons();
}
void CheckAllButtons(void) {
if (buttbx.getKeys())
{
for (int i = 0; i < LIST_MAX; i++)
{
if ( buttbx.key[i].stateChanged )
{
switch (buttbx.key[i].kstate) {
case PRESSED:
case HOLD:
Joystick.setButton(buttbx.key[i].kchar, 1);
break;
case RELEASED:
case IDLE:
Joystick.setButton(buttbx.key[i].kchar, 0);
break;
}
}
}
}
Serial.println("Done checking");
}