#include <BluetoothSerial.h>
#include <Button.h>
#include <ESP32Encoder.h>
// Define pins for buttons and encoders
#define BUTTON1_PIN 2
#define BUTTON2_PIN 4
#define BUTTON3_PIN 5
#define BUTTON4_PIN 18
#define ENCODER1_PIN_A 19
#define ENCODER1_PIN_B 21
#define ENCODER2_PIN_A 22
#define ENCODER2_PIN_B 23
BluetoothSerial SerialBT;
Button button1(BUTTON1_PIN);
Button button2(BUTTON2_PIN);
Button button3(BUTTON3_PIN);
Button button4(BUTTON4_PIN);
ESP32Encoder encoder1;
ESP32Encoder encoder2;
unsigned long lastPressTime = 0;
bool singleClick = false;
void setup() {
Serial.begin(115200);
SerialBT.begin("ESP32_S3_Mini"); // Bluetooth device name
// Initialize buttons
button1.begin();
button2.begin();
button3.begin();
button4.begin();
// Initialize encoders
pinMode(ENCODER1_PIN_A, INPUT_PULLUP);
pinMode(ENCODER1_PIN_B, INPUT_PULLUP);
pinMode(ENCODER2_PIN_A, INPUT_PULLUP);
pinMode(ENCODER2_PIN_B, INPUT_PULLUP);
encoder1.attachSingleEdge(ENCODER1_PIN_A, ENCODER1_PIN_B);
encoder2.attachSingleEdge(ENCODER2_PIN_A, ENCODER2_PIN_B);
encoder1.clearCount();
encoder2.clearCount();
}
void loop() {
// Handle button presses
if (button1.pressed()) {
SerialBT.println("Navigate to stats");
}
if (button2.pressed()) {
SerialBT.println("Navigate to items");
}
if (button3.pressed()) {
SerialBT.println("Navigate to data");
}
if (button4.pressed()) {
unsigned long currentTime = millis();
if (currentTime - lastPressTime < 500) { // Double click detected
SerialBT.println("Scroll up");
} else { // Single click detected
SerialBT.println("Scroll down");
}
lastPressTime = currentTime;
}
// Handle encoder rotations
long pos1 = encoder1.getCount();
long pos2 = encoder2.getCount();
if (pos1 != 0) {
if (pos1 > 0) {
SerialBT.println("Scroll right");
} else {
SerialBT.println("Scroll left");
}
encoder1.clearCount();
}
if (pos2 != 0) {
if (pos2 > 0) {
SerialBT.println("Volume up");
} else {
SerialBT.println("Volume down");
}
encoder2.clearCount();
}
delay(100); // Small delay to debounce buttons and encoders
}