/*
This code is specifically designed for an Arduino Uno R4 board. It utilizes the
ArduinoBLE library to establish a Bluetooth® Low Energy (BLE) service, enabling
a central device to control the onboard LED. When the central device connects to
the Arduino and writes a value to a specific characteristic, it will alter the
state of the LED accordingly. Any non-zero value will turn on the LED, while a
zero value will turn it off.
Board: Arduino Uno R4 WiFI
*/
/*
#include <ArduinoBLE.h>
BLEService ledService("19B10000-E8F2-537E-4F6C-D104768A1214"); // Bluetooth® Low Energy LED Service
// Bluetooth® Low Energy LED Switch Characteristic - custom 128-bit UUID, read and writable by central
BLEByteCharacteristic switchCharacteristic("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite);
#define LED_BUILTIN 26
const int ledPin = LED_BUILTIN; // pin to use for the LED
void setup() {
Serial.begin(9600);
while (!Serial);
// set LED pin to output mode
pinMode(ledPin, OUTPUT);
// begin initialization
if (!BLE.begin()) {
Serial.println("starting Bluetooth® Low Energy module failed!");
while (1);
}
// set advertised local name and service UUID:
BLE.setLocalName("UNO R4 LED");
BLE.setAdvertisedService(ledService);
// add the characteristic to the service
ledService.addCharacteristic(switchCharacteristic);
// add service
BLE.addService(ledService);
// set the initial value for the characeristic:
switchCharacteristic.writeValue(0);
// start advertising
BLE.advertise();
Serial.println("BLE LED Peripheral");
}
void loop() {
// listen for Bluetooth® Low Energy peripherals to connect:
BLEDevice central = BLE.central();
// if a central is connected to peripheral:
if (central) {
Serial.print("Connected to central: ");
// print the central's MAC address:
Serial.println(central.address());
// while the central is still connected to peripheral:
while (central.connected()) {
// if the remote device wrote to the characteristic,
// use the value to control the LED:
if (switchCharacteristic.written()) {
if (switchCharacteristic.value()) { // any value other than 0
Serial.println("LED on");
digitalWrite(ledPin, HIGH); // will turn the LED on
} else { // a 0 value
Serial.println(F("LED off"));
digitalWrite(ledPin, LOW); // will turn the LED off
}
}
}
// when the central disconnects, print it out:
Serial.print(F("Disconnected from central: "));
Serial.println(central.address());
}
}
*/
#include <ArduinoBLE.h>
// Define BLE service and characteristics UUIDs
BLEService movementService("19B10000-E8F2-537E-4F6C-D104768A1214");
BLEFloatCharacteristic moveCharacteristic("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite);
BLEFloatCharacteristic rotateCharacteristic("19B10002-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite);
// Pins for motor control or servo control
const int motorPin = 9; // Example motor pin
const int servoPin = 10; // Example servo pin
void setup() {
Serial.begin(9600);
while (!Serial);
// Initialize BLE
if (!BLE.begin()) {
Serial.println("starting BLE failed!");
while (1);
}
// Set advertised local name and service UUID
BLE.setLocalName("MovementControl");
BLE.setAdvertisedService(movementService);
// Add characteristics to the service
movementService.addCharacteristic(moveCharacteristic);
movementService.addCharacteristic(rotateCharacteristic);
// Add service
BLE.addService(movementService);
// Set initial values for characteristics
moveCharacteristic.writeValue(0.0);
rotateCharacteristic.writeValue(0.0);
// Start advertising
BLE.advertise();
Serial.println("BLE Movement Control Peripheral");
}
void loop() {
// Listen for BLE central devices to connect
BLEDevice central = BLE.central();
// If a central is connected
if (central) {
Serial.print("Connected to central: ");
Serial.println(central.address());
// While central is still connected
while (central.connected()) {
// Check if either characteristic has been written to
if (moveCharacteristic.written() || rotateCharacteristic.written()) {
// Read the values written to the characteristics
float moveValue = moveCharacteristic.value();
float rotateValue = rotateCharacteristic.value();
// Process the received values (replace this with your actual motor/servo control code)
Serial.print("Move value: ");
Serial.println(moveValue);
Serial.print("Rotate value: ");
Serial.println(rotateValue);
// Here you can implement your motor or servo control logic
// For example, you can use moveValue and rotateValue to control motors or servos
}
}
// When central disconnects, print it out
Serial.print("Disconnected from central: ");
Serial.println(central.address());
}
}