Hi, I have a code that when it enters a condiditions (e.g. the acceleration is higher than 2g for 5 times) it should pause the script for a specific time (x_time). However when i create the pause the BLE connection is lost. Any suggestions on how to avoid the lost connection and creating a pause in the script?
Hi there,
You probably need it in the WhileBT.connected loop to keep the connection and always use milli’s as it’s a non-blocking in the code stream. Delay stops Everything.
HTH
GL
Hi PJ_GLasse
Thanks for the respons and the advide on using milli’s. However I still have the problem that is disconnec to the central if the pause is more than 20-30 sek. I have written an example and would appriciate any advice on how to keep the connection during the “pause”
#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;
int x_time = 40000;
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);
}
digitalWrite(LEDB, HIGH); // High == turning off the Led
Serial.print("Disconnected from central: ");
Serial.println(central.address());
}
}
HI there,
So I tweaked it and Tested with NRF connect BLE ver 4.04 seems to stay connected and doesn’t output anything until I move it, then prints paused until idle(at rest) BLE stay’s connected AOK.
Here is output from NFR connect BLE desktop
#include <ArduinoBLE.h>
#include <Arduino.h>
#include <LSM6DS3.h>
#include <Wire.h>
//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);
Serial.println("Power ON "); // Let's BEGIN!!
Serial.println("Test program compiled on " __DATE__ " at "__TIME__);
Serial.println();
myIMU.settings.gyroEnabled = 0; // Gyro currently not used, disabled to save power
while (!Serial) {
//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");
}
setupblink(); // Tests The onboard RGB LED
Serial.println("BLE: ok");
BLE.setLocalName("BLUE ACCEL");
BLE.setDeviceName("BLUE ACCEL");
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;
int x_time = 4000;// 400000
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");
}
Serial.println("run");
}
AccBLE.writeValue(accelerometerX);
}
digitalWrite(LEDB, HIGH); // High == turning off the Led
Serial.print("Disconnected from central: ");
Serial.println(central.address());
}
}
void setupblink() {
pinMode(LEDR, OUTPUT); // initialize the LED pin as an output:
pinMode(LEDG, OUTPUT); // initialize the LED pin as an output:
pinMode(LEDB, OUTPUT);
pinMode(LED_BUILTIN, OUTPUT); // initialize the LED pin as an output:
setLedRGB(false, true, false); // Red
delay(1000);
setLedRGB(true, false, false); // Green
delay(1000);
setLedRGB(false, false, true); // Blue
delay(1000);
setLedRGB(false, false, false); // OFF
}
void setLedRGB(bool red, bool green, bool blue) {
if (!blue) {
digitalWrite(LEDB, HIGH);
} else {
digitalWrite(LEDB, LOW);
}
if (!green) {
digitalWrite(LEDG, HIGH);
} else {
digitalWrite(LEDG, LOW);
}
if (!red) {
digitalWrite(LEDR, HIGH);
} else {
digitalWrite(LEDR, LOW);
}
}
I have tested your modified code, however it still disconnect if I increase the “pause” to 40 sec (40000). It works great with 4 sec pause. I am in doubt if you had no issues with 400 sec as in your comment?