Hi!
I have a software in my Seeed XAIO nRF52840 Sense that should send a letter over BLE when two pins are connected.
But I cant find a way of make the board understand when they are connected.
I have put an open Switch between P0.02 and P0.03 and when the switch are closed the software should recognize this and send a letter, in this case a “A”.
I use Arduino to code, but cant make this work, when connected via serial or BLE.
Please help.
Regards
Fredrik
#include <bluefruit.h>
// Define pin numbers and their aliases
#define PIN_P0_02_A0_D0 18 // P0.02, A0, D0
#define PIN_P0_03_A1_D1 19 // P0.03, A1, D1
#define LED_PIN 13 // LED pin (could be D13 or any other available pin)
BLEHidAdafruit blehid;
bool bleConnected = false;
void setup()
{
// Start serial communication for debugging
Serial.begin(115200);
while (!Serial) delay(10); // Wait until serial is ready
// Initialize Bluetooth
Bluefruit.begin();
Bluefruit.setTxPower(4); // Adjust transmission power
// Set the name of the Bluetooth device
Bluefruit.setName("BLE Pin Test"); // Change the name here!
// Initialize HID (keyboard functionality)
blehid.begin();
// Connection callback
Bluefruit.Periph.setConnectCallback(connect_callback);
Bluefruit.Periph.setDisconnectCallback(disconnect_callback);
// Start advertising immediately when the device is powered on
startAdv();
// Initialize P0.02/A0/D0 and P0.03/A1/D1 as inputs with internal pull-up
pinMode(PIN_P0_02_A0_D0, INPUT_PULLUP);
pinMode(PIN_P0_03_A1_D1, INPUT_PULLUP);
// Initialize LED_PIN as output
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, LOW); // Turn off LED initially
Serial.println("Setup completed");
// Test to blink LED to ensure it works
for (int i = 0; i < 5; i++) {
digitalWrite(LED_PIN, HIGH);
delay(500);
digitalWrite(LED_PIN, LOW);
delay(500);
}
}
void startAdv(void)
{
// Add advertising data
Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE);
Bluefruit.Advertising.addTxPower();
Bluefruit.Advertising.addAppearance(BLE_APPEARANCE_HID_KEYBOARD);
Bluefruit.Advertising.addName();
// Set advertising settings
Bluefruit.Advertising.restartOnDisconnect(true);
Bluefruit.Advertising.setInterval(32, 244); // Unit in 0.625 ms
Bluefruit.Advertising.setFastTimeout(30); // Number of seconds in fast mode
Bluefruit.Advertising.start(0); // 0 = Advertise until connection is made
}
void loop()
{
// Read status on P0.02/A0/D0 and P0.03/A1/D1
int pinStatusP0_02_A0_D0 = digitalRead(PIN_P0_02_A0_D0);
int pinStatusP0_03_A1_D1 = digitalRead(PIN_P0_03_A1_D1);
// Print status on P0.02/A0/D0 and P0.03/A1/D1 to serial monitor
Serial.print("P0.02/A0/D0: ");
Serial.print(pinStatusP0_02_A0_D0);
Serial.print(" P0.03/A1/D1: ");
Serial.println(pinStatusP0_03_A1_D1);
// Check if P0.02/A0/D0 and P0.03/A1/D1 are connected
if (pinStatusP0_02_A0_D0 == LOW && pinStatusP0_03_A1_D1 == LOW) {
Serial.println("P0.02/A0/D0 and P0.03/A1/D1 are connected");
digitalWrite(LED_PIN, HIGH); // Turn on LED
if (bleConnected) {
Serial.println("BLE is connected and sending 'A'");
blehid.keyPress('A'); // Send key press 'A'
delay(5);
blehid.keyRelease(); // Release the key press
}
} else {
digitalWrite(LED_PIN, LOW); // Turn off LED
}
delay(1000); // Wait one second before the next read to reduce the number of prints
}
void connect_callback(uint16_t conn_handle)
{
bleConnected = true;
Serial.println("BLE connected");
}
void disconnect_callback(uint16_t conn_handle, uint8_t reason)
{
bleConnected = false;
Serial.println("BLE disconnected");
startAdv(); // Start advertising again
}