Description
Operating System
Others
IDE version
Arduino IDE: 2.3.6
Board
Seeed XIAO nRF52840
BSP version
Latest github
Sketch
// PageTurner_BLE_Keyboard_Adafruit.ino
// BLE HID page turner for Adafruit bootloader-based XIAO nRF52840
#include <bluefruit.h> // Include the Adafruit Bluefruit BLE library
// Pin definitions for the pedal switches and onboard LED
#define PEDAL_DOWN_PIN 2 // D02: Pedal/button for Page Down
#define PEDAL_UP_PIN 3 // D03: Pedal/button for Page Up
#define LED_PIN 13 // Onboard LED
BLEHidAdafruit blehid; // Create a BLE HID keyboard object
// Callback for keyboard LED state (e.g., Caps Lock)
// Not used in this project, but required by the library
void set_keyboard_led(uint16_t conn_hdl, uint8_t led_state) {
// Optional: handle keyboard LED state changes here
}
void setup() {
Serial.begin(115200); // Start serial communication for debugging
// Set up pedal pins as inputs with internal pull-up resistors
pinMode(PEDAL_DOWN_PIN, INPUT_PULLUP);
pinMode(PEDAL_UP_PIN, INPUT_PULLUP);
pinMode(LED_PIN, OUTPUT); // Set onboard LED as output
// Initialize BLE hardware
Bluefruit.begin();
Bluefruit.setTxPower(4); // Set BLE transmit power
Bluefruit.setName("PagePedalTurner"); // Set BLE device name
blehid.begin(); // Initialize BLE HID keyboard service
blehid.setKeyboardLedCallback(set_keyboard_led); // Register LED callback
// Set up BLE advertising so devices can discover and connect
Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE);
Bluefruit.Advertising.addTxPower();
Bluefruit.Advertising.addAppearance(BLE_APPEARANCE_HID_KEYBOARD);
Bluefruit.Advertising.addService(blehid);
Bluefruit.Advertising.start(0); // Start advertising (0 = never stop)
Serial.println("BLE PageTurner is ready!"); // Print status to serial monitor
}
void loop() {
static bool lastDown = HIGH; // Store previous state of Page Down pedal
static bool lastUp = HIGH; // Store previous state of Page Up pedal
// Read current state of pedals (LOW = pressed, HIGH = released)
bool pedalDown = digitalRead(PEDAL_DOWN_PIN);
bool pedalUp = digitalRead(PEDAL_UP_PIN);
// Turn LED ON if either pedal is pressed, OFF otherwise (for active-low LED)
if (pedalDown == LOW || pedalUp == LOW) {
digitalWrite(LED_PIN, LOW); // ON for active-low LED
} else {
digitalWrite(LED_PIN, HIGH); // OFF for active-low LED
}
// If Page Down pedal was just pressed (falling edge)
if (lastDown == HIGH && pedalDown == LOW) {
blehid.keyPress(HID_KEY_PAGE_DOWN); // Send Page Down key press
delay(10); // Short delay for key press
blehid.keyRelease(); // Release all keys
}
lastDown = pedalDown; // Update last state
// If Page Up pedal was just pressed (falling edge)
if (lastUp == HIGH && pedalUp == LOW) {
blehid.keyPress(HID_KEY_PAGE_UP); // Send Page Up key press
delay(10); // Short delay for key press
blehid.keyRelease(); // Release all keys
}
lastUp = pedalUp; // Update last state
delay(5); // Small delay for debounce and to reduce CPU usage
}
What happened ?
I am trying to prototype a BLE music page turner for tablets using - Seeed_XIAO_nRF52840_bootloader-0.6.2_s140_7.3.0
Seeed XIAO nRF52840
HID_KEY_PAGE_DOWN is giving N and HID_KEY_PAGE_UP displays K
Hope this helps
How to reproduce ?
- Load the sketch
- Press Page Down button - sends "N" to both Mac and iPhone
- Press Page Up button - sends "K" to both Mac and iPhone
Debug Log
No response