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: Car_Alarm
- - Source Code compiled for: Arduino Uno
- - Source Code created on: 2024-11-19 22:09:59
- - Source Code generated by: Alessandro
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* Upon powering on, the system must take 10 readings */
- /* from the accelerometer across all three axes (X, */
- /* Y, Z). Compute the average of these readings and */
- /* use it to offset gravity from subsequent periodic */
- /* accelerometer measurements. */
- /****** SYSTEM REQUIREMENT 2 *****/
- /* On startup, the car alarm must be disengaged by */
- /* default. If an authorized RFID car is scanned */
- /* using the RFID card reader: 1) enable the car */
- /* alarm it it is currently disabled. 2) Disable the */
- /* car alarm if it is currently enabled. */
- /****** SYSTEM REQUIREMENT 3 *****/
- /* When the car alarm is enabled: 1) Periodically */
- /* read the accelerometer measurements. 2) If the */
- /* absolute value of any axis (X, Y, or Z) exceeds a */
- /* predefined threshold (set by calibration), trigger */
- /* the alarm. */
- /****** SYSTEM REQUIREMENT 4 *****/
- /* When the alarm is triggered, the relay must */
- /* control the car horn. The relay will toggle the */
- /* car horn ON and OFF for 10 cycles. Each ON/OFF */
- /* cycle must have a delay of 500 milliseconds. */
- /****** END SYSTEM REQUIREMENTS *****/
- /********* User code review feedback **********
- #### Feedback 1 ####
- - complete checkRFID() code
- ********* User code review feedback **********/
- /* START CODE */
- /****** DEFINITION OF LIBRARIES *****/
- #include <Wire.h>
- #include <SPI.h>
- #include <MPU6050.h> //https://github.com/electroniccats/mpu6050
- #include <Relay.h> //https://github.com/rafaelnsantos/Relay
- #include <MFRC522.h> //https://github.com/miguelbalboa/rfid
- /****** FUNCTION PROTOTYPES *****/
- void setup(void);
- void loop(void);
- void calibrateAccelerometer();
- void readAccelerometer();
- void checkRFID();
- void triggerAlarm();
- void controlHorn();
- /***** DEFINITION OF DIGITAL INPUT PINS *****/
- const uint8_t accelerometer_MPU6050_Interrupt_PIN_D2 = 2;
- const uint8_t card_reader_MFRC522_IRQ_PIN_D5 = 5;
- /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
- const uint8_t car_horn_RelayModule_Signal_PIN_D3 = 3;
- const uint8_t card_reader_MFRC522_RST_PIN_D4 = 4;
- /***** DEFINITION OF I2C PINS *****/
- const uint8_t accelerometer_MPU6050_I2C_PIN_SDA_A4 = A4;
- const uint8_t accelerometer_MPU6050_I2C_PIN_SCL_A5 = A5;
- /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
- bool car_horn_RelayModule_Signal_PIN_D3_rawData = 0;
- bool card_reader_MFRC522_RST_PIN_D4_rawData = 0;
- /***** DEFINITION OF ACCELEROMETER VARIABLES *****/
- MPU6050 accelerometer;
- float accelOffset[3] = {0.0, 0.0, 0.0}; // Offset for X, Y, Z axes
- bool alarmEnabled = false; // Alarm state
- const float threshold = 1.5; // Threshold for triggering alarm
- // Create an instance of the MFRC522 class
- MFRC522 rfid(card_reader_MFRC522_IRQ_PIN_D5, card_reader_MFRC522_RST_PIN_D4);
- /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
- void setup(void)
- {
- // Initialize pins
- pinMode(accelerometer_MPU6050_Interrupt_PIN_D2, INPUT);
- pinMode(card_reader_MFRC522_IRQ_PIN_D5, INPUT_PULLUP);
- pinMode(car_horn_RelayModule_Signal_PIN_D3, OUTPUT);
- pinMode(card_reader_MFRC522_RST_PIN_D4, OUTPUT);
- // Start the SPI library:
- SPI.begin();
- // Initialize the RFID reader
- rfid.PCD_Init();
- // Initialize the accelerometer
- accelerometer.initialize();
- calibrateAccelerometer();
- }
- void loop(void)
- {
- // Check for RFID
- checkRFID();
- // If alarm is enabled, read accelerometer data
- if (alarmEnabled) {
- readAccelerometer();
- }
- }
- void calibrateAccelerometer() {
- // Take 10 readings from the accelerometer
- int16_t ax, ay, az;
- for (int i = 0; i < 10; i++) {
- accelerometer.getAcceleration(&ax, &ay, &az);
- accelOffset[0] += ax;
- accelOffset[1] += ay;
- accelOffset[2] += az;
- delay(100);
- }
- // Compute average
- accelOffset[0] /= 10.0;
- accelOffset[1] /= 10.0;
- accelOffset[2] /= 10.0;
- }
- void readAccelerometer() {
- int16_t ax, ay, az;
- accelerometer.getAcceleration(&ax, &ay, &az);
- // Offset gravity
- ax -= accelOffset[0];
- ay -= accelOffset[1];
- az -= accelOffset[2];
- // Check if any axis exceeds the threshold
- if (abs(ax) > threshold || abs(ay) > threshold || abs(az) > threshold) {
- triggerAlarm();
- }
- }
- void checkRFID() {
- // Look for a new card
- if (!rfid.PICC_IsNewCardPresent()) {
- return;
- }
- // Select one of the cards
- if (!rfid.PICC_ReadCardSerial()) {
- return;
- }
- // Check if the scanned RFID is authorized (for simplicity, using a specific UID)
- // Replace with actual authorization logic
- byte authorizedUID[] = {0xDE, 0xAD, 0xBE, 0xEF}; // Example UID
- if (memcmp(rfid.uid.uidByte, authorizedUID, sizeof(authorizedUID)) == 0) {
- alarmEnabled = !alarmEnabled; // Toggle alarm state
- }
- // Stop reading the card
- rfid.PICC_HaltA();
- }
- void triggerAlarm() {
- controlHorn();
- }
- void controlHorn() {
- for (int i = 0; i < 10; i++) {
- digitalWrite(car_horn_RelayModule_Signal_PIN_D3, HIGH);
- delay(500);
- digitalWrite(car_horn_RelayModule_Signal_PIN_D3, LOW);
- delay(500);
- }
- }
- /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement