Xiao Sense Accelerometer Examples and Low Power

Hey everyone. I have a weird one that might be relevant to this thread. Basically whats going on is that I’ve got daCoder’s nudgeWakeUp code running and it works great. The weird part is that it only works great when it is plugged into my computer. The second I try to run things off of battery it will go to sleep and then wake right back up again a few milliseconds later. What’s happening here? Any help would be greatly appreciated!

Here’s my code

#include <Adafruit_SPIFlash.h>
#include <Arduino.h>
#include <U8g2lib.h>
#ifdef U8X8_HAVE_HW_SPI
#include <SPI.h>
#endif
#ifdef U8X8_HAVE_HW_I2C
#include <Wire.h>
#endif
#include <LSM6DS3.h>
#include <nrf52840.h>

Adafruit_FlashTransport_QSPI flashTransport;
U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/U8X8_PIN_NONE);
LSM6DS3 myIMU(I2C_MODE, 0x6A);

#define SLEEP_TIMEOUT 7000

const int button1Pin = D2;
const int button2Pin = D3;
unsigned long startTimeoutTime = 0;
volatile bool awake = true;

void QSPIF_sleep(void) {
  flashTransport.begin();
  flashTransport.runCommand(0xB9);
  flashTransport.end();
}

void setupWakeUpInterrupt() {  
  myIMU.writeRegister(LSM6DS3_ACC_GYRO_WAKE_UP_DUR, 0x00); // No duration
  myIMU.writeRegister(LSM6DS3_ACC_GYRO_WAKE_UP_THS, 0x05); // Set wake-up threshold
  myIMU.writeRegister(LSM6DS3_ACC_GYRO_TAP_CFG1, 0x80);    // Enable interrupts and apply slope filter; latch mode disabled
  myIMU.writeRegister(LSM6DS3_ACC_GYRO_CTRL1_XL, 0x70);    // Turn on the accelerometer
                                                           // ODR_XL = 833 Hz, FS_XL = ±2 g
  delay(4);                                                // Delay time per application note
  myIMU.writeRegister(LSM6DS3_ACC_GYRO_CTRL1_XL, 0xB0);    // ODR_XL = 1.6 Hz
  myIMU.writeRegister(LSM6DS3_ACC_GYRO_CTRL6_G, 0x10);     // High-performance operating mode disabled for accelerometer
  myIMU.writeRegister(LSM6DS3_ACC_GYRO_MD1_CFG, 0x20);     // Wake-up interrupt driven to INT1 pin

	pinMode(PIN_LSM6DS3TR_C_INT1, INPUT_PULLDOWN_SENSE);

  return;
}

void setup() {
  u8g2.begin();

  pinMode(button1Pin, INPUT_PULLUP);
  pinMode(button2Pin, INPUT_PULLUP);

  QSPIF_sleep();

  myIMU.settings.gyroEnabled = 0;
  myIMU.settings.tempEnabled = 0;
  myIMU.begin();

  u8g2.clearBuffer();
  u8g2.setFont(u8g2_font_profont11_mf);
  u8g2.setDisplayRotation(U8G2_R3);

  NRF_POWER->DCDCEN = 1;
}

void loop() {
  u8g2.setPowerSave(0);
  u8g2.clearBuffer();
  u8g2.setFont(u8g2_font_profont11_mf);
  u8g2.setCursor(10, 10);
  u8g2.print(F("Hello!"));
  u8g2.sendBuffer();

  if (startTimeoutTime == 0) startTimeoutTime = millis();

  if (startTimeoutTime > 0 && ((millis() - startTimeoutTime) > SLEEP_TIMEOUT)) {
    startTimeoutTime = 0;
    enterStandbySleep();
  }
}

void enterStandbySleep() {
  u8g2.setPowerSave(1);
  setupWakeUpInterrupt();
  pinMode(button1Pin, INPUT_SENSE_HIGH);
  delay(200);
  NRF_POWER->SYSTEMOFF = 1;
}