Advertisement
pleasedontcode

"Alarm Activation" rev_01

Jul 20th, 2024
177
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: "Alarm Activation"
  13.     - Source Code NOT compiled for: Arduino Uno
  14.     - Source Code created on: 2024-07-21 02:26:35
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Create a security system utilizing EasyButton and */
  21.     /* Ultrasonic libraries. The system should monitor */
  22.     /* distance with an HC-SR04 sensor and activate an */
  23.     /* alarm when either button not  connected to pin D2 */
  24.     /* or or HC-SR04 sensor sense human pressence. */
  25. /****** END SYSTEM REQUIREMENTS *****/
  26.  
  27. /****** DEFINITION OF LIBRARIES *****/
  28. #include <EasyButton.h>  //https://github.com/evert-arias/EasyButton
  29. #include <Ultrasonic.h>  //https://github.com/ErickSimoes/Ultrasonic
  30.  
  31. /****** FUNCTION PROTOTYPES *****/
  32. void setup(void);
  33. void loop(void);
  34. void updateOutputs(void);
  35. void activateAlarm(void);
  36.  
  37. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  38. const uint8_t PushButton1_PIN_D2 = 2;
  39. const uint8_t HCSR04_Echo_PIN_D4 = 4;
  40. const uint8_t PushButton2_PIN_D5 = 5;
  41.  
  42. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  43. const uint8_t HCSR04_Trigger_PIN_D3 = 3;
  44. const uint8_t Alarm_PIN_D6 = 6;  // Pin for the alarm
  45.  
  46. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  47. /***** used to store raw data *****/
  48. bool HCSR04_Trigger_PIN_D3_rawData = 0;
  49.  
  50. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  51. /***** used to store data after characteristic curve transformation *****/
  52. float HCSR04_Trigger_PIN_D3_phyData = 0.0;
  53.  
  54. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  55. EasyButton button1(PushButton1_PIN_D2);  // Initialize EasyButton for button 1
  56. EasyButton button2(PushButton2_PIN_D5);  // Initialize EasyButton for button 2
  57.  
  58. Ultrasonic ultrasonic(HCSR04_Trigger_PIN_D3, HCSR04_Echo_PIN_D4); // Initialize Ultrasonic sensor
  59.  
  60. void onButton1Pressed() {
  61.   Serial.println("Button1 pressed");
  62.   activateAlarm();
  63. }
  64.  
  65. void onButton2Pressed() {
  66.   Serial.println("Button2 pressed");
  67. }
  68.  
  69. void setup(void) {
  70.   // put your setup code here, to run once:
  71.   Serial.begin(115200);
  72.  
  73.   Serial.println();
  74.   Serial.println(">>> Security System Example <<<");
  75.  
  76.   pinMode(PushButton1_PIN_D2, INPUT_PULLUP);
  77.   pinMode(HCSR04_Echo_PIN_D4, INPUT);
  78.   pinMode(PushButton2_PIN_D5, INPUT_PULLUP);
  79.   pinMode(HCSR04_Trigger_PIN_D3, OUTPUT);
  80.   pinMode(Alarm_PIN_D6, OUTPUT);  // Set alarm pin as output
  81.  
  82.   button1.begin();
  83.   button2.begin();
  84.   button1.onPressed(onButton1Pressed);
  85.   button2.onPressed(onButton2Pressed);
  86. }
  87.  
  88. void loop(void) {
  89.   // put your main code here, to run repeatedly:
  90.   button1.read();  // Read the state of button 1
  91.   button2.read();  // Read the state of button 2
  92.  
  93.   updateOutputs();  // Refresh output data
  94.  
  95.   int distance = ultrasonic.read();  // Read distance from ultrasonic sensor
  96.   Serial.print("Distance in CM: ");
  97.   Serial.println(distance);
  98.  
  99.   if (distance < 50) {  // If distance is less than 50 cm, activate alarm
  100.     activateAlarm();
  101.   }
  102.  
  103.   delay(1000);  // Delay for 1 second
  104. }
  105.  
  106. void updateOutputs() {
  107.   digitalWrite(HCSR04_Trigger_PIN_D3, HCSR04_Trigger_PIN_D3_rawData);
  108. }
  109.  
  110. void activateAlarm() {
  111.   digitalWrite(Alarm_PIN_D6, HIGH);  // Activate alarm
  112.   delay(5000);  // Keep alarm on for 5 seconds
  113.   digitalWrite(Alarm_PIN_D6, LOW);  // Deactivate alarm
  114. }
  115.  
  116. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement