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(&quot;Device error&quot;);
} else {
  Serial.println(&quot;Device OK!&quot;);
}
// init BLE
if (!BLE.begin()) {
  Serial.println(&quot;BLE: failed&quot;);
}
Serial.println(&quot;BLE: ok&quot;);

BLE.setAdvertisedService(myService);
myService.addCharacteristic(AccBLE);
BLE.addService(myService);
AccBLE.writeValue(0);
// start advertising
BLE.advertise();
Serial.println(&quot;Advertising started&quot;);
Serial.println(&quot;Bluetooth device active, waiting for connections...&quot;);

} } 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 &gt; 2) {
    BreakPreviousMillis = millis();
    while (millis() &lt; BreakPreviousMillis + x_time) {
      Serial.println(&quot;pause&quot;);
    }
  }
  AccBLE.writeValue(accelerometerX);
  Serial.print(&quot;*************LOOP TIME[uS] = &quot;);
  Serial.println(micros() - timestamp);  // 13HZ =79000-80000 uS   // used to check framerate
}

digitalWrite(LEDB, HIGH);  // High == turning off the Led
Serial.print(&quot;Disconnected from central: &quot;);
Serial.println(central.address());

} }

Masilila
  • 121
  • 1

0 Answers0