Advertisement
pleasedontcode

**Intruder Alert** rev_01

Dec 10th, 2024
51
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:23:07
  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. /* START CODE */
  28.  
  29. /****** DEFINITION OF LIBRARIES *****/
  30.  
  31. /****** FUNCTION PROTOTYPES *****/
  32. void setup(void);
  33. void loop(void);
  34.  
  35. // USER CODE
  36. int pir_pin = 2;
  37. int led_pin = 3;
  38. int buzzer_pin = 4;
  39. unsigned long start_time = 0; // Variable to store the start time
  40. bool intruder_detected = false; // Intruder detection state
  41.  
  42. void setup(void)
  43. {
  44.   // put your setup code here, to run once:
  45.   pinMode(pir_pin, INPUT);
  46.   pinMode(led_pin, OUTPUT);
  47.   pinMode(buzzer_pin, OUTPUT);
  48.   Serial.begin(9600);
  49. }
  50.  
  51. void loop(void)
  52. {
  53.   // put your main code here, to run repeatedly:
  54.   if (digitalRead(pir_pin) == HIGH)
  55.   {
  56.     if (!intruder_detected) // Only activate if not previously detected
  57.     {
  58.       intruder_detected = true;
  59.       start_time = millis(); // Record the start time
  60.       digitalWrite(led_pin, HIGH);
  61.       digitalWrite(buzzer_pin, HIGH);
  62.       Serial.println("Intruder detected");
  63.     }
  64.  
  65.     // Turn off the buzzer and LED after 2 seconds
  66.     if (millis() - start_time >= 2000)
  67.     {
  68.       digitalWrite(buzzer_pin, LOW);
  69.       digitalWrite(led_pin, LOW);
  70.       intruder_detected = false; // Reset the state to allow for immediate detection again
  71.     }
  72.   }
  73.   else
  74.   {
  75.     // If no intruder is detected, keep the LED and buzzer off
  76.     digitalWrite(led_pin, LOW);
  77.     digitalWrite(buzzer_pin, LOW);
  78.     Serial.println("Monitoring");
  79.   }
  80. }
  81.  
  82. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement