Salve a tutti,
ispirandomi allo sketch di esempio BatteryMonitor ho scritto il seguente codice
#include <ArduinoBLE.h>
#include <Arduino_LSM9DS1.h>
BLEService PaddleService("180F");
BLEUnsignedCharCharacteristic PaddleLevelChar("2A19", // standard 16-bit characteristic UUID
BLERead | BLENotify); // remote clients will be able to get notifications if this characteristic changes
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
// begin initialization
if (!BLE.begin()) {
while (1);
}
if (!IMU.begin()) {
while (1);
}
BLE.setLocalName("PaddleMonitor");
BLE.setAdvertisedService(PaddleService); // add the service UUID
PaddleService.addCharacteristic(PaddleLevelChar); // add the paddle level characteristic
BLE.addService(PaddleService); // Add the battery service
// start advertising
BLE.advertise();
}
void loop() {
float x, y, z;
int PaddleLevel, tempo;
// wait for a BLE central
BLEDevice central = BLE.central();
// if a central is connected to the peripheral:
if (central) {
digitalWrite(LED_BUILTIN, HIGH);
while (central.connected()) {
if (IMU.accelerationAvailable()) {
IMU.readAcceleration(x, y, z);
if(x > 0.7 ) {
PaddleLevel=1;
PaddleLevelChar.writeValue(PaddleLevel);
} else {
PaddleLevel=0;
PaddleLevelChar.writeValue(PaddleLevel);
}
}
}
// when the central disconnects, turn off the LED:
digitalWrite(LED_BUILTIN, LOW);
}
}
l'obiettivo è di comunicare al Central un valore 0 o 1 in base ad un valore (più o meno di 0,7) ricavato sull'asse x dell'accelerometro.
Problema:
Lo sketch funziona bene (su Nano 33 BLE sense) ma non ne ho assolutamente alcun controllo.
Mi spiego: se volessi inviare invece del valore 0 o 1 la stringa "è in alto" o "è in basso" non saprei come fare.
Ho provato a documentarmi:
- a questa pagina ArduinoBLE - Arduino Reference
- su GitHub GitHub - arduino-libraries/ArduinoBLE: ArduinoBLE library for Arduino
- cercando eventuali progetti fatti utilizzando la stessa libreria nel Project Hub
ma niente, non riesco a trovare esempi e la documentazione è stringatissima.
Non voglio assolutamente la pappa fatta, vorrei delle dritte su come approfondire il problema.
Spero di essermi spiegato correttamente
Grazie
Andrea