Xiao Sense Accelerometer Examples and Low Power

Nice work daCoder,
I’m using both Motion detection and Fall detection With BLE . it took allot of trial an Error to get the correct Sensitivity ,Duration, Tap THS reg and the ODR, for both features to work independently, Quiet and Shock time windows 7F and in combination.
I have some comments in there as well. Here are some snips. When I get more time I’ll massage it to work on Dev board and post the complete code. It uses BLE to turn on and off each feature and Notifies the MIT app inventor 2 app. So I’m interested in battery power saving and I’m currently using the double-tap wake up from sleep to save battery now, but only ON & off are my only options, a low power operational mode WOULD be preferred. :wink:

// Add default values for enabling/disabling functions
bool enableFallDetection = true;
bool enableMotionDetection = false;
// Fall detection parameters
float impactThreshold = 1.5;  // G-force threshold for impact (default value)CD CC 0C 40* HEX 20 40 =10G, CD CC 0C 41 =8.80G ,20 41 =2.5 G, 10 40= 2.25G,80 3F 1.0G,C0 3F = 1.5G (liked 2.2)
const int impactTime = 250;   // Time (ms) to wait for impact
bool impactDetected = false;
unsigned long impactTimestamp;
unsigned long lastReadTimestamp = 0;
const int readInterval = 50;  // Time interval (ms) to read acceleration data
// Motion detection parameters
float motionSensitivity =  0.026; // Sensitivity threshold for motion detection (default value)CD CC 4C 3D (8F C2 F5 3E),0.42 F4 FD D4 3E,F4 FD 44 3D=.05*
unsigned long lastMotionTimestamp = 0;
const int motionInterval = 1000;  // Time interval (ms) to check for motion
//bool enableDoupleTap = 0;         // ** turned off for fall demo**
unsigned long lastActivityTime = millis();
String statusString;
//Create a instance of class BLE services & characteristics
BLEService ledService("19B10000-E8F2-537E-4F6C-D104768A1214");  // Bluetooth® Low Energy LED Service
// Bluetooth® Low Energy LED Switch Characteristic - custom 128-bit UUID, read and writable by central
BLEByteCharacteristic switchCharacteristic("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite);

BLEService fallService("19B10002-E8F2-537E-4F6C-D104768A1214");
BLEStringCharacteristic fallCharacteristic("19B10003-E8F2-537E-4F6C-D104768A1214", BLERead | BLENotify, 20);//4E-6F-20-46-61-6C-6C"No Fall"(READ), Notify ON, 46414c4c "FALL"
BLEFloatCharacteristic impactLevelCharacteristic("19B10004-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite);
BLEBoolCharacteristic enableFallDetectionCharacteristic("19B10008-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite);
// BLE Service and Characteristics
BLEService motionService("19B10005-E8F2-537E-4F6C-D104768A1214");
BLEStringCharacteristic motionCharacteristic("19B10006-E8F2-537E-4F6C-D104768A1214", BLERead | BLENotify, 20);//4E-6F-20-4D-6F-74-69-6F-6E"No Motion", (READ), Notify ON, 4d4f54494f4e "MOTION"
BLEFloatCharacteristic motionSensitivityCharacteristic("19B10007-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite);
BLEBoolCharacteristic enableMotionDetectionCharacteristic("19B10009-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite);

BLEService systemStatusService("19B1000C-E8F2-537E-4F6C-D104768A1214"); // UUID for the service
BLEStringCharacteristic systemStatusCharacteristic("19B1000B-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite, 200 ); // UUID for the characteristic

Double Tap Wake up…

void setupDoubleTapInterrupt(bool enable) {
  uint8_t error = 0;
  uint8_t dataToWrite = 0;

  // Double Tap Config
  myIMU.writeRegister(LSM6DS3_ACC_GYRO_CTRL1_XL, 0x60);
  myIMU.writeRegister(LSM6DS3_ACC_GYRO_TAP_CFG1, 0x8E);
  myIMU.writeRegister(LSM6DS3_ACC_GYRO_TAP_THS_6D, 0x85);  // Changed the value to 0x01 for increased sensitivity was 85 Set tap threshold 8C,03 in demo
  //myIMU.writeRegister(LSM6DS3_ACC_GYRO_TAP_THS_6D, tapThreshold);
  myIMU.writeRegister(LSM6DS3_ACC_GYRO_INT_DUR2, 0x7F);  // Set Duration, Quiet and Shock time windows 7F
  myIMU.writeRegister(LSM6DS3_ACC_GYRO_WAKE_UP_THS, 0x80); // Single & double-tap enabled (SINGLE_DOUBLE_TAP = 1)
  if (enable) {
     Serial.println("Double Tap ON!");
    myIMU.writeRegister(LSM6DS3_ACC_GYRO_MD1_CFG, 0x08);  // Enable the double-tap interrupt
  } else {
    myIMU.writeRegister(LSM6DS3_ACC_GYRO_MD1_CFG, 0x00);  // Disable the double-tap interrupt
    Serial.println("Double Tap OFF");
  }
}

GL :slight_smile: PJ