Advertisement
pleasedontcode

**Motion Detection** rev_01

Nov 7th, 2024
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: **Motion Detection**
  13.     - Source Code NOT compiled for: Arduino Mega
  14.     - Source Code created on: 2024-11-07 22:59:56
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* buzzer will buzz if the ultrasonic sensor detects */
  21.     /* motion or obstacle, pir sensor will detect the */
  22.     /* motion */
  23. /****** END SYSTEM REQUIREMENTS *****/
  24.  
  25. /* START CODE */
  26.  
  27. /****** DEFINITION OF LIBRARIES *****/
  28. #include <ESP_AT_WiFiManager.h> //https://github.com/khoih-prog/ESP_AT_WiFiManager
  29. #include <NewPing.h>             // Include NewPing library for ultrasonic sensor
  30. #include <Wire.h>               // Include Wire library for I2C communication
  31. #include <Adafruit_Sensor.h>    // Include Adafruit Sensor library
  32. #include <Adafruit_MPU6050.h>   // Include Adafruit MPU6050 library for motion detection
  33. #include <Servo.h>              // Include Servo library for controlling the buzzer
  34.  
  35. /****** FUNCTION PROTOTYPES *****/
  36. void setup(void);
  37. void loop(void);
  38.  
  39. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  40. ESP_AT_WiFiManager wifiManager; // Instance of WiFi Manager
  41. NewPing sonar(7, 8, 200);        // Set trigger pin to 7, echo pin to 8, max distance to 200 cm
  42. Adafruit_MPU6050 mpu;            // Create an instance of the MPU6050
  43. Servo buzzer;                    // Create a Servo object for the buzzer
  44.  
  45. void setup(void)
  46. {
  47.     Serial.begin(115200);        // Start serial communication at 115200 baud
  48.     wifiManager.autoConnect();   // Connect to WiFi
  49.  
  50.     // Initialize the ultrasonic sensor
  51.     sonar.ping();
  52.  
  53.     // Initialize the MPU6050
  54.     if (!mpu.begin()) {
  55.         Serial.println("Failed to find MPU6050 chip");
  56.         while (1);
  57.     }
  58.  
  59.     // Attach the buzzer to pin 9
  60.     buzzer.attach(9);
  61.     buzzer.write(0); // Start with the buzzer off
  62. }
  63.  
  64. void loop(void)
  65. {
  66.     // Check for motion or obstacles
  67.     float distance = sonar.ping_cm(); // Get distance from ultrasonic sensor
  68.     if (distance > 0 && distance < 20) { // If an obstacle is detected within 20 cm
  69.         buzzer.write(90); // Buzz the buzzer
  70.     } else {
  71.         buzzer.write(0); // Turn off the buzzer
  72.     }
  73.  
  74.     // Check for motion using MPU6050
  75.     sensors_event_t a, g, temp;
  76.     mpu.getEvent(&a, &g, &temp);
  77.     if (a.acceleration.x > 1 || a.acceleration.y > 1 || a.acceleration.z > 1) { // If motion is detected
  78.         buzzer.write(90); // Buzz the buzzer
  79.     } else {
  80.         buzzer.write(0); // Turn off the buzzer
  81.     }
  82.  
  83.     delay(100); // Small delay to avoid rapid toggling
  84. }
  85.  
  86. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement