3
3
4
4
This example:
5
5
- 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
7
9
8
10
The circuit:
9
11
- Arduino Nano 33 IoT
18
20
#include < ArduinoBLE.h>
19
21
20
22
// Define custom BLE service for position (read-only)
21
- BLEService positionService (" 95ff7bf8-aa6f-4671-82d9-22a8931c5387" );
23
+ BLEService posService (" 95ff7bf8-aa6f-4671-82d9-22a8931c5387" );
22
24
BLEFloatCharacteristic posX (" 95ff7bf8-aa6f-4671-82d9-22a8931c5387" , BLERead);
23
25
BLEFloatCharacteristic posY (" f49caa00-17f8-4e92-b5fd-d27137ca4515" , BLERead);
24
26
BLEFloatCharacteristic posZ (" 84f9b003-6d14-44d7-8db1-d574d29c10c3" , BLERead);
25
27
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);
28
31
29
32
void setup () {
30
33
// Initialize internal LED (for visual debugging)
@@ -59,18 +62,22 @@ void setup() {
59
62
}
60
63
61
64
// Set advertised local name and services UUID
62
- BLE.setLocalName (" Nano33IoT" );
65
+ BLE.setDeviceName (" Arduino Nano 33 IoT" );
66
+ BLE.setLocalName (" BLE Experiment" );
63
67
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);
69
75
70
76
// Set default values for characteristics
71
77
posX.writeValue ((float )0.0 );
72
78
posY.writeValue ((float )0.0 );
73
79
posZ.writeValue ((float )0.0 );
80
+ led.writeValue (0 );
74
81
75
82
// Start advertising
76
83
BLE.advertise ();
@@ -89,14 +96,11 @@ void loop() {
89
96
// While central is still connected...
90
97
while (central.connected ()) {
91
98
float x, y, z;
99
+ byte ledValue = 0x0 ;
92
100
93
101
// Read gyroscope values
94
102
if (IMU.gyroscopeAvailable ()) {
95
103
IMU.readGyroscope (x, y, z);
96
- delay (900 );
97
- digitalWrite (LED_BUILTIN, HIGH);
98
- delay (100 );
99
- digitalWrite (LED_BUILTIN, LOW);
100
104
}
101
105
102
106
// Display gyroscope values on serial
@@ -106,11 +110,23 @@ void loop() {
106
110
Serial.print (y);
107
111
Serial.print (" ," );
108
112
Serial.println (z);
113
+ Serial.print (" LED is " );
114
+ Serial.println (ledValue);
109
115
110
116
// Write values on BLE
111
117
posX.writeValue (x);
112
118
posY.writeValue (y);
113
119
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 );
114
130
}
115
131
116
132
// when the central disconnects, print it out:
0 commit comments