0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

ESP32をBluetoothキーボードにした際、データが正常に入力できない時の対処法

Last updated at Posted at 2024-09-19

前回ESP32でBluetoothのキーボードを作成したが、
https://qiita.com/Kurogara/items/e847e2fac57a61215b70
連続した文字を入力した際、正常に入力できない現象が発生していた。

前回の対処法は「123」を送信したい場合、
「bleKeyboard.print("1");」の後に「delay(100);」を入れることで、
「123」を入力していた。
以下がそのソースコード

#include <BleKeyboard.h>

BleKeyboard bleKeyboard("TESTBLE");

int LED_PIN = 5;
int BUTTON_PIN = 6;
int SWITCH_PIN = 7;
int back_data = 0;

void setup() {
  // put your setup code here, to run once:
  pinMode(BUTTON_PIN, INPUT_PULLUP);
  pinMode(SWITCH_PIN, INPUT_PULLUP);
  pinMode(LED_PIN, OUTPUT);

  bleKeyboard.begin();
}

void loop() {
  if (bleKeyboard.isConnected()) {
    digitalWrite(LED_PIN, HIGH);
    if (digitalRead(SWITCH_PIN) == 0) {
      if (back_data == 0) {
        bleKeyboard.print("1");
        delay(100);
        bleKeyboard.print("2");
        delay(100);
        bleKeyboard.print("3");
        delay(100);
        bleKeyboard.press(KEY_LEFT_SHIFT);
        bleKeyboard.press(KEY_TAB);
        delay(200);
        bleKeyboard.releaseAll(); // すべてのキーを離す
        back_data = 1;
      }
    } else if (digitalRead(BUTTON_PIN) == 0) {
      if (back_data == 0) {
        bleKeyboard.write(KEY_TAB);
        delay(100);
        back_data = 1;
      }
    } else {
      digitalWrite(LED_PIN, LOW);
      back_data = 0;
    }
  } else {
    digitalWrite(LED_PIN, LOW);
  }
}

実際はsetDelayを入れるだけで対処ができた。

  bleKeyboard.begin();
  bleKeyboard.setDelay(50);

変更後のソースコードは以下。

#include <BleKeyboard.h>

BleKeyboard bleKeyboard("TESTBLE");

int LED_PIN = 5;
int BUTTON_PIN = 6;
int SWITCH_PIN = 7;
int back_data = 0;

void setup() {
  // put your setup code here, to run once:
  pinMode(BUTTON_PIN, INPUT_PULLUP);
  pinMode(SWITCH_PIN, INPUT_PULLUP);
  pinMode(LED_PIN, OUTPUT);

  bleKeyboard.begin();
  bleKeyboard.setDelay(50);//←ここが追加箇所
}

void loop() {
  if (bleKeyboard.isConnected()) {
    digitalWrite(LED_PIN, HIGH);
    if (digitalRead(SWITCH_PIN) == 0) {
      if (back_data == 0) {
        bleKeyboard.print("123");
        back_data = 1;
      }
    } else if (digitalRead(BUTTON_PIN) == 0) {
      if (back_data == 0) {
        bleKeyboard.write(KEY_TAB);
        delay(100);
        bleKeyboard.press(KEY_LEFT_SHIFT);
        bleKeyboard.press(KEY_TAB);
        delay(200);
        bleKeyboard.releaseAll(); // すべてのキーを離す
        back_data = 1;
      }
    } else {
      digitalWrite(LED_PIN, LOW);
      back_data = 0;
    }
  } else {
    digitalWrite(LED_PIN, LOW);
  }
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?