Skip to content

Commit ae7e2f3

Browse files
committed
Add control of LED through BLE
1 parent 769c6bb commit ae7e2f3

File tree

1 file changed

+30
-14
lines changed

1 file changed

+30
-14
lines changed

src/BLE/BLE.ino

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
44
This example:
55
- reads the acceleration values from the LSM6DS3 sensor
6-
- sends the position data to the serial
6+
- show the position data to the serial
7+
- sends the data on BLE
8+
- expose the LED status on the BLE
79
810
The circuit:
911
- Arduino Nano 33 IoT
@@ -18,13 +20,14 @@
1820
#include <ArduinoBLE.h>
1921

2022
// Define custom BLE service for position (read-only)
21-
BLEService positionService("95ff7bf8-aa6f-4671-82d9-22a8931c5387");
23+
BLEService posService("95ff7bf8-aa6f-4671-82d9-22a8931c5387");
2224
BLEFloatCharacteristic posX("95ff7bf8-aa6f-4671-82d9-22a8931c5387", BLERead);
2325
BLEFloatCharacteristic posY("f49caa00-17f8-4e92-b5fd-d27137ca4515", BLERead);
2426
BLEFloatCharacteristic posZ("84f9b003-6d14-44d7-8db1-d574d29c10c3", BLERead);
2527

26-
// BLEService ledService("40369706-e126-4563-b7ae-3a34b45b3ab8");
27-
// BLEByteCharacteristic switchCharacteristic("40369706-e126-4563-b7ae-3a34b45b3ab8", BLERead | BLEWrite);
28+
// Define custom BLE service for LED management (read/write)
29+
BLEService ledService("daaac223-ea6d-411f-8d8e-32bfb46d4bad");
30+
BLEByteCharacteristic led("40369706-e126-4563-b7ae-3a34b45b3ab8", BLERead | BLEWrite);
2831

2932
void setup() {
3033
// Initialize internal LED (for visual debugging)
@@ -59,18 +62,22 @@ void setup() {
5962
}
6063

6164
// Set advertised local name and services UUID
62-
BLE.setLocalName("Nano33IoT");
65+
BLE.setDeviceName("Arduino Nano 33 IoT");
66+
BLE.setLocalName("BLE Experiment");
6367

64-
BLE.setAdvertisedService(positionService);
65-
positionService.addCharacteristic(posX);
66-
positionService.addCharacteristic(posY);
67-
positionService.addCharacteristic(posZ);
68-
BLE.addService(positionService);
68+
posService.addCharacteristic(posX);
69+
posService.addCharacteristic(posY);
70+
posService.addCharacteristic(posZ);
71+
BLE.addService(posService);
72+
73+
ledService.addCharacteristic(led);
74+
BLE.addService(ledService);
6975

7076
// Set default values for characteristics
7177
posX.writeValue((float)0.0);
7278
posY.writeValue((float)0.0);
7379
posZ.writeValue((float)0.0);
80+
led.writeValue(0);
7481

7582
// Start advertising
7683
BLE.advertise();
@@ -89,14 +96,11 @@ void loop() {
8996
// While central is still connected...
9097
while(central.connected()) {
9198
float x, y, z;
99+
byte ledValue = 0x0;
92100

93101
// Read gyroscope values
94102
if (IMU.gyroscopeAvailable()) {
95103
IMU.readGyroscope(x, y, z);
96-
delay(900);
97-
digitalWrite(LED_BUILTIN, HIGH);
98-
delay(100);
99-
digitalWrite(LED_BUILTIN, LOW);
100104
}
101105

102106
// Display gyroscope values on serial
@@ -106,11 +110,23 @@ void loop() {
106110
Serial.print(y);
107111
Serial.print(",");
108112
Serial.println(z);
113+
Serial.print("LED is ");
114+
Serial.println(ledValue);
109115

110116
// Write values on BLE
111117
posX.writeValue(x);
112118
posY.writeValue(y);
113119
posZ.writeValue(z);
120+
led.readValue(ledValue);
121+
122+
// Read values on BLE
123+
if(ledValue == 0x0) {
124+
digitalWrite(LED_BUILTIN, LOW);
125+
} else {
126+
digitalWrite(LED_BUILTIN, HIGH);
127+
}
128+
129+
delay(1000);
114130
}
115131

116132
// when the central disconnects, print it out:

0 commit comments

Comments
 (0)