# Detect if analog voltage input drops for duration of time

Hi all
My script currently outputs HIGH/LOW to pin 13 when analog voltage input is above or below 5v

I’m trying to figure out how to only output low if voltage input drops below 5v for 5 seconds

Here’s what I currently have

``````/*

float vout = 0.0;
float vin = 0.0;
float R1 = 10000.0; // resistance of R1 (100K)
float R2 = 1000.0; // resistance of R2 (10K)
int value = 0;
unsigned long time;

void setup() {
//start serial connection
Serial.begin(9600);
analogReference(INTERNAL);
pinMode(13, OUTPUT);
delay(500);

}
void loop(){

vout = (value * 1.1) / 1024.0;
vin = vout / (R2/(R1+R2));
if (vin<0.09) {
}

if (vin>5) {
digitalWrite(13, HIGH);
} else {

digitalWrite(13, LOW);
}

Serial.println(vin);

}
``````

The simplest way is to think of the problem upside down. Record the time whenever the value is HIGH and then check how long since it was last HIGH - like this

``````if (value == HIGH) {
timeValueLastHigh = millis();
}

if (millis() - timeValueLastHigh >= 5000) {
// value has been low for 5 secs
// do stuff
}
``````

...R

Robin2:
The simplest way is to think of the problem upside down. Record the time whenever the value is HIGH and then check how long since it was last HIGH - like this

``````if (value == HIGH) {
``````

timeValueLastHigh = millis();
}

if (millis() - timeValueLastHigh >= 5000) {
// value has been low for 5 secs
// do stuff
}

``````

...R
``````

Brilliant! I knew it would be simple. Works perfectly, thanks