Hardware: arduino mkr nb1500 + arduino mkr imu shield
Goal: after x seconds of no motion detected activate low power mode, once motion is detected activate normal power mode
Currently i managed to set the imu in low power mode, which is activated after 5s of no motion, and is set in normal power mode once motion is detected.
Now i would like to also put the host (arduino mkr nb1500) into low power mode once the imu low power mode is triggered. This should be possible with the use of the interrupt pin.
I am having trouble achieving this in actual code. I am able to read the INT_STA register and detect the interrupts; 64 is motion detected, 128 is no motion detected.
I thought about using the arduino low power library [Arduino Low Power - Arduino Reference], but even a simple timed sleep does not work. It doesn't wake up anymore.
Any tips/hints to lead me in the right directions are appreciated!
#include <Adafruit_BNO055.h>
#include "ArduinoLowPower.h"
#define BNO055_ADDRESS 0x28
#define BNO055_INT_STA 0x37
#define BNO055_PWR_MODE 0x3E
#define BNO055_OPR_MODE 0x3D
#define BNO055_ACC_NM_SET 0x16
#define BNO055_PAGE_ID 0x07
Adafruit_BNO055 bno = Adafruit_BNO055();
imu::Vector<3> acc;
int x = 1;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
while (!Serial);
Serial.println("Serial communication started");
initializeIMU();
}
void loop() {
// put your main code here, to run repeatedly:
acc = bno.getVector(Adafruit_BNO055::VECTOR_ACCELEROMETER);
Serial.print("Acc_x: "); Serial.println(acc.x());
// interrupt pin from imu: 64 = any motion detected, 128 = no motion detected
uint8_t imuTrigger = readByte(BNO055_ADDRESS, BNO055_INT_STA);
Serial.println(imuTrigger);
// Sleep for 10s, it doesn't wake up after 10s which i did expect to happen
LowPower.sleep(10000);
}
/* -----------------------------------------------------------------------------------------------------*/
void writeByte(uint8_t address, uint8_t subAddress, uint8_t data)
{
Wire.beginTransmission(address); // Initialize the Tx buffer
Wire.write(subAddress); // Put slave register address in Tx buffer
Wire.write(data); // Put data in Tx buffer
Wire.endTransmission(); // Send the Tx buffer
}
uint8_t readByte(uint8_t address, uint8_t subAddress)
{
uint8_t data; // `data` will store the register data
Wire.beginTransmission(address); // Initialize the Tx buffer
Wire.write(subAddress); // Put slave register address in Tx buffer
Wire.endTransmission(false); // Send the Tx buffer, but send a restart to keep connection alive
Wire.requestFrom(address, (size_t) 1); // Read one byte from slave register address
data = Wire.read(); // Fill Rx buffer with result
return data; // Return data read from slave register
}
void initializeIMU()
{
bno.begin();
delay(20);
writeByte(BNO055_ADDRESS, BNO055_OPR_MODE, 0x00); // Put IMU in operation mode; 0x00 = config, 0x08 is IMU fusion mode
delay(20);
writeByte(BNO055_ADDRESS, BNO055_PWR_MODE, 0x01); // Put IMU in low power mode
Serial.println("IMU low power mode selected");
delay(20);
writeByte(BNO055_ADDRESS, BNO055_PAGE_ID, 0x01); // SELECT PAGE 1 TO CONFIGRE SETTINGS of page 1
delay(20);
// writeByte(BNO055_ADDRESS, BNO055_ACC_NM_SET, 0x7E); // 336s (~5.5 min), max duration
writeByte(BNO055_ADDRESS, BNO055_ACC_NM_SET, 0x09); // 5s usefull for testing
Serial.println("no motion to low power duration: 5s");
delay(20);
writeByte(BNO055_ADDRESS, BNO055_PAGE_ID, 0x00); // SELECT PAGE 0 TO CONFIGRE SETTINGS of page 0
delay(20);
writeByte(BNO055_ADDRESS, BNO055_OPR_MODE, 0x08); // Operation mode: IMU fusion mode
Serial.println("IMU fusion mode selected");
delay(100);
}
