This getting more and more confusing…
Now I have solved the microphone problem, works ok. But now I can´t connect the BLE. My computer sees the SEEED, but can´t connect…
Can you see whats wrong with this code? I have tryed everything soon, even asked ChatGPT… no solution…
#include <ArduinoBLE.h>
#include <mic.h>
// BLE HID service and characteristic
BLEService hidService("1812"); // HID service UUID
BLECharacteristic inputReportCharacteristic("2a4d", BLERead | BLENotify, 8);
// Microphone settings
#define SAMPLES 800 // Number of audio samples per cycle
mic_config_t mic_config{
.channel_cnt = 1,
.sampling_rate = 16000,
.buf_size = 1600,
#if defined(ARDUINO_ARCH_NRF52840)
.debug_pin = LED_BUILTIN // Debug pin (LED_BUILTIN)
#endif
};
NRF52840_ADC_Class Mic(&mic_config); // Initialize the microphone
int16_t recording_buf[SAMPLES]; // Buffer to store audio data
volatile uint8_t recording = 0;
volatile static bool record_ready = false;
int soundLevel = 0; // Calculated sound level from the microphone
// Threshold to trigger keypress
const int threshold = 100;
// Function to send 'A' as a keypress
void sendKeyA() {
uint8_t report[8] = {0}; // HID Report: Modifier, Reserved, Key1, ...
report[2] = 0x04; // HID Keycode for 'A'
// Send key press
Serial.println("Sending 'A' key press...");
inputReportCharacteristic.writeValue(report, sizeof(report));
delay(10); // Simulate keypress duration
// Send key release
report[2] = 0; // Release the key
inputReportCharacteristic.writeValue(report, sizeof(report));
}
// Callback function for PDM (Microphone)
static void audio_rec_callback(uint16_t *buf, uint32_t buf_len) {
static uint32_t idx = 0;
// Copy audio data from DMA buffer to our buffer
for (uint32_t i = 0; i < buf_len; i++) {
recording_buf[idx++] = buf[i]; // Store audio sample in the buffer
if (idx >= SAMPLES) {
idx = 0;
recording = 0;
record_ready = true;
break;
}
}
}
void setup() {
Serial.begin(115200);
while (!Serial) { delay(10); }
// Initialize BLE
if (!BLE.begin()) {
Serial.println("Starting BLE failed!");
while (1);
}
BLE.setLocalName("XIAO BLE Keyboard");
BLE.setAdvertisedService(hidService);
hidService.addCharacteristic(inputReportCharacteristic);
BLE.addService(hidService);
BLE.advertise();
// Initialize the microphone and set callback
Mic.set_callback(audio_rec_callback);
if (!Mic.begin()) {
Serial.println("Mic initialization failed");
while (1);
}
Serial.println("Mic initialization done.");
}
void loop() {
BLEDevice central = BLE.central(); // Check if a central device is connected
// If recording is done and audio data is available
if (record_ready) {
// Calculate sound level by summing and averaging the absolute sound strength
soundLevel = 0;
for (int i = 0; i < SAMPLES; i++) {
soundLevel += abs(recording_buf[i]); // Take the absolute value for each sample
}
soundLevel /= SAMPLES; // Average sound strength
Serial.print("Sound Level: ");
Serial.println(soundLevel);
// If sound level exceeds the threshold, trigger a keypress
if (soundLevel > threshold) {
if (central) {
Serial.println("Loud sound detected! Device connected. Sending 'A'...");
sendKeyA();
} else {
Serial.println("Loud sound detected, but device is NOT connected.");
}
}
// Reset the status after processing the sound
record_ready = false;
}
delay(10); // Short delay for stability
}