Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /********* Pleasedontcode.com **********
- Pleasedontcode thanks you for automatic code generation! Enjoy your code!
- - Terms and Conditions:
- You have a non-exclusive, revocable, worldwide, royalty-free license
- for personal and commercial use. Attribution is optional; modifications
- are allowed, but you're responsible for code maintenance. We're not
- liable for any loss or damage. For full terms,
- please visit pleasedontcode.com/termsandconditions.
- - Project: **Motion Detection**
- - Source Code NOT compiled for: Arduino Mega
- - Source Code created on: 2024-11-07 22:59:56
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* buzzer will buzz if the ultrasonic sensor detects */
- /* motion or obstacle, pir sensor will detect the */
- /* motion */
- /****** END SYSTEM REQUIREMENTS *****/
- /* START CODE */
- /****** DEFINITION OF LIBRARIES *****/
- #include <ESP_AT_WiFiManager.h> //https://github.com/khoih-prog/ESP_AT_WiFiManager
- #include <NewPing.h> // Include NewPing library for ultrasonic sensor
- #include <Wire.h> // Include Wire library for I2C communication
- #include <Adafruit_Sensor.h> // Include Adafruit Sensor library
- #include <Adafruit_MPU6050.h> // Include Adafruit MPU6050 library for motion detection
- #include <Servo.h> // Include Servo library for controlling the buzzer
- /****** FUNCTION PROTOTYPES *****/
- void setup(void);
- void loop(void);
- /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
- ESP_AT_WiFiManager wifiManager; // Instance of WiFi Manager
- NewPing sonar(7, 8, 200); // Set trigger pin to 7, echo pin to 8, max distance to 200 cm
- Adafruit_MPU6050 mpu; // Create an instance of the MPU6050
- Servo buzzer; // Create a Servo object for the buzzer
- void setup(void)
- {
- Serial.begin(115200); // Start serial communication at 115200 baud
- wifiManager.autoConnect(); // Connect to WiFi
- // Initialize the ultrasonic sensor
- sonar.ping();
- // Initialize the MPU6050
- if (!mpu.begin()) {
- Serial.println("Failed to find MPU6050 chip");
- while (1);
- }
- // Attach the buzzer to pin 9
- buzzer.attach(9);
- buzzer.write(0); // Start with the buzzer off
- }
- void loop(void)
- {
- // Check for motion or obstacles
- float distance = sonar.ping_cm(); // Get distance from ultrasonic sensor
- if (distance > 0 && distance < 20) { // If an obstacle is detected within 20 cm
- buzzer.write(90); // Buzz the buzzer
- } else {
- buzzer.write(0); // Turn off the buzzer
- }
- // Check for motion using MPU6050
- sensors_event_t a, g, temp;
- mpu.getEvent(&a, &g, &temp);
- if (a.acceleration.x > 1 || a.acceleration.y > 1 || a.acceleration.z > 1) { // If motion is detected
- buzzer.write(90); // Buzz the buzzer
- } else {
- buzzer.write(0); // Turn off the buzzer
- }
- delay(100); // Small delay to avoid rapid toggling
- }
- /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement