Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /********************************************************
- * Arduino For Kids: Tripwire Alarm *
- * Learnelectronics Sept. 2023 (C) *
- * https://www.youtube.com/learnelectronics *
- ********************************************************/
- const int buttonPin = 2; // Input pin for the button
- const int ledPin = 6; // Output pin for the LED
- const int tonePin = 3; // Output pin for the tone
- bool alarmActive = false; // Flag to track the alarm state
- void setup() {
- pinMode(buttonPin, INPUT_PULLUP); // Set button pin as input with internal pull-up resistor
- pinMode(ledPin, OUTPUT);
- pinMode(tonePin, OUTPUT);
- }
- void loop() {
- int buttonState = digitalRead(buttonPin); // Read the state of the button
- if (buttonState == HIGH) {
- // Button is pressed, activate the alarm
- if (!alarmActive) {
- alarmActive = true;
- digitalWrite(ledPin, HIGH); // Turn on the LED
- //tone(tonePin, 1000); // Play a 1 kHz tone
- for (int i = 1000; i <= 1500; i++) {
- tone(tonePin, i);
- delay(5);
- }
- }
- }
- else {
- // Button is not pressed, deactivate the alarm
- if (alarmActive) {
- alarmActive = false;
- digitalWrite(ledPin, LOW); // Turn off the LED
- noTone(tonePin); // Stop the tone
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement