#include <DFRobot_BMI160.h>
int t;
#define Light_string 6
DFRobot_BMI160 bmi160;
const int8_t i2c_addr = 0x69;
void setup(){
Serial.begin(115200);
delay(100);
pinMode( Light_string, OUTPUT);
//init the hardware bmin160
if (bmi160.softReset() != BMI160_OK){
Serial.println("reset false");
while(1);
}
//set and init the bmi160 i2c address
if (bmi160.I2cInit(i2c_addr) != BMI160_OK){
Serial.println("init false");
while(1);
}
}
void loop() {
int i = 0;
int rslt;
int16_t accelGyro[6]={0};
//get both accel and gyro data from bmi160
//parameter accelGyro is the pointer to store the data
rslt = bmi160.getAccelGyroData(accelGyro);
Serial.print(rslt);Serial.print("\t");
// read accelerometer:
float rawX = accelGyro[3]*3.14/180.0;
float rawY = accelGyro[4]*3.14/180.0;
float rawZ = accelGyro[5]*3.14/180.0;
Serial.print(rawX);Serial.print("\t");
Serial.print(rawY);Serial.print("\t");
Serial.print(rawZ);Serial.print("\t");
float angle = atan2(rawZ, rawY); // the funtion atan2() converts x and y forces into an angle in radians. cool! Output is -3.14 to 3.14
Serial.print(abs(angle));Serial.print("\t\n");
if (abs(angle) > 0.2 && abs(angle) < 2.8) { // digital pins are down
/// turn lights off in this position
analogWrite( Light_string, 255);
}else{
analogWrite( Light_string, LOW);
}
delay(500);
}