/** // This code configures an ESP32C3 as a bluetooth keyboard */ #include BleKeyboard bleKeyboard; // Define GPIO pins for switches const int switchLeftPin = 2; //Left Arrow Key const int switchCapsSpacePin = 3; // Capslock + Spacebar const int switchRightPin = 4; //Right Arrow Key // Variable to store the state of switches bool lastLeftState = HIGH; bool lastCapsSpaceState = HIGH; bool lastRightState = HIGH; void setup () { Serial.begin(115200); //Initialize BLE Keyboard bleKeyboard.begin(); // Configure GPIO pins as inputs w/ pull-up resistors pinMode(switchLeftPin, INPUT_PULLUP); pinMode(switchCapsSpacePin, INPUT_PULLUP); pinMode(switchRightPin, INPUT_PULLUP); Serial.println("Bluetooth Keyboard started, waiting for connection..."); } void loop() { //Ensure BLE is connected if (bleKeyboard.isConnected()) { //Read switch states bool currentLeftState = digitalRead(switchLeftPin); bool currentCapsSpaceState = digitalRead(switchCapsSpacePin); bool currentRightState = digitalRead(switchRightPin); //Check for left arrow key press if (currentLeftState == LOW && lastLeftState == HIGH) { Serial.println("Left arrow pressed"); bleKeyboard.press(KEY_LEFT_ARROW); delay(100); //Debounce delay bleKeyboard.release(KEY_LEFT_ARROW); } //Check for Caps + Space key press if (currentCapsSpaceState == LOW && lastCapsSpaceState == HIGH) { Serial.println("Caps + Space Pressed"); bleKeyboard.press(KEY_CAPS_LOCK); bleKeyboard.press(' '); //Spacebar delay(100); //Debounce delay bleKeyboard.release(KEY_CAPS_LOCK); bleKeyboard.release(' '); } //Check for right arrow key press if (currentRightState == LOW && lastRightState == HIGH) { Serial.println("Right arrow pressed"); bleKeyboard.press(KEY_RIGHT_ARROW); delay(100); //Debounce delay bleKeyboard.release(KEY_RIGHT_ARROW); } // Update last state variables lastLeftState = currentLeftState; lastCapsSpaceState = currentCapsSpaceState; lastRightState = currentRightState; } else { Serial.println("Waiting for connection..."); delay(1000); } delay(10); // Short delay to prevent spamming }