New to all of this and would appreciate some help. How can I keep an alarm on for a period of time (4 seconds) after it had been triggered and the threshold is no longer exceeded? If the threshold continues to be exceeded I would like the alarm to continue sounding but if the threshold was triggered for an instant I would like the alarm to sound for a minimum of 4 seconds.
I found an example online and changed it up a bit but cannot figure out how to keep the alarm on for a certain period of time. Also, it is important that the serial printer not be impacted when a delay is set as I would like to continue to take readings at a different pace (other than every 4 seconds). Thanking you in advance for your help.
/*
Analog Input
Demonstrates analog input by reading an analog sensor on analog pin 0 and
turning on alarm connected to digital pin 13 for at least 4 seconds when
threshold is exceeded. Alarm should stop sounding once sensorValue is below threshold
but alarm should always sound for at least 4 seconds.
*/
int sensorPin = A0; // select the input pin for the analog sensor
int alarmPin = 13; // select the pin for the piezo alarm
int sensorValue = 0; // variable to store the value coming from the sensor
void setup() {
// declare the alarmPin as an OUTPUT:
pinMode(alarmPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
int threshold = 512;
if(analogRead(sensorPin) > threshold)
{
digitalWrite(alarmPin, HIGH);
}
else{ digitalWrite(alarmPin, LOW);
}
int sensorValue = analogRead(A0);
Serial.println(sensorValue);
}