I uploaded to my Arduino RP2040 the following sketch
#include <ArduinoBLE.h>
#include <Arduino_LSM6DSOX.h>
#define BUFFER_SIZE 64
BLEService service("180F");
BLEUnsignedCharCharacteristic accellerometerChar("0001", // standard 16-bit characteristic UUID
BLERead | BLENotify); // remote clients will be able to get notifications if this characteristic changes
int oldBatteryLevel = 0; // last battery level reading from analog input
long previousMillis = 0; // last time the battery level was checked, in ms
int i = 0;
float buffer[BUFFER_SIZE];
/*
* SETUP
*/
void setup() {
Serial.begin(9600); // initialize serial communication
while (!Serial);
pinMode(LED_BUILTIN, OUTPUT); // initialize the built-in LED pin to indicate when a central is connected
// avvio del bluetooth
if (!BLE.begin()) {
Serial.println("starting BLE failed!");
while (1);
}
Serial.println("Bluetooth device attivo, in attesa di connessione...");
//Avvia dell'accellerometro
if (!IMU.begin()) {
Serial.println("Failed to initialize IMU!");
while (1);
}
Serial.println("Unità IMU inizializzata...");
/*
* Impostazioni dei parametri del servizio
*/
BLE.setLocalName("Algoritma");
BLE.setAdvertisedService(service); // add the service UUID
service.addCharacteristic(accellerometerChar); // add the battery level characteristic
BLE.addService(service); // Add the battery service
accellerometerChar.writeValue(oldBatteryLevel); // set initial value for this characteristic
// start advertising
BLE.advertise();
}
void loop() {
// wait for a BLE central
BLEDevice central = BLE.central();
readSensoreAccellerometetro();
// if a central is connected to the peripheral:
if (central) {
Serial.print("Connected to central: ");
Serial.println(central.address());
digitalWrite(LED_BUILTIN, HIGH);
// check the battery level every 200ms
// while the central is connected:
while (central.connected()) {
long currentMillis = millis();
readSensoreAccellerometetro();
// if 200ms have passed, check the battery level:
if (currentMillis - previousMillis >= 200) {
previousMillis = currentMillis;
updateAccellerometerLevel();
}
}
// when the central disconnects, turn off the LED:
digitalWrite(LED_BUILTIN, LOW);
Serial.print("Disconnected from central: ");
Serial.println(central.address());
}
}
void readSensoreAccellerometetro(){
float x, y, z;
if (IMU.accelerationAvailable()) {
IMU.readAcceleration(x, y, z);
buffer[i] = sqrt( abs(x)* abs(x) + abs(y) * abs(y) + abs(z) * abs(z));
i++;
if (i > BUFFER_SIZE){
i = 0;
}
}
}
void updateAccellerometerLevel() {
float batteryLevel = 0;
float tot = 0;
for(int i = 0; i < BUFFER_SIZE; i++){
tot += buffer[i] * buffer[i];
}
batteryLevel = sqrt( tot ) / BUFFER_SIZE;
if (batteryLevel != oldBatteryLevel) { // if the battery level has changed
Serial.print("Accellerometro : "); // print it
Serial.println(batteryLevel);
int val = (int) (batteryLevel * 100);
accellerometerChar.writeValue(val); // and update the battery level characteristic
oldBatteryLevel = batteryLevel; // save the level for next comparison
}
}
now the device is blinking forever. and I'm not able to upload any new sketch. If a try the IDE waits few minutes and gives the following generic message
.....................
Errore durante il caricamento dello sketch
as if it is not able to use the serial port.
Is it possible to reset the board to facortory settings? I've already tried with the reset button and with the reset pin. What in my sketch must be avoided to get in this situation?