Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ESP32_Media_controll.ino #344

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions examples/ESP32_Media_controll/ESP32_Media_controll.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/* Media control with rotary encoder */

#include <BleKeyboard.h>

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;
int buttonState = HIGH; // Assuming the button is normally open
int lastButtonState = HIGH;
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 50;

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 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){
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
}
}