Klaus_K:
#include <ArduinoBLE.h>
#include <Arduino_LSM9DS1.h>
#define BLE_UUID_TEST_SERVICE "9A48ECBA-2E92-082F-C079-9E75AAE428B1"
#define BLE_UUID_ACCELERATION "2713"
#define BLE_UUID_COUNTER "1A3AC130-31EE-758A-BC50-54A61958EF81"
#define BLE_UUID_RESET_COUNTER "FE4E19FF-B132-0099-5E94-3FFB2CF07940"
BLEService testService( BLE_UUID_TEST_SERVICE );
BLEFloatCharacteristic accelerationCharacteristic( BLE_UUID_ACCELERATION, BLERead | BLENotify );
BLEUnsignedLongCharacteristic counterCharacteristic( BLE_UUID_COUNTER, BLERead | BLENotify );
BLEBoolCharacteristic resetCounterCharacteristic( BLE_UUID_RESET_COUNTER, BLEWriteWithoutResponse );
const int BLE_LED_PIN = LED_BUILTIN;
const int RSSI_LED_PIN = LED_PWR;
void setup()
{
Serial.begin( 9600 );
pinMode( BLE_LED_PIN, OUTPUT );
pinMode( RSSI_LED_PIN, OUTPUT );
if ( !IMU.begin() )
{
Serial.println( "Failed to initialize IMU!" );
while ( 1 );
}
if( setupBleMode() )
{
digitalWrite( BLE_LED_PIN, HIGH );
}
}
void loop()
{
static unsigned long counter = 0;
static long previousMillis = 0;
BLEDevice central = BLE.central();
if ( central )
{
Serial.print( "Connected to central: " );
Serial.println( central.address() );
while ( central.connected() )
{
if( resetCounterCharacteristic.written() )
{
counter = 0;
}
long interval = 20;
unsigned long currentMillis = millis();
if( currentMillis - previousMillis > interval )
{
previousMillis = currentMillis;
Serial.print( "Central RSSI: " );
Serial.println( central.rssi() );
if( central.rssi() != 0 )
{
digitalWrite( RSSI_LED_PIN, LOW );
float accelerationX, accelerationY, accelerationZ;
if ( IMU.accelerationAvailable() )
{
IMU.readAcceleration( accelerationX, accelerationY, accelerationZ );
accelerationCharacteristic.writeValue( accelerationX );
}
counter++;
counterCharacteristic.writeValue( counter );
}
else
{
digitalWrite( RSSI_LED_PIN, HIGH );
}
}
}
}
}
bool setupBleMode()
{
if ( !BLE.begin() )
{
return false;
}
BLE.setDeviceName( "Arduino Nano 33 BLE" );
BLE.setLocalName( "Arduino Nano 33 BLE" );
BLE.setAdvertisedService( testService );
testService.addCharacteristic( accelerationCharacteristic );
testService.addCharacteristic( counterCharacteristic );
testService.addCharacteristic( resetCounterCharacteristic );
BLE.addService( testService );
accelerationCharacteristic.writeValue( 0.0 );
counterCharacteristic.writeValue( 0 );
BLE.advertise();
return true;
}
Hi Klaus_K
In the above code that you provided earlier, accelerationX value is being written to a characteristic which we can read from our central device. However, the frequency at which I can call the data from the central device is limited in my case.
Is there a way that Arduino keeps a buffer and keeps recording, let's say 20 consecutive samples, which I can later read from my central device? For example, if I can set read mode as 'latest' and read the last 20 samples from characteristic?
How should the above code be modified for that?