Advertisement
pleasedontcode

Car Alarm

Nov 19th, 2024
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 6.01 KB | Source Code | 0 0
  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: Car_Alarm
  13.     - Source Code compiled for: Arduino Uno
  14.     - Source Code created on: 2024-11-19 22:09:59
  15.     - Source Code generated by: Alessandro
  16.  
  17. ********* Pleasedontcode.com **********/
  18.  
  19. /****** SYSTEM REQUIREMENTS *****/
  20. /****** SYSTEM REQUIREMENT 1 *****/
  21.     /* Upon powering on, the system must take 10 readings */
  22.     /* from the accelerometer across all three axes (X, */
  23.     /* Y, Z). Compute the average of these readings and */
  24.     /* use it to offset gravity from subsequent periodic */
  25.     /* accelerometer measurements. */
  26. /****** SYSTEM REQUIREMENT 2 *****/
  27.     /* On startup, the car alarm must be disengaged by */
  28.     /* default. If an authorized RFID car is scanned */
  29.     /* using the RFID card reader: 1) enable the car */
  30.     /* alarm it it is currently disabled. 2) Disable the */
  31.     /* car alarm if it is currently enabled. */
  32. /****** SYSTEM REQUIREMENT 3 *****/
  33.     /* When the car alarm is enabled: 1) Periodically */
  34.     /* read the accelerometer measurements. 2) If the */
  35.     /* absolute value of any axis (X, Y, or Z) exceeds a */
  36.     /* predefined threshold (set by calibration), trigger */
  37.     /* the alarm. */
  38. /****** SYSTEM REQUIREMENT 4 *****/
  39.     /* When the alarm is triggered, the relay must */
  40.     /* control the car horn. The relay will toggle the */
  41.     /* car horn ON and OFF for 10 cycles. Each ON/OFF */
  42.     /* cycle must have a delay of 500 milliseconds. */
  43. /****** END SYSTEM REQUIREMENTS *****/
  44.  
  45.  
  46. /********* User code review feedback **********
  47. #### Feedback 1 ####
  48. - complete checkRFID() code
  49. ********* User code review feedback **********/
  50.  
  51. /* START CODE */
  52.  
  53. /****** DEFINITION OF LIBRARIES *****/
  54. #include <Wire.h>
  55. #include <SPI.h>
  56. #include <MPU6050.h>    //https://github.com/electroniccats/mpu6050
  57. #include <Relay.h>      //https://github.com/rafaelnsantos/Relay
  58. #include <MFRC522.h>    //https://github.com/miguelbalboa/rfid
  59.  
  60. /****** FUNCTION PROTOTYPES *****/
  61. void setup(void);
  62. void loop(void);
  63. void calibrateAccelerometer();
  64. void readAccelerometer();
  65. void checkRFID();
  66. void triggerAlarm();
  67. void controlHorn();
  68.  
  69. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  70. const uint8_t accelerometer_MPU6050_Interrupt_PIN_D2        = 2;
  71. const uint8_t card_reader_MFRC522_IRQ_PIN_D5                  = 5;
  72.  
  73. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  74. const uint8_t car_horn_RelayModule_Signal_PIN_D3              = 3;
  75. const uint8_t card_reader_MFRC522_RST_PIN_D4                   = 4;
  76.  
  77. /***** DEFINITION OF I2C PINS *****/
  78. const uint8_t accelerometer_MPU6050_I2C_PIN_SDA_A4           = A4;
  79. const uint8_t accelerometer_MPU6050_I2C_PIN_SCL_A5           = A5;
  80.  
  81. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  82. bool    car_horn_RelayModule_Signal_PIN_D3_rawData           = 0;
  83. bool    card_reader_MFRC522_RST_PIN_D4_rawData               = 0;
  84.  
  85. /***** DEFINITION OF ACCELEROMETER VARIABLES *****/
  86. MPU6050 accelerometer;
  87. float accelOffset[3] = {0.0, 0.0, 0.0}; // Offset for X, Y, Z axes
  88. bool alarmEnabled = false; // Alarm state
  89. const float threshold = 1.5; // Threshold for triggering alarm
  90.  
  91. // Create an instance of the MFRC522 class
  92. MFRC522 rfid(card_reader_MFRC522_IRQ_PIN_D5, card_reader_MFRC522_RST_PIN_D4);
  93.  
  94. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  95.  
  96. void setup(void)
  97. {
  98.     // Initialize pins
  99.     pinMode(accelerometer_MPU6050_Interrupt_PIN_D2, INPUT);
  100.     pinMode(card_reader_MFRC522_IRQ_PIN_D5, INPUT_PULLUP);
  101.     pinMode(car_horn_RelayModule_Signal_PIN_D3, OUTPUT);
  102.     pinMode(card_reader_MFRC522_RST_PIN_D4, OUTPUT);
  103.    
  104.     // Start the SPI library:
  105.     SPI.begin();
  106.    
  107.     // Initialize the RFID reader
  108.     rfid.PCD_Init();
  109.  
  110.     // Initialize the accelerometer
  111.     accelerometer.initialize();
  112.     calibrateAccelerometer();
  113. }
  114.  
  115. void loop(void)
  116. {
  117.     // Check for RFID
  118.     checkRFID();
  119.    
  120.     // If alarm is enabled, read accelerometer data
  121.     if (alarmEnabled) {
  122.         readAccelerometer();
  123.     }
  124. }
  125.  
  126. void calibrateAccelerometer() {
  127.     // Take 10 readings from the accelerometer
  128.     int16_t ax, ay, az;
  129.     for (int i = 0; i < 10; i++) {
  130.         accelerometer.getAcceleration(&ax, &ay, &az);
  131.         accelOffset[0] += ax;
  132.         accelOffset[1] += ay;
  133.         accelOffset[2] += az;
  134.         delay(100);
  135.     }
  136.     // Compute average
  137.     accelOffset[0] /= 10.0;
  138.     accelOffset[1] /= 10.0;
  139.     accelOffset[2] /= 10.0;
  140. }
  141.  
  142. void readAccelerometer() {
  143.     int16_t ax, ay, az;
  144.     accelerometer.getAcceleration(&ax, &ay, &az);
  145.    
  146.     // Offset gravity
  147.     ax -= accelOffset[0];
  148.     ay -= accelOffset[1];
  149.     az -= accelOffset[2];
  150.  
  151.     // Check if any axis exceeds the threshold
  152.     if (abs(ax) > threshold || abs(ay) > threshold || abs(az) > threshold) {
  153.         triggerAlarm();
  154.     }
  155. }
  156.  
  157. void checkRFID() {
  158.     // Look for a new card
  159.     if (!rfid.PICC_IsNewCardPresent()) {
  160.         return;
  161.     }
  162.    
  163.     // Select one of the cards
  164.     if (!rfid.PICC_ReadCardSerial()) {
  165.         return;
  166.     }
  167.  
  168.     // Check if the scanned RFID is authorized (for simplicity, using a specific UID)
  169.     // Replace with actual authorization logic
  170.     byte authorizedUID[] = {0xDE, 0xAD, 0xBE, 0xEF}; // Example UID
  171.     if (memcmp(rfid.uid.uidByte, authorizedUID, sizeof(authorizedUID)) == 0) {
  172.         alarmEnabled = !alarmEnabled; // Toggle alarm state
  173.     }
  174.    
  175.     // Stop reading the card
  176.     rfid.PICC_HaltA();
  177. }
  178.  
  179. void triggerAlarm() {
  180.     controlHorn();
  181. }
  182.  
  183. void controlHorn() {
  184.     for (int i = 0; i < 10; i++) {
  185.         digitalWrite(car_horn_RelayModule_Signal_PIN_D3, HIGH);
  186.         delay(500);
  187.         digitalWrite(car_horn_RelayModule_Signal_PIN_D3, LOW);
  188.         delay(500);
  189.     }
  190. }
  191.  
  192. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement