Advertisement
pleasedontcode

Acceleration Alert rev_01

Aug 19th, 2024
71
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: Acceleration Alert
  13.     - Source Code NOT compiled for: Arduino Nano ESP32
  14.     - Source Code created on: 2024-08-19 16:59:16
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Read the values from the accelerometer. Hold the */
  21.     /* values as reference values to be compaired with */
  22.     /* the active values. Monitor the active values for a */
  23.     /* change that is greater than 10% in any direction */
  24.     /* for a period of 3 seconds. sound the buzzer when */
  25.     /* true */
  26. /****** END SYSTEM REQUIREMENTS *****/
  27.  
  28. /****** DEFINITION OF LIBRARIES *****/
  29. #include <Wire.h>
  30. #include <Adafruit_Sensor.h>
  31. #include <Adafruit_MPU6050.h>
  32.  
  33. /****** FUNCTION PROTOTYPES *****/
  34. void setup(void);
  35. void loop(void);
  36. void updateOutputs();
  37. void checkAccelerationChange();
  38.  
  39. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  40. const uint8_t Buzzer_ActiveBuzzer_output_PIN_D2 = 2;
  41.  
  42. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  43. /***** used to store raw data *****/
  44. bool Buzzer_ActiveBuzzer_output_PIN_D2_rawData = 0;
  45.  
  46. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  47. /***** used to store data after characteristic curve transformation *****/
  48. float Buzzer_ActiveBuzzer_output_PIN_D2_phyData = 0.0;
  49.  
  50. // MPU6050 object
  51. Adafruit_MPU6050 mpu;
  52.  
  53. // Reference acceleration values
  54. float referenceAccel[3] = {0.0, 0.0, 0.0}; // X, Y, Z
  55. unsigned long changeStartTime = 0; // Time when change started
  56. bool changeDetected = false; // Flag for detecting change
  57.  
  58. void setup(void)
  59. {
  60.     // Initialize serial communication
  61.     Serial.begin(115200);
  62.     pinMode(Buzzer_ActiveBuzzer_output_PIN_D2, OUTPUT);
  63.  
  64.     // Initialize MPU6050
  65.     if (!mpu.begin()) {
  66.         Serial.println("Failed to find MPU6050 chip");
  67.         while (1) {
  68.             delay(10);
  69.         }
  70.     }
  71.  
  72.     // Set accelerometer range
  73.     mpu.setAccelerometerRange(MPU6050_RANGE_2_G);
  74.     delay(100);
  75.    
  76.     // Read initial values for reference
  77.     sensors_event_t a, g, temp;
  78.     mpu.getEvent(&a, &g, &temp);
  79.     referenceAccel[0] = a.acceleration.x;
  80.     referenceAccel[1] = a.acceleration.y;
  81.     referenceAccel[2] = a.acceleration.z;
  82. }
  83.  
  84. void loop(void)
  85. {
  86.     // Refresh output data
  87.     updateOutputs();
  88.     // Check for acceleration changes
  89.     checkAccelerationChange();
  90. }
  91.  
  92. void updateOutputs()
  93. {
  94.     // Write the state of the buzzer
  95.     digitalWrite(Buzzer_ActiveBuzzer_output_PIN_D2, Buzzer_ActiveBuzzer_output_PIN_D2_rawData);
  96. }
  97.  
  98. void checkAccelerationChange()
  99. {
  100.     sensors_event_t a, g, temp;
  101.     mpu.getEvent(&a, &g, &temp);
  102.  
  103.     // Calculate percentage change for each axis
  104.     float changeX = abs(a.acceleration.x - referenceAccel[0]) / referenceAccel[0];
  105.     float changeY = abs(a.acceleration.y - referenceAccel[1]) / referenceAccel[1];
  106.     float changeZ = abs(a.acceleration.z - referenceAccel[2]) / referenceAccel[2];
  107.  
  108.     // Check if any change exceeds 10%
  109.     if ((changeX > 0.1 || changeY > 0.1 || changeZ > 0.1) && !changeDetected) {
  110.         changeDetected = true; // Set flag for change detected
  111.         changeStartTime = millis(); // Record the time change started
  112.     }
  113.  
  114.     // If change is detected, check if it has been for 3 seconds
  115.     if (changeDetected) {
  116.         if (millis() - changeStartTime >= 3000) {
  117.             Buzzer_ActiveBuzzer_output_PIN_D2_rawData = true; // Activate buzzer
  118.         }
  119.     }
  120.  
  121.     // Reset if no change is detected
  122.     if (changeX <= 0.1 && changeY <= 0.1 && changeZ <= 0.1) {
  123.         changeDetected = false; // Reset change detected flag
  124.         Buzzer_ActiveBuzzer_output_PIN_D2_rawData = false; // Deactivate buzzer
  125.     }
  126. }
  127.  
  128. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement