// #include <Adafruit_MPU6050.h>
// #include <Adafruit_Sensor.h>
// // #include <Wire.h>
// // Adafruit_MPU6050 m_p_u;
// // void setup() {
// // // put your setup code here, to run once:
// // Serial.begin(115200);
// // while(!Serial)
// // delay(20);
// // if(!m_p_u.begin()){
// // while(1){
// // delay(20);
// // }
// // }
// // }
// // void loop() {
// // sensors_event_t acc, gcc, temp;
// // m_p_u.getEvent(&acc, &gcc, &temp);
// // Serial.println("Acceleration on x axes");
// // Serial.println(acc.acceleration.x);
// // delay(1000); // this speeds up the simulation
// // Serial.println("Acceleration on y axes");
// // Serial.println(acc.acceleration.y);
// // delay(1000);
// // Serial.println("Acceleration on z axes");
// // Serial.println(acc.acceleration.z);
// // delay(1000);
// // Serial.println("Rotation of x axes: ");
// // Serial.println((gcc.gyro.x)*180/3.14);
// // delay(1000);
// // }
// // #include <Wire.h>
// // // #include <MPU6050.h> // Include the MPU6050 library
// // Adafruit_MPU6050 mpu;
// // // ... (Previous code remains unchanged)
// // void setup() {
// // // ... (Previous setup code remains unchanged)
// // // Initialize the MPU6050
// // Wire.begin();
// // mpu.initialize();
// // }
// // void loop() {
// // int lightState = digitalRead(DO_PIN);
// // if (lightState == HIGH) {
// // // Serial.println("The light is NOT present");
// // turnServo();
// // digitalWrite(LED_PIN, LOW); // Turn on the LED
// // } else {
// // // Serial.println("The light is present");
// // digitalWrite(LED_PIN, HIGH); // Turn off the LED
// // printDHTData();
// // printWeight();
// // detectMotion(); // New function to detect motion
// // }
// // }
// // void detectMotion() {
// // // Read accelerometer values from MPU6050
// // int16_t ax, ay, az;
// // mpu.getAcceleration(&ax, &ay, &az);
// // // Calculate total acceleration
// // float totalAcceleration = sqrt(ax * ax + ay * ay + az * az);
// // // Set a threshold for motion detection (adjust as needed)
// // float motionThreshold = 5.0;
// // // Check if the total acceleration exceeds the threshold
// // if (totalAcceleration > motionThreshold) {
// // Serial.println("Motion detected! Beehive security alert!");
// // // Add any additional actions or alerts here
// // }
// // delay(1000); // Adjust delay as needed
// // }
// // #include <Adafruit_MPU6050.h>
// // #include <Adafruit_Sensor.h>
// // #include <Wire.h>
// // // #include <MPU6050.h>
// // Adafruit_MPU6050 mpu;
// // void setup() {
// // Serial.begin(115200);
// // Wire.begin(21, 22); // Specify SDA and SCL pins
// // mpu.initialize();
// // Serial.println("Anti-Theft System: MPU6050 Movement Detection");
// // }
// // void loop() {
// // int16_t ax, ay, az;
// // mpu.getAcceleration(&ax, &ay, &az);
// // // Set a threshold for motion detection (adjust as needed)
// // int motionThreshold = 500;
// // // Calculate total acceleration
// // int totalAcceleration = abs(ax) + abs(ay) + abs(az);
// // // Check if the total acceleration exceeds the threshold
// // if (totalAcceleration > motionThreshold) {
// // Serial.println("Movement detected! Anti-Theft Alert!");
// // // Add any additional actions or alerts here
// // }
// // delay(1000); // Adjust delay as needed
// // }
// #include <Adafruit_MPU6050.h>
// #include <Adafruit_Sensor.h>
// #include <Wire.h>
// Adafruit_MPU6050 mpu;
// void setup() {
// Serial.begin(115200);
// Wire.begin(); // Remove the specific SDA and SCL pin arguments as they are not necessary
// mpu.begin(); // Use mpu.begin() instead of mpu.initialize()
// Serial.println("Anti-Theft System: MPU6050 Movement Detection");
// }
// void loop() {
// sensors_event_t a, g, temp;
// mpu.getEvent(&a, &g, &temp); // Use getEvent to obtain accelerometer data
// // Extract accelerometer values
// float ax = a.acceleration.x;
// float ay = a.acceleration.y;
// float az = a.acceleration.z;
// // Set a threshold for motion detection (adjust as needed)
// float motionThreshold = 0.5; // Adjusted to a float value for better sensitivity
// // Calculate total acceleration
// float totalAcceleration = sqrt(ax * ax + ay * ay + az * az);
// // Check if the total acceleration exceeds the threshold
// if (totalAcceleration > motionThreshold) {
// Serial.println("Movement detected! Anti-Theft Alert!");
// // Add any additional actions or alerts here
// }
// delay(1000); // Adjust delay as needed
// }
#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
#include <Wire.h>
Adafruit_MPU6050 mpu;
const int buzzerPin = 13; // Define the buzzer pin as Pin 13
void setup() {
Serial.begin(115200);
Wire.begin();
mpu.begin();
pinMode(buzzerPin, OUTPUT); // Set the buzzer pin as an output
Serial.println("Anti-Theft System: MPU6050 Movement Detection");
}
void loop() {
sensors_event_t a, g, temp;
mpu.getEvent(&a, &g, &temp);
float ax = a.acceleration.x;
float ay = a.acceleration.y;
float az = a.acceleration.z;
float motionThreshold = 0.5;
float totalAcceleration = sqrt(ax * ax + ay * ay + az * az);
// Check if the total acceleration exceeds the threshold and motion is primarily in the X and Y axes
if (totalAcceleration > motionThreshold && abs(az) < 0.2) {
Serial.println("Movement detected! Anti-Theft Alert!");
digitalWrite(buzzerPin, HIGH); // Activate the buzzer
delay(500); // Buzzer on time (adjust as needed)
digitalWrite(buzzerPin, LOW); // Turn off the buzzer
// Add any additional actions or alerts here
}
delay(1000);
}