I used this below mentioned code . The board must send the data of only accelerometer sensor every 10 milliseconds to the dashboard but when we are recording it, the dt value is keep changing. Because of this, we are not able to plot the FFT. So, how can we fix the one sampling rate?
Many Thanks!
#include "Nicla_System.h"
#include "Arduino_BHY2.h"
#include <ArduinoBLE.h>
#define BLE_SENSE_UUID(val) ("19b10000-" val "-537e-4f6c-d104768a1214")
const int VERSION = 0x00000000;
BLEService service(BLE_SENSE_UUID("0000"));
BLEUnsignedIntCharacteristic versionCharacteristic(BLE_SENSE_UUID("1001"), BLERead);
BLEFloatCharacteristic temperatureCharacteristic(BLE_SENSE_UUID("2001"), BLERead | BLENotify);
BLEUnsignedIntCharacteristic humidityCharacteristic(BLE_SENSE_UUID("3001"), BLERead | BLENotify);
BLEFloatCharacteristic pressureCharacteristic(BLE_SENSE_UUID("4001"), BLERead | BLENotify);
BLECharacteristic accelerometerCharacteristic(BLE_SENSE_UUID("5001"), BLERead | BLENotify, 3 * sizeof(float)); // Array of 3x 2 Bytes, XYZ
BLECharacteristic gyroscopeCharacteristic(BLE_SENSE_UUID("6001"), BLERead | BLENotify, 3 * sizeof(float)); // Array of 3x 2 Bytes, XYZ
BLECharacteristic quaternionCharacteristic(BLE_SENSE_UUID("7001"), BLERead | BLENotify, 4 * sizeof(float)); // Array of 4x 2 Bytes, XYZW
BLECharacteristic rgbLedCharacteristic(BLE_SENSE_UUID("8001"), BLERead | BLEWrite, 3 * sizeof(byte)); // Array of 3 bytes, RGB
BLEFloatCharacteristic bsecCharacteristic(BLE_SENSE_UUID("9001"), BLERead | BLENotify);
BLEIntCharacteristic co2Characteristic(BLE_SENSE_UUID("9002"), BLERead | BLENotify);
BLEUnsignedIntCharacteristic gasCharacteristic(BLE_SENSE_UUID("9003"), BLERead | BLENotify);
// String to calculate the local and device name
String name;
Sensor temperature(SENSOR_ID_TEMP);
Sensor humidity(SENSOR_ID_HUM);
Sensor pressure(SENSOR_ID_BARO);
Sensor gas(SENSOR_ID_GAS);
SensorXYZ gyroscope(SENSOR_ID_GYRO);
SensorXYZ accelerometer(SENSOR_ID_ACC);
SensorQuaternion quaternion(SENSOR_ID_RV);
SensorBSEC bsec(SENSOR_ID_BSEC);
void setup() {
Serial.begin(115200);
Serial.println("Start");
nicla::begin();
nicla::leds.begin();
nicla::leds.setColor(green);
// Sensors initialization
BHY2.begin(NICLA_STANDALONE);
temperature.begin();
humidity.begin();
pressure.begin();
gyroscope.begin();
accelerometer.begin();
quaternion.begin();
bsec.begin();
gas.begin();
if (!BLE.begin()) {
Serial.println("Failed to initialize BLE!");
while (1);
}
String address = BLE.address();
Serial.print("address = ");
Serial.println(address);
address.toUpperCase();
name = "NiclaSenseME-";
name += address[address.length() - 5];
name += address[address.length() - 4];
name += address[address.length() - 2];
name += address[address.length() - 1];
Serial.print("name = ");
Serial.println(name);
BLE.setLocalName(name.c_str());
BLE.setDeviceName(name.c_str());
BLE.setAdvertisedService(service);
// Add all the previously defined Characteristics
service.addCharacteristic(temperatureCharacteristic);
service.addCharacteristic(humidityCharacteristic);
service.addCharacteristic(pressureCharacteristic);
service.addCharacteristic(versionCharacteristic);
service.addCharacteristic(accelerometerCharacteristic);
service.addCharacteristic(gyroscopeCharacteristic);
service.addCharacteristic(quaternionCharacteristic);
service.addCharacteristic(bsecCharacteristic);
service.addCharacteristic(co2Characteristic);
service.addCharacteristic(gasCharacteristic);
service.addCharacteristic(rgbLedCharacteristic);
// Disconnect event handler
BLE.setEventHandler(BLEDisconnected, blePeripheralDisconnectHandler);
// Sensors event handlers
temperatureCharacteristic.setEventHandler(BLERead, onTemperatureCharacteristicRead);
humidityCharacteristic.setEventHandler(BLERead, onHumidityCharacteristicRead);
pressureCharacteristic.setEventHandler(BLERead, onPressureCharacteristicRead);
bsecCharacteristic.setEventHandler(BLERead, onBsecCharacteristicRead);
co2Characteristic.setEventHandler(BLERead, onCo2CharacteristicRead);
gasCharacteristic.setEventHandler(BLERead, onGasCharacteristicRead);
rgbLedCharacteristic.setEventHandler(BLEWritten, onRgbLedCharacteristicWrite);
versionCharacteristic.setValue(VERSION);
BLE.addService(service);
BLE.advertise();
}
void loop() {
static unsigned long lastUpdateTime = 0;
unsigned long currentTime = millis();
if (currentTime - lastUpdateTime >= 10) {
lastUpdateTime = currentTime;
BHY2.update();
if (gyroscopeCharacteristic.subscribed()) {
float x = gyroscope.x();
float y = gyroscope.y();
float z = gyroscope.z();
float gyroscopeValues[3] = {x, y, z};
gyroscopeCharacteristic.writeValue(gyroscopeValues, sizeof(gyroscopeValues));
}
if (accelerometerCharacteristic.subscribed()) {
float x = accelerometer.x();
float y = accelerometer.y();
float z = accelerometer.z();
float accelerometerValues[3] = {x, y, z};
accelerometerCharacteristic.writeValue(accelerometerValues, sizeof(accelerometerValues));
}
if (quaternionCharacteristic.subscribed()) {
float x = quaternion.x();
float y = quaternion.y();
float z = quaternion.z();
float w = quaternion.w();
float quaternionValues[4] = {x, y, z, w};
quaternionCharacteristic.writeValue(quaternionValues, sizeof(quaternionValues));
}
if (temperatureCharacteristic.subscribed()) {
float temperatureValue = temperature.value();
temperatureCharacteristic.writeValue(temperatureValue);
}
if (humidityCharacteristic.subscribed()) {
uint8_t humidityValue = humidity.value() + 0.5f;
humidityCharacteristic.writeValue(humidityValue);
}
if (pressureCharacteristic.subscribed()) {
float pressureValue = pressure.value();
pressureCharacteristic.writeValue(pressureValue);
}
if (bsecCharacteristic.subscribed()) {
float airQuality = float(bsec.iaq());
bsecCharacteristic.writeValue(airQuality);
}
if (co2Characteristic.subscribed()) {
uint32_t co2 = bsec.co2_eq();
co2Characteristic.writeValue(co2);
}
if (gasCharacteristic.subscribed()) {
unsigned int g = gas.value();
gasCharacteristic.writeValue(g);
}
}
BLE.poll();
}
void blePeripheralDisconnectHandler(BLEDevice central) {
nicla::leds.setColor(red);
}
void onTemperatureCharacteristicRead(BLEDevice central, BLECharacteristic characteristic) {
float temperatureValue = temperature.value();
temperatureCharacteristic.writeValue(temperatureValue);
}
void onHumidityCharacteristicRead(BLEDevice central, BLECharacteristic characteristic) {
uint8_t humidityValue = humidity.value() + 0.5f;
humidityCharacteristic.writeValue(humidityValue);
}
void onPressureCharacteristicRead(BLEDevice central, BLECharacteristic characteristic) {
float pressureValue = pressure.value();
pressureCharacteristic.writeValue(pressureValue);
}
void onBsecCharacteristicRead(BLEDevice central, BLECharacteristic characteristic) {
float airQuality = float(bsec.iaq());
bsecCharacteristic.writeValue(airQuality);
}
void onCo2CharacteristicRead(BLEDevice central, BLECharacteristic characteristic) {
uint32_t co2 = bsec.co2_eq();
co2Characteristic.writeValue(co2);
}
void onGasCharacteristicRead(BLEDevice central, BLECharacteristic characteristic) {
unsigned int g = gas.value();
gasCharacteristic.writeValue(g);
}
void onRgbLedCharacteristicWrite(BLEDevice central, BLECharacteristic characteristic) {
byte r = rgbLedCharacteristic[0];
byte g = rgbLedCharacteristic[1];
byte b = rgbLedCharacteristic[2];
nicla::leds.setColor(r, g, b);
}