BLEKeyboard not debouncing?

z01ks001
Posts: 2
Joined: Sun Jun 09, 2024 5:08 pm

BLEKeyboard not debouncing?

Postby z01ks001 » Tue Dec 17, 2024 7:06 pm

I have a bunch of switches/buttons connected to my ESP32 and I'm using it with BLEKeyboard. Most of the buttons have the issue of sending too many keystrokes with one button press. So it seems like maybe the debounce is not working, although I assume that some debouncing should be built in to the BLEKeyboard library.

This happens only when I use the ESP32 as a bluetooth keyboard. I'm also using ezButton array and printing to the serial monitor, and there is no problem with multiple keystrokes. Here is the blekeyboard code:

Code: Select all

#define USE_NIMBLE
#include <BleKeyboard.h>
BleKeyboard bleKeyboard("Sweetbuttons", "z01ks", 100);

#define BUTTON_1_PIN 25
#define BUTTON_2_PIN 26
#define BUTTON_3_PIN 0
#define BUTTON_4_PIN 14
#define BUTTON_5_PIN 13
#define BUTTON_6_PIN 2
#define BUTTON_7_PIN 21
#define BUTTON_8_PIN 22
#define BUTTON_9_PIN 19
#define BUTTON_10_PIN 23
#define BUTTON_11_PIN 18
#define BUTTON_12_PIN 35
#define BUTTON_13_PIN 36
#define BUTTON_14_PIN 39

bool keyStates[14] = {false, false, false, false, false, false, false, false, false, false, false, false, false, false};
int keyPins[14] = {
  BUTTON_1_PIN, 
  BUTTON_2_PIN,
  BUTTON_3_PIN,
  BUTTON_4_PIN,
  BUTTON_5_PIN,
  BUTTON_6_PIN, 
  BUTTON_7_PIN,
  BUTTON_8_PIN,
  BUTTON_9_PIN,
  BUTTON_10_PIN,
  BUTTON_11_PIN,
  BUTTON_12_PIN,
  BUTTON_13_PIN,
  BUTTON_14_PIN
  };
uint8_t keyCodes[14] = {
  'b',//KEY_LEFT_SHIFT,
  'c',//KEY_LEFT_CTRL,
  'a',//'KEY_END',
  'u',
  'i',
  'o',
  'd',//KEY_LEFT_ALT,
  ' ',
  'p',
  'k',
  'l',
  'n',
  '[',
  ']'};

bool connectNotificationSent = false;


void setup() {
// analogReadResolution(12);
  pinMode(BUTTON_1_PIN, INPUT_PULLUP);
  pinMode(BUTTON_2_PIN, INPUT_PULLUP);
  pinMode(BUTTON_3_PIN, INPUT_PULLUP);
  pinMode(BUTTON_4_PIN, INPUT_PULLUP);
  pinMode(BUTTON_5_PIN, INPUT_PULLUP);
  pinMode(BUTTON_6_PIN, INPUT_PULLUP);
  pinMode(BUTTON_7_PIN, INPUT_PULLUP);
  pinMode(BUTTON_8_PIN, INPUT_PULLUP);
  pinMode(BUTTON_9_PIN, INPUT_PULLUP);
  pinMode(BUTTON_10_PIN, INPUT_PULLUP);
  pinMode(BUTTON_11_PIN, INPUT_PULLUP);
  pinMode(BUTTON_12_PIN, INPUT_PULLUP);
  pinMode(BUTTON_13_PIN, INPUT_PULLUP);
  pinMode(BUTTON_14_PIN, INPUT_PULLUP);
  bleKeyboard.begin();
  // Serial.begin(115200);
  // Serial.println();

}

void loop() {
  static int lastCounter = 0;
  int counter;

  if(bleKeyboard.isConnected()) {
  if (!connectNotificationSent) {
    Serial.println("Code connected...");
    connectNotificationSent = true;
  }
  for(counter = 0; counter < 14; counter ++){
    handleButton(counter);
    }
  }
}

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]);
    }
  }
}

Who is online

Users browsing this forum: No registered users and 2 guests