I have a problem with my codes.
I need a BLE keyboard with 4 keys: N key, K key, right arrow key, left arrow key.
I found two codes but:
- in one code the arrow keys work but not the letters.
- In the second code it is exactly the other way around, the letters but the arrow keys don't work.
these four things should each be controlled with a push button.
does anyone have an idea?
Thank you
Code with working Arrows, but when i add or change KEY_ARROW_RIGHT to maybe KEY_N it does not work:
Code: Select all
#include <BleKeyboard.h>
#include <driver/adc.h>
BleKeyboard bleKeyboard;
//both pins are pulled low via external resistor
#define leftButton 32
#define rightButton 33
void setup() {
adc_power_off();
Serial.begin(115200);
bleKeyboard.begin();
pinMode(leftButton, INPUT);
pinMode(rightButton, INPUT);
Serial.print("Connecting...");
while(!bleKeyboard.isConnected());
Serial.println("Connected");
}
void loop() {
while(bleKeyboard.isConnected()) {
if(digitalRead(leftButton)) {
bleKeyboard.press(KEY_LEFT_ARROW);
bleKeyboard.releaseAll();
delay(1000); //debouncing
}
if(digitalRead(rightButton)) {
bleKeyboard.press(KEY_RIGHT_ARROW);
bleKeyboard.releaseAll();
delay(1000);
}
delay(10);
}
delay(1000);
second code, if i set "KEY_LEFT_ARROW" i get only a W if i push the button:
Code: Select all
#include <BleKeyboard.h>
//Se the name of the bluetooth keyboard (that shows up in the bluetooth menu of your device)
BleKeyboard bleKeyboard("Franzi-Buttons");
byte buttonPins[] = {23, 22, 21, 19, 18};
char buttonChars[] = {'n', 'k', 'KEY_LEFT_ARROW', '2', 'e'};
boolean buttonStates[] = {false, false, false, false, false};
//Set the old button state to be LOW/false; which means not pressed
boolean oldPinState = LOW;
void setup() {
//Start the Serial communication (with the computer at 115200 bits per second)
Serial.begin(115200);
//Send this message to the computer
Serial.println("Starting BLE work!");
//Begin the BLE keyboard/start advertising the keyboard (so phones can find it)
bleKeyboard.begin();
//Make the button pin an INPUT_PULLUP pin, which means that it is normally HIGH, untill it is pressed/ connected to the GND/0V
for(int i=0;i<sizeof(buttonPins);i++){
pinMode(buttonPins[i], INPUT_PULLUP);
}
}
void loop() {
if (bleKeyboard.isConnected()) {
for(int i=0;i<sizeof(buttonPins);i++){
boolean state = digitalRead(buttonPins[i]);
if (state != buttonStates[i]){
if (state == LOW){
bleKeyboard.press(buttonChars[i]);
}else{
bleKeyboard.release(buttonChars[i]);
}
buttonStates[i] = state;
}
}
}
}