Advertisement
learnelectronics

Arduino Laser Tripwire

Sep 22nd, 2023
891
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 1.34 KB | Software | 0 0
  1. /********************************************************
  2.  *                 Arduino For Kids: Tripwire Alarm     *
  3.  *                 Learnelectronics Sept. 2023 (C)      *
  4.  *          https://www.youtube.com/learnelectronics    *
  5.  ********************************************************/
  6.  
  7. const int buttonPin = 2;  // Input pin for the button
  8. const int ledPin = 6;     // Output pin for the LED
  9. const int tonePin = 3;    // Output pin for the tone
  10.  
  11. bool alarmActive = false;  // Flag to track the alarm state
  12.  
  13. void setup() {
  14.   pinMode(buttonPin, INPUT_PULLUP);  // Set button pin as input with internal pull-up resistor
  15.   pinMode(ledPin, OUTPUT);
  16.   pinMode(tonePin, OUTPUT);
  17. }
  18.  
  19. void loop() {
  20.   int buttonState = digitalRead(buttonPin);  // Read the state of the button
  21.  
  22.   if (buttonState == HIGH) {
  23.     // Button is pressed, activate the alarm
  24.     if (!alarmActive) {
  25.       alarmActive = true;
  26.       digitalWrite(ledPin, HIGH);  // Turn on the LED
  27.       //tone(tonePin, 1000);       // Play a 1 kHz tone
  28.       for (int i = 1000; i <= 1500; i++) {
  29.         tone(tonePin, i);
  30.         delay(5);
  31.   }
  32. }
  33.     }
  34.    else {
  35.     // Button is not pressed, deactivate the alarm
  36.     if (alarmActive) {
  37.       alarmActive = false;
  38.       digitalWrite(ledPin, LOW);  // Turn off the LED
  39.       noTone(tonePin);          // Stop the tone
  40.     }
  41.   }
  42. }
  43.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement