0

I have a program in Arduino that checks an LDR sensor. If it goes over the set values it will trigger an alarm. How do I set it so once triggered it stays on until say a button push is detected to disarm it?

Code:

const int ledPin = 8;
const int buzzerPin = 4;
const int ldrPin = A0;
void setup () {
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);
  pinMode(buzzerPin, OUTPUT);
  pinMode(ldrPin, INPUT);
}

void loop() {
  int ldrStatus = analogRead(ldrPin);
  if (ldrStatus >= 30) {
    noTone(buzzerPin);
    digitalWrite(ledPin, LOW);
  } else {
    tone(buzzerPin, 100);
    digitalWrite(ledPin, HIGH);
    delay(100);
    noTone(buzzerPin);
    digitalWrite(ledPin, LOW);
    delay(100);
    Serial.println("----------- ALARM ACTIVATED -----------");
  }
}
dda
  • 6,030
  • 2
  • 25
  • 34
user443
  • 43
  • 2

1 Answers1

0

You should use a FLAG to fire the alarm instead of using threshold directly.

if (ldrStatus >= 30) {
   AlarmFlag = true; //Set alarm
}
...
if (digitalRead(pushButton) == LOW){
   AlarmFlag = false; //Turn off alarm
}
...
if (AlarmFlag == true){
  Serial.println("ALARM ON");
  ...
}
Ngo Thanh Nhan
  • 503
  • 2
  • 5
  • 17