I have a little experience with coding and have made this compound scrip that takes a matrix button input (From a keypad) and outputs it to my pc in the form of a joystick button.
#include <Joystick.h>
#include <Keypad.h>
const byte ROWS = 4;
const byte COLS = 3;
char Keys[ROWS][COLS] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'A','B','C'}
};
byte rowPins[ROWS] = {3, 8, 7, 5};
byte colPins[COLS] = {4, 2, 6};
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 == '1'){
Buttons.pressButton(0);
delay(100);
Buttons.releaseButton(0);
}
if(customKey == '2'){
Buttons.pressButton(1);
delay(100);
Buttons.releaseButton(1);
}
if(customKey == '3'){
Buttons.pressButton(2);
delay(100);
Buttons.releaseButton(2);
}
if(customKey == '4'){
Buttons.pressButton(3);
delay(100);
Buttons.releaseButton(3);
}
if(customKey == '5'){
Buttons.pressButton(4);
delay(100);
Buttons.releaseButton(4);
}
if(customKey == '6'){
Buttons.pressButton(5);
delay(100);
Buttons.releaseButton(5);
}
if(customKey == '7'){
Buttons.pressButton(6);
delay(100);
Buttons.releaseButton(6);
}
if(customKey == '8'){
Buttons.pressButton(7);
delay(100);
Buttons.releaseButton(7);
}
if(customKey == '9'){
Buttons.pressButton(8);
delay(100);
Buttons.releaseButton(8);
}
if(customKey == 'A'){
Buttons.pressButton(9);
delay(100);
Buttons.releaseButton(9);
}
if(customKey == 'B'){
Buttons.pressButton(10);
delay(100);
Buttons.releaseButton(10);
}
if(customKey == 'C'){
Buttons.pressButton(11);
delay(100);
Buttons.releaseButton(11);
}
}
}
However to get it to work every time the board is reset to get it to work i need to comment out all the if statements and the Buttons.begin(), upload that, then uncomment the Buttons.begin() reupload and then finally uncomment the rest reupload and it works. As you can probably tell this is a horrid process but i have no idea why its happening and how to fix it. Any help would be appreciated.