#include <MPU9250_WE.h>
#include <Wire.h>
#define MPU9250_ADDR 0x68
MPU9250_WE myMPU9250 = MPU9250_WE(MPU9250_ADDR);
const int ledPin1 = 12; // Pin for the first LED
const int ledPin2 = 14; // Pin for the second LED
const int ledPin3 = 13; // Pin for the third LED
void setup() {
Serial.begin(115200);
Wire.begin();
if (!myMPU9250.init()) {
Serial.println("MPU9250 does not respond");
} else {
Serial.println("MPU9250 is connected");
}
Serial.println("Position your MPU9250 flat and don't move it - calibrating...");
delay(1000);
myMPU9250.autoOffsets();
Serial.println("Done!");
myMPU9250.setSampleRateDivider(5);
myMPU9250.setAccRange(MPU9250_ACC_RANGE_2G);
myMPU9250.enableAccDLPF(true);
myMPU9250.setAccDLPF(MPU9250_DLPF_6);
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
}
void loop() {
xyzFloat accRaw = myMPU9250.getAccRawValues();
xyzFloat accCorrRaw = myMPU9250.getCorrectedAccRawValues();
xyzFloat gValue = myMPU9250.getGValues();
float resultantG = myMPU9250.getResultantG(gValue);
Serial.println("Raw acceleration values (x, y, z):");
Serial.print(accRaw.x);
Serial.print(" ");
Serial.print(accRaw.y);
Serial.print(" ");
Serial.println(accRaw.z);
Serial.println("Corrected ('calibrated') acceleration values (x, y, z):");
Serial.print(accCorrRaw.x);
Serial.print(" ");
Serial.print(accCorrRaw.y);
Serial.print(" ");
Serial.println(accCorrRaw.z);
Serial.println("g values (x, y, z):");
Serial.print(gValue.x);
Serial.print(" ");
Serial.print(gValue.y);
Serial.print(" ");
Serial.println(gValue.z);
Serial.print("Resultant g: ");
Serial.println(resultantG);
// Check for significant acceleration change (you can adjust the threshold)
if (abs(accRaw.x) > 0.1 || abs(accRaw.y) > 0.1 || abs(accRaw.z) > 0.1) {
digitalWrite(ledPin1, HIGH);
digitalWrite(ledPin2, HIGH);
digitalWrite(ledPin3, HIGH);
} else {
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin3, LOW);
}
// You can add similar logic for the other two LEDs (ledPin2 and ledPin3)
delay(1000);
}