From 4a4a57ed36443d2c1c62982fe3848ac71f4098e4 Mon Sep 17 00:00:00 2001 From: Shourov Paul <101161294+Shourov0@users.noreply.github.com> Date: Fri, 10 Nov 2023 21:15:15 +0600 Subject: [PATCH 1/2] Add files via upload --- .../ESP32_Media_controll.ino | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 examples/ESP32_Media_controll/ESP32_Media_controll.ino diff --git a/examples/ESP32_Media_controll/ESP32_Media_controll.ino b/examples/ESP32_Media_controll/ESP32_Media_controll.ino new file mode 100644 index 0000000..d37600e --- /dev/null +++ b/examples/ESP32_Media_controll/ESP32_Media_controll.ino @@ -0,0 +1,49 @@ +/* Media control with rotary encoder */ + +#include + +BleKeyboard bleKeyboard; + +#define outputA 13 //CLK Pin of Rotary encoder +#define outputB 34 //DT Pin of Rotary encoder +#define PinSW 25 //Switch pin of Rotary encoder +//Gnd --> Gnd + +int aState; +int aLastState; + +void setup() +{ + Serial.begin(115200); + Serial.println("Starting BLE work!"); + bleKeyboard.begin(); + pinMode (outputA,INPUT_PULLUP); + pinMode (outputB,INPUT_PULLUP); + pinMode(PinSW, INPUT_PULLUP); + + aLastState = digitalRead(outputA); +} + +void loop() +{ + if(bleKeyboard.isConnected()) { + + int btn = digitalRead(PinSW); + + if (!(digitalRead(PinSW))) { + bleKeyboard.write(KEY_MEDIA_MUTE); + delay(250); + } + + aState = digitalRead(outputA); // Reads the "current" state of the outputA + if (aState != aLastState){ + if (digitalRead(outputB) != aState) { + bleKeyboard.write(KEY_MEDIA_VOLUME_UP); + bleKeyboard.releaseAll(); + } else { + bleKeyboard.write(KEY_MEDIA_VOLUME_DOWN); + } + } + aLastState = aState; // Updates the previous state of the outputA with the current state + } +} From a09e34bd4046d89c053618c73e42bef6fc3ea6ee Mon Sep 17 00:00:00 2001 From: Shourov Paul <101161294+Shourov0@users.noreply.github.com> Date: Fri, 10 Nov 2023 22:33:51 +0600 Subject: [PATCH 2/2] ESP32_Media_controll.ino --- .../ESP32_Media_controll.ino | 28 +++++++++++++++---- 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/examples/ESP32_Media_controll/ESP32_Media_controll.ino b/examples/ESP32_Media_controll/ESP32_Media_controll.ino index d37600e..af481c3 100644 --- a/examples/ESP32_Media_controll/ESP32_Media_controll.ino +++ b/examples/ESP32_Media_controll/ESP32_Media_controll.ino @@ -10,7 +10,11 @@ BleKeyboard bleKeyboard; //Gnd --> Gnd int aState; -int aLastState; +int aLastState; +int buttonState = HIGH; // Assuming the button is normally open +int lastButtonState = HIGH; +unsigned long lastDebounceTime = 0; +unsigned long debounceDelay = 50; void setup() { @@ -28,12 +32,24 @@ void loop() { if(bleKeyboard.isConnected()) { - int btn = digitalRead(PinSW); - - if (!(digitalRead(PinSW))) { - bleKeyboard.write(KEY_MEDIA_MUTE); - delay(250); + int reading = digitalRead(PinSW); + + if (reading != lastButtonState) { + lastDebounceTime = millis(); } + + if ((millis() - lastDebounceTime) > debounceDelay) { + if (reading != buttonState) { + buttonState = reading; + + if (!(digitalRead(PinSW))) { + bleKeyboard.write(KEY_MEDIA_MUTE); + delay(250); + } + } + } + + lastButtonState = reading; aState = digitalRead(outputA); // Reads the "current" state of the outputA if (aState != aLastState){