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: Acceleration Alert
- - Source Code NOT compiled for: Arduino Nano ESP32
- - Source Code created on: 2024-08-19 16:59:16
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* Read the values from the accelerometer. Hold the */
- /* values as reference values to be compaired with */
- /* the active values. Monitor the active values for a */
- /* change that is greater than 10% in any direction */
- /* for a period of 3 seconds. sound the buzzer when */
- /* true */
- /****** END SYSTEM REQUIREMENTS *****/
- /****** DEFINITION OF LIBRARIES *****/
- #include <Wire.h>
- #include <Adafruit_Sensor.h>
- #include <Adafruit_MPU6050.h>
- /****** FUNCTION PROTOTYPES *****/
- void setup(void);
- void loop(void);
- void updateOutputs();
- void checkAccelerationChange();
- /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
- const uint8_t Buzzer_ActiveBuzzer_output_PIN_D2 = 2;
- /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
- /***** used to store raw data *****/
- bool Buzzer_ActiveBuzzer_output_PIN_D2_rawData = 0;
- /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
- /***** used to store data after characteristic curve transformation *****/
- float Buzzer_ActiveBuzzer_output_PIN_D2_phyData = 0.0;
- // MPU6050 object
- Adafruit_MPU6050 mpu;
- // Reference acceleration values
- float referenceAccel[3] = {0.0, 0.0, 0.0}; // X, Y, Z
- unsigned long changeStartTime = 0; // Time when change started
- bool changeDetected = false; // Flag for detecting change
- void setup(void)
- {
- // Initialize serial communication
- Serial.begin(115200);
- pinMode(Buzzer_ActiveBuzzer_output_PIN_D2, OUTPUT);
- // Initialize MPU6050
- if (!mpu.begin()) {
- Serial.println("Failed to find MPU6050 chip");
- while (1) {
- delay(10);
- }
- }
- // Set accelerometer range
- mpu.setAccelerometerRange(MPU6050_RANGE_2_G);
- delay(100);
- // Read initial values for reference
- sensors_event_t a, g, temp;
- mpu.getEvent(&a, &g, &temp);
- referenceAccel[0] = a.acceleration.x;
- referenceAccel[1] = a.acceleration.y;
- referenceAccel[2] = a.acceleration.z;
- }
- void loop(void)
- {
- // Refresh output data
- updateOutputs();
- // Check for acceleration changes
- checkAccelerationChange();
- }
- void updateOutputs()
- {
- // Write the state of the buzzer
- digitalWrite(Buzzer_ActiveBuzzer_output_PIN_D2, Buzzer_ActiveBuzzer_output_PIN_D2_rawData);
- }
- void checkAccelerationChange()
- {
- sensors_event_t a, g, temp;
- mpu.getEvent(&a, &g, &temp);
- // Calculate percentage change for each axis
- float changeX = abs(a.acceleration.x - referenceAccel[0]) / referenceAccel[0];
- float changeY = abs(a.acceleration.y - referenceAccel[1]) / referenceAccel[1];
- float changeZ = abs(a.acceleration.z - referenceAccel[2]) / referenceAccel[2];
- // Check if any change exceeds 10%
- if ((changeX > 0.1 || changeY > 0.1 || changeZ > 0.1) && !changeDetected) {
- changeDetected = true; // Set flag for change detected
- changeStartTime = millis(); // Record the time change started
- }
- // If change is detected, check if it has been for 3 seconds
- if (changeDetected) {
- if (millis() - changeStartTime >= 3000) {
- Buzzer_ActiveBuzzer_output_PIN_D2_rawData = true; // Activate buzzer
- }
- }
- // Reset if no change is detected
- if (changeX <= 0.1 && changeY <= 0.1 && changeZ <= 0.1) {
- changeDetected = false; // Reset change detected flag
- Buzzer_ActiveBuzzer_output_PIN_D2_rawData = false; // Deactivate buzzer
- }
- }
- /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement