#include <Wire.h>
#include <MPU6050.h>
MPU6050 mpu;
const int buzzerPin = 2; // Replace with the desired buzzer pin
// Define the threshold value for triggering the buzzer
const int threshold = 5000; // Adjust this value as needed
void setup() {
Serial.begin(9600);
Wire.begin();
mpu.initialize();
pinMode(buzzerPin, OUTPUT);
}
void loop() {
int16_t ax, ay, az, temp;
mpu.getAcceleration(&ax, &ay, &az);
temp = mpu.getTemperature();
// Calculate the magnitude of acceleration
float magnitude = sqrt(ax * ax + ay * ay + az * az);
Serial.print("Acceleration - X: ");
Serial.print(ax);
Serial.print(" Y: ");
Serial.print(ay);
Serial.print(" Z: ");
Serial.println(az);
Serial.print("Magnitude: ");
Serial.println(magnitude);
Serial.print("Temperature: ");
Serial.println(temp);
// Check if the magnitude exceeds the threshold
if (magnitude > threshold) {
// Activate the buzzer
digitalWrite(buzzerPin, HIGH);
delay(5000); // Adjust the duration of the buzzer sound as needed
digitalWrite(buzzerPin, LOW);
}
delay(100); // Adjust the delay between readings as needed
}