Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /********************************************
- * Arduino Fire Alarm *
- * learnelectronics *
- * Sept 11, 2023 *
- * https://www.youtube.com/learnelectronics *
- ********************************************/
- const int inputPin = 2;
- const int outputPin1 = 4;
- const int outputPin2 = 3;
- bool previousState = HIGH;
- bool currentState;
- bool tonePlaying = false;
- bool toneState = LOW;
- void setup() {
- pinMode(inputPin, INPUT);
- pinMode(outputPin1, OUTPUT);
- pinMode(outputPin2, OUTPUT);
- digitalWrite(outputPin1, LOW); // Start with pin 4 LOW
- digitalWrite(outputPin2, LOW); // Start with pin 3 LOW
- }
- void loop() {
- currentState = digitalRead(inputPin);
- if (currentState == LOW && previousState == HIGH) {
- digitalWrite(outputPin1, HIGH); // Set digital pin 4 to HIGH
- if (!tonePlaying) {
- tone(outputPin2, 1000); // Start generating a 1000 Hz tone on pin 3
- tonePlaying = true;
- }
- } else if (currentState == HIGH && previousState == LOW) {
- digitalWrite(outputPin1, LOW); // Set digital pin 4 to HIGH
- if (tonePlaying) {
- noTone(outputPin2); // Stop the tone on pin 3
- tonePlaying = false;
- }
- }
- if (tonePlaying) {
- // Alternate the tone state (high/low) every 500 ms
- if (millis() % 1000 < 500) {
- tone(outputPin2, 1000);
- } else {
- noTone(outputPin2);
- }
- }
- previousState = currentState;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement