Advertisement
pleasedontcode

**Intruder Alert** rev_02

Dec 10th, 2024
63
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: **Intruder Alert**
  13.     - Source Code NOT compiled for: Arduino Uno
  14.     - Source Code created on: 2024-12-11 00:33:53
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Im working on a project where i use a PIR HC-SR501 */
  21.     /* a led and a buzzer, the idea is that the PIR */
  22.     /* detects movement and the buzzer with the led turn */
  23.     /* on for 2 seconds , after that I need the PIR to */
  24.     /* identify movement again but instantly */
  25. /****** END SYSTEM REQUIREMENTS *****/
  26.  
  27.  
  28. /********* User code review feedback **********
  29. #### Feedback 1 ####
  30. - The light and buzzer sounds like 60 seconds remember it has to s
  31. topped after 3 seconds, and when the buzzer and led turns off af
  32. ter those 3 seconds it has to start reading movement again after
  33.  it finish
  34.  
  35. ********* User code review feedback **********/
  36.  
  37. /* START CODE */
  38.  
  39. /****** DEFINITION OF LIBRARIES *****/
  40.  
  41. /****** FUNCTION PROTOTYPES *****/
  42. void setup(void);
  43. void loop(void);
  44.  
  45. // USER CODE
  46. int pir_pin = 2;
  47. int led_pin = 3;
  48. int buzzer_pin = 4;
  49. unsigned long start_time = 0; // Variable to store the start time
  50. bool intruder_detected = false; // Intruder detection state
  51.  
  52. void setup(void)
  53. {
  54.   // put your setup code here, to run once:
  55.   pinMode(pir_pin, INPUT);
  56.   pinMode(led_pin, OUTPUT);
  57.   pinMode(buzzer_pin, OUTPUT);
  58.   Serial.begin(9600);
  59. }
  60.  
  61. void loop(void)
  62. {
  63.   // put your main code here, to run repeatedly:
  64.   if (digitalRead(pir_pin) == HIGH)
  65.   {
  66.     if (!intruder_detected) // Only activate if not previously detected
  67.     {
  68.       intruder_detected = true;
  69.       start_time = millis(); // Record the start time
  70.       digitalWrite(led_pin, HIGH);
  71.       digitalWrite(buzzer_pin, HIGH);
  72.       Serial.println("Intruder detected");
  73.     }
  74.  
  75.     // Turn off the buzzer and LED after 3 seconds
  76.     if (millis() - start_time >= 3000)
  77.     {
  78.       digitalWrite(buzzer_pin, LOW);
  79.       digitalWrite(led_pin, LOW);
  80.       intruder_detected = false; // Reset the state to allow for immediate detection again
  81.       Serial.println("Monitoring resumed");
  82.     }
  83.   }
  84.   else
  85.   {
  86.     // If no intruder is detected, keep the LED and buzzer off
  87.     digitalWrite(led_pin, LOW);
  88.     digitalWrite(buzzer_pin, LOW);
  89.     Serial.println("Monitoring");
  90.   }
  91. }
  92.  
  93. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement