Control Board: Uno R4 Wifi
Code:
#include <ArduinoBLE.h>
// variables for buttons
const int RHbuttonPin = 2;
const int LHbuttonPin = 3; // New button on pin 3
int RHoldButtonState = LOW;
int LHoldButtonState = LOW; // State for LH button
void setup() {
Serial.begin(9600);
// configure the button pins as input
pinMode(RHbuttonPin, INPUT_PULLUP);
pinMode(LHbuttonPin, INPUT_PULLUP); // Configure LH button pin
if (!BLE.begin()) {
Serial.println("BLE initialization failed!");
while (1); // Halt if BLE initialization fails
}
Serial.println("Bluetooth® Low Energy Central - LED control");
// Start scanning for peripherals advertising the service UUID
BLE.scanForUuid("19b10000-e8f2-537e-4f6c-d104768a1214");
}
void loop() {
BLEDevice peripheral = BLE.available();
if (peripheral) {
Serial.print("Found ");
Serial.print(peripheral.address());
Serial.print(" '");
Serial.print(peripheral.localName());
Serial.print("' ");
Serial.print(peripheral.advertisedServiceUuid());
Serial.println();
if (peripheral.localName() != "LED") {
return;
}
// Stop scanning as we've found a matching peripheral
BLE.stopScan();
controlLed(peripheral);
// After disconnecting, restart scanning for peripherals
BLE.scanForUuid("19b10000-e8f2-537e-4f6c-d104768a1214");
}
}
void controlLed(BLEDevice peripheral) {
Serial.println("Connecting...");
if (peripheral.connect()) {
Serial.println("Connected");
} else {
Serial.println("Failed to connect!");
return;
}
// Discover attributes on the peripheral (LED characteristics)
if (peripheral.discoverAttributes()) {
Serial.println("Attributes discovered");
} else {
Serial.println("Attribute discovery failed!");
peripheral.disconnect();
return;
}
// Retrieve the LED characteristic (for both RH and LH)
BLECharacteristic RHledCharacteristic = peripheral.characteristic("19b10001-e8f2-537e-4f6c-d104768a1214");
BLECharacteristic LHledCharacteristic = peripheral.characteristic("19b10002-e8f2-537e-4f6c-d104768a1214");
if (!RHledCharacteristic || !LHledCharacteristic) {
Serial.println("Peripheral does not have LED characteristics!");
peripheral.disconnect();
return;
} else {
Serial.println("LED characteristics found and valid.");
}
while (peripheral.connected()) {
int RHbuttonState = !digitalRead(RHbuttonPin);
int LHbuttonState = !digitalRead(LHbuttonPin);
if (RHoldButtonState != RHbuttonState) {
RHoldButtonState = RHbuttonState;
if (RHbuttonState) {
Serial.println("RH Button pressed");
RHledCharacteristic.writeValue((uint8_t)0x01); // Turn ON RH LED
} else {
RHledCharacteristic.writeValue((uint8_t)0x00); // Turn OFF RH LED
}
}
if (LHoldButtonState != LHbuttonState) {
LHoldButtonState = LHbuttonState;
if (LHbuttonState) {
Serial.println("LH Button pressed");
LHledCharacteristic.writeValue((uint8_t)0x01); // Turn ON LH LED
} else {
LHledCharacteristic.writeValue((uint8_t)0x00); // Turn OFF LH LED
}
}
}
Serial.println("Peripheral disconnected");
}
Peripheral Board: Nano33 BLE Rev 2
#include <ArduinoBLE.h>
// Define the service and characteristic UUIDs
BLEService ledService("19b10000-e8f2-537e-4f6c-d104768a1214"); // LED Control Service
// Define two characteristics for controlling two LEDs
BLECharacteristic RHledCharacteristic("19b10001-e8f2-537e-4f6c-d104768a1214", BLERead | BLEWrite, 1); // RH LED control
BLECharacteristic LHledCharacteristic("19b10002-e8f2-537e-4f6c-d104768a1214", BLERead | BLEWrite, 1); // LH LED control
// Define the pins for the two LEDs
const int RHledPin = 2; // RH LED pin
const int LHledPin = 3; // LH LED pin
void setup() {
// Start serial communication
Serial.begin(9600);
// Initialize BLE
if (!BLE.begin()) {
Serial.println("Starting BLE failed!");
while (1);
}
// Set up the LED pins
pinMode(RHledPin, OUTPUT);
pinMode(LHledPin, OUTPUT);
digitalWrite(RHledPin, LOW); // Initially turn off RH LED
digitalWrite(LHledPin, LOW); // Initially turn off LH LED
// Add characteristics to the LED service
ledService.addCharacteristic(RHledCharacteristic);
ledService.addCharacteristic(LHledCharacteristic);
// Add the service to BLE
BLE.addService(ledService);
// Start advertising the service
BLE.advertise();
Serial.println("Advertising BLE LED control service...");
}
void loop() {
// Poll for BLE events (handle requests from central)
BLE.poll();
// If the RH LED characteristic is written to, change the RH LED state
if (RHledCharacteristic.written()) {
uint8_t RHledState = *(RHledCharacteristic.value()); // Dereference the pointer to get the byte value
if (RHledState == 0x01) {
digitalWrite(RHledPin, HIGH); // Turn ON RH LED
} else {
digitalWrite(RHledPin, LOW); // Turn OFF RH LED
}
Serial.print("RH LED State: ");
Serial.println(RHledState == 0x01 ? "ON" : "OFF");
}
// If the LH LED characteristic is written to, change the LH LED state
if (LHledCharacteristic.written()) {
uint8_t LHledState = *(LHledCharacteristic.value()); // Dereference the pointer to get the byte value
if (LHledState == 0x01) {
digitalWrite(LHledPin, HIGH); // Turn ON LH LED
} else {
digitalWrite(LHledPin, LOW); // Turn OFF LH LED
}
Serial.print("LH LED State: ");
Serial.println(LHledState == 0x01 ? "ON" : "OFF");
}
}
I cannot get the boards to connect and the one time they did I bounced between on the control Serial
00:54:10.246 -> Found b2:78:90:04:4c:65 'LED' 19b10000-e8f2-537e-4f6c-d104768a1214
00:54:10.292 -> Connecting...
00:54:10.384 -> Connected
00:54:11.076 -> Attributes discovered
00:54:11.076 -> Peripheral does not have LED characteristics!
The Peripheral board kept saying:
00:51:10.075 -> Connected to central: c0:4e:30:12:94:99
00:51:10.813 -> Disconnected from central: c0:4e:30:12:94:99
This is my first Bluetooth project, please help! Thanks!!