2

I have a code that should pause the script for a specific time (x_time). if x_time is above 30-40 sec the BLE connection to the android phone is lost. Any suggestions on how to avoid the lost connection and creating a pause in the script?

code:

#include "LSM6DS3.h" #include "Wire.h" #include <ArduinoBLE.h> // Arduino BLE library //Create a instance of class LSM6DS3 LSM6DS3 myIMU(I2C_MODE, 0x6A); //I2C device address 0x6A const char* UUID_serv = "75681d64-fd7e-40f7-874f-c6f2e7ec996c"; // UUids for accelerometer characteristics const char* UUID_AccBLE = "75681d64-dd7e-40f7-874f-c6f2e7ec996c"; BLEService myService(UUID_serv); BLEIntCharacteristic AccBLE(UUID_AccBLE, BLERead | BLENotify); void setup() { // put your setup code here, to run once: Serial.begin(9600); while (!Serial) { pinMode(LEDB, OUTPUT); // onboard led blue //Call .begin() to configure the IMUs if (myIMU.begin() != 0) { Serial.println("Device error"); } else { Serial.println("Device OK!"); } // init BLE if (!BLE.begin()) { Serial.println("BLE: failed"); } Serial.println("BLE: ok"); BLE.setAdvertisedService(myService); myService.addCharacteristic(AccBLE); BLE.addService(myService); AccBLE.writeValue(0); // start advertising BLE.advertise(); Serial.println("Advertising started"); Serial.println("Bluetooth device active, waiting for connections..."); } } float accelerometerX; unsigned long BreakPreviousMillis; unsigned long timestamp = micros(); int x_time = 50000; void loop() { BLEDevice central = BLE.central(); if (central) { Serial.print("Connected to central: "); while (central.connected()) { accelerometerX = myIMU.readFloatAccelX(); if (accelerometerX > 2) { BreakPreviousMillis = millis(); while (millis() < BreakPreviousMillis + x_time) { Serial.println("pause"); } } AccBLE.writeValue(accelerometerX); Serial.print("*************LOOP TIME[uS] = "); Serial.println(micros() - timestamp); // 13HZ =79000-80000 uS // used to check framerate } digitalWrite(LEDB, HIGH); // High == turning off the Led Serial.print("Disconnected from central: "); Serial.println(central.address()); } } 
5
  • 1
    Which core? There are two of them, one has RTOS and second mbed-os (like Arduino 33 nano ble), there might be some differences. Or it needs calling some method to handle events. For example BLE.poll(); or something Commented Apr 3, 2023 at 8:39
  • Thanks for the comment. I use the mbed-os core Commented Apr 3, 2023 at 9:05
  • 1
    One more thought - add some delay in that while loop, maybe the BLE service is running on lower priority thread and it's "starving" for resources Commented Apr 3, 2023 at 9:13
  • 1
    you can just use delay(x_time); instead of the while loop Commented Apr 3, 2023 at 9:18
  • 1
    Thanks for the response. I tried the delay(x_time) however it was even worse and disconnected af 10-15 sec. Commented Apr 3, 2023 at 10:54

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.