Advertisement
learnelectronics

Arduino Fire Alarm

Sep 11th, 2023
1,177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 1.52 KB | Software | 0 0
  1. /********************************************
  2.  *            Arduino Fire Alarm            *
  3.  *             learnelectronics             *
  4.  *              Sept 11, 2023               *
  5.  * https://www.youtube.com/learnelectronics *
  6.  *           [email protected]          *
  7.  ********************************************/
  8.  
  9.  
  10.  
  11.  
  12.  
  13.  
  14.  
  15. const int inputPin = 2;
  16. const int outputPin1 = 4;
  17. const int outputPin2 = 3;
  18.  
  19. bool previousState = HIGH;
  20. bool currentState;
  21. bool tonePlaying = false;
  22. bool toneState = LOW;
  23.  
  24. void setup() {
  25.   pinMode(inputPin, INPUT);
  26.   pinMode(outputPin1, OUTPUT);
  27.   pinMode(outputPin2, OUTPUT);
  28.   digitalWrite(outputPin1, LOW);  // Start with pin 4 LOW
  29.   digitalWrite(outputPin2, LOW);  // Start with pin 3 LOW
  30. }
  31.  
  32. void loop() {
  33.   currentState = digitalRead(inputPin);
  34.  
  35.   if (currentState == LOW && previousState == HIGH) {
  36.     digitalWrite(outputPin1, HIGH);  // Set digital pin 4 to HIGH
  37.     if (!tonePlaying) {
  38.       tone(outputPin2, 1000);  // Start generating a 1000 Hz tone on pin 3
  39.       tonePlaying = true;
  40.     }
  41.   } else if (currentState == HIGH && previousState == LOW) {
  42.     digitalWrite(outputPin1, LOW);  // Set digital pin 4 to HIGH
  43.     if (tonePlaying) {
  44.       noTone(outputPin2);  // Stop the tone on pin 3
  45.       tonePlaying = false;
  46.     }
  47.   }
  48.  
  49.   if (tonePlaying) {
  50.     // Alternate the tone state (high/low) every 500 ms
  51.     if (millis() % 1000 < 500) {
  52.       tone(outputPin2, 1000);
  53.     } else {
  54.       noTone(outputPin2);
  55.     }
  56.   }
  57.  
  58.   previousState = currentState;
  59. }
  60.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement