ArduinoBLE reads occasionally wrong characteristic value

Hi,
i am struggling with an issue on the RP2040 when i use the ArduinoBLE library. The characteristic can receive values from my iphone app in the range of [0, 100]. I occasionally read wrong values. The program also at some point (random time) simply freezes. This is most definitely not an iphone app issue, as the frequency the wrong values occur increases if i uncomment the mbed ticker. I also ran the code on a nano 33 BLE and i had no issues.
Would be great if you have an idea what this could be, if i did overlook something - or whether it might be a bug in the lib in connection with the RP2040.

Thanks a lot already!

#include <Arduino_LSM6DSOX.h>
#include <ArduinoBLE.h>
//#include <mbed.h>

//mbed::Ticker motor_ticker;

BLEService SliderService("7fc1c80a-d646-4198-8250-7a698e82e5a3"); // BLE LED Service
BLEIntCharacteristic SliderCharacteristicHorizontal("7fc1c80b-d646-4198-8250-7a698e82e5a3", BLEWrite);
BLEIntCharacteristic SliderCharacteristicVertical("7fc1c80c-d646-4198-8250-7a698e82e5a3", BLEWrite);

//Variables
float ax, ay, az, gx, gy, gz; //IMU Data
int slidertest = 0, slidertest2 = 0;

//Initialise Functions
void SliderCharacteristicHorizontalWritten(BLEDevice central, BLECharacteristic characteristic);
void SliderCharacteristicVerticalWritten(BLEDevice central, BLECharacteristic characteristic);
void step_motor();

void setup() {

  //Serial Connection Initialisation
  Serial.begin(115200);

//  if (!IMU.begin()) {
//    Serial.println("Failed to initialize IMU!");
//    while (1);
//  }

  
  // begin initialization of BLE
  if (!BLE.begin()) {
    Serial.println("starting BLE failed!");
    while (1);
  }

  // set advertised local name and service UUID:
  BLE.setLocalName("Robot");
  BLE.setAdvertisedService(SliderService);

  // add the characteristic to the service
  SliderService.addCharacteristic(SliderCharacteristicHorizontal);
  SliderService.addCharacteristic(SliderCharacteristicVertical);

  // add service
  BLE.addService(SliderService);

  // assign event handlers for characteristic
  SliderCharacteristicHorizontal.setEventHandler(BLEWritten, SliderCharacteristicHorizontalWritten);
  SliderCharacteristicVertical.setEventHandler(BLEWritten, SliderCharacteristicVerticalWritten);

  // set the initial value for the characeristic:
  SliderCharacteristicHorizontal.writeValue(0);
  SliderCharacteristicVertical.writeValue(0);

  // start advertising
  BLE.advertise();

  Serial.println("BLE Peripheral Ready");

  //Start ticker for motor with 1ms
 //motor_ticker.attach_us(&step_motor, 100);


}

void loop() {

      //Polls
      BLE.poll();


//      //Read IMU data and update angle
//      if (IMU.accelerationAvailable() && IMU.gyroscopeAvailable()) {
//        
//          IMU.readAcceleration(ax, ay, az);
//          IMU.readGyroscope(gx, gy, gz);
//
//      }

       Serial.print("slider value hor: ");
       Serial.print(slidertest);
       Serial.print(" slider value ver: ");
       Serial.println(slidertest2);

}

void SliderCharacteristicHorizontalWritten(BLEDevice central, BLECharacteristic characteristic) {

          slidertest = SliderCharacteristicHorizontal.value();

}

void SliderCharacteristicVerticalWritten(BLEDevice central, BLECharacteristic characteristic) {
       
         slidertest2 = SliderCharacteristicVertical.value();
}

void step_motor()
{
       //Here is my motor code
}

Serial_Output

I would recommend you reduce the number of BLE.poll() calls. Your BLE module may be stressed out. :slight_smile:

Did you write the app yourself? Is that complicated? Can you recommend any tutorials that help you write the app?

Thanks, will try - fingers crossed.

In respect to the app:

1 Like