Hi all, I'm having issues and I hope someone can assist. I want to create a relatively simple bluetooth button box for use with emulation, specifically light gun games. All I want is it to have directional inputs plus approx 5 buttons to do basic stuff like provide credits, start, escape from the game, etc.
I am using a Lolin32 lite board which has onboard battery charging & BT. I got this code from a YouTube video and have altered it slightly. I know nothing about Arduino coding so I'm not sure what's going on. After uploading the sketch to the board, it connects to my PC via BT no problem, but when shorting out a respective pin, there is no output. I would've expected something to type when testing in notepad.
I have also tested the GPIO pins using a multimeter and they are turning high with 3.3v so it seems to be working. Any help would be greatly appreciated as I'm tearing my hair out (what's left of it, lol). Code is below, thanks.
However if there is a different code which will serve me better, I'd love to hear it.
#define USE_NIMBLE
#include <BleKeyboard.h>
BleKeyboard bleKeyboard;
#define GP_UP 32
#define GP_DOWN 33
#define GP_LEFT 25
#define GP_RIGHT 26
#define GP_COIN 13
#define GP_START 15
#define GP_EXTRA 2
#define GP_RETURN 0
#define GP_ESCAPE 4
bool keyStates[9] = {false, false, false, false, false, false, false, false, false};
int keyPins[9] = {GP_UP, GP_DOWN, GP_LEFT, GP_RIGHT, GP_COIN, GP_START, GP_EXTRA, GP_RETURN, GP_ESCAPE};
uint8_t keyCodes[9] = {'KEY_UP_ARROW', 'KEY_DOWN_ARROW', 'KEY_LEFT_ARROW', 'KEY_RIGHT_ARROW', '5', '1', 'KEY_SPACE', 'KEY_RETURN', 'KEY_ESC'};
void setup() {
Serial.begin(115200);
Serial.println("Code running...");
setInputs();
bleKeyboard.begin();
}
bool connectNotificationSent = false;
void loop() {
int counter;
if(bleKeyboard.isConnected()) {
if (!connectNotificationSent) {
Serial.println("Code connected...");
connectNotificationSent = true;
}
for(counter = 0; counter < 9; counter ++){
handleButton(counter);
}
}
}
void setInputs() {
pinMode(GP_UP, INPUT_PULLUP);
pinMode(GP_DOWN, INPUT_PULLUP);
pinMode(GP_LEFT, INPUT_PULLUP);
pinMode(GP_RIGHT, INPUT_PULLUP);
pinMode(GP_COIN, INPUT_PULLUP);
pinMode(GP_START, INPUT_PULLUP);
pinMode(GP_EXTRA, INPUT_PULLUP);
pinMode(GP_RETURN, INPUT_PULLUP);
pinMode(GP_ESCAPE, INPUT_PULLUP);
}
void handleButton(int keyIndex){
// handle the button press
if (!digitalRead(keyPins[keyIndex])){
// button pressed
if (!keyStates[keyIndex]){
// key not currently pressed
keyStates[keyIndex] = true;
bleKeyboard.press(keyCodes[keyIndex]);
}
}
else {
// button not pressed
if (keyStates[keyIndex]){
// key currently pressed
keyStates[keyIndex] = false;
bleKeyboard.release(keyCodes[keyIndex]);
}
}
}