Is there a way that I can print the longest time that the alarm sounded for during the monitoring period? I would like for the longest event to print every second like the other data. While a newbie and with the help of others I was able to cobble together how to report the # of times the alarm sounded for during the monitoring period, the total time the alarm sounded for and the average amount of time the alarm sounded for each time it was triggered. I am stumped on how to print how long the longest alarm sounded for during the monitoring period. Any help would be appreciated.
int sensorPin = A0; // input pin for the analog sensor
int alarmPin = 13; // pin for the piezo alarm
unsigned int sensorValue = 0; // stores value coming from the sensor
unsigned int alarmCount = 0;
unsigned long duration = 0;
unsigned int average = 0;
int alarmState=LOW;
int lastAlarmState=LOW;
unsigned long time;
unsigned long switchoff = 0;
unsigned int threshold = 512;
long startTime;
long alarmDuration;
void setup(){
pinMode(alarmPin, OUTPUT);
Serial.begin(9600);
alarmState=digitalRead(13);
}
void loop() {
if(alarmState==HIGH && lastAlarmState==LOW) {
alarmCount++;
}
lastAlarmState=alarmState;
alarmState=digitalRead(13);
sensorValue = analogRead(A0);
Serial.println(sensorValue);
Serial.println(alarmCount);
Serial.println(duration/1000);
Serial.println(average = (duration/alarmCount)/1000);
delay(250);
if(sensorValue > threshold) {
digitalWrite(alarmPin, HIGH); // Set off alarm
switchoff = millis()+4000; // keep alarmPin HIGH for a minimum of millis
}
if (millis() > switchoff && switchoff > 0) { // turn alarmPin off after minimum of millis and if threshold is no longer exceeded
digitalWrite(alarmPin, LOW);
switchoff = 0;
}
if(alarmState == HIGH) {
duration = millis(); // total time alarm was on
}
}