 # execute if/else if a variable is a certain value for a period of time?

``````    if (voltage > 2.00) {
ledState = HIGH;
} else {
ledState = LOW;
}
``````

Right now I'm working on a project that will turn a LED on or off if a value is above or below a set number for more than 10 seconds.

In other words, if the voltage reading is above 2, for 10 seconds. Turn on the LED.

What's the best and most accepted way to determine if "voltage" has been above my threshold for 10 seconds?

Record the value form millis when it first goes above 2. Then, on each pass through loop, if it is still above 2 compare the current millis with the one you took when it first went above 2 to see how long it has been.

Just wondering if the question's also about how to tell if a voltage is actually above 2? Just in case that is the question, well that's just an analogRead and you will be wanting to check for that value to be over 2/5 of 1023 (or is that 1024?) so over 409.

You should have a look at the StateChangeDetection example, which is also in the IDE at File > Examples > 2.Digital

That example includes a whole load of stuff to do with counting presses which is unnecessary in your case, and in another thread recently member groundfungus pruned it back to do only the detection. I include that below; it's for a digital read so you'll need to change it to be for your analog case, but the idea is essentially the same.

``````//StateChangeDetection example pruned of all the counting stuff

// this constant won't change:
const int  inputPin = 9;    // the pin that the input voltage is attached to

// Variables will change:
int inputState = 0;         // current state of the input
int lastInputState = 0;     // previous state of the input

void setup()
{
// initialize the input pin as a input: (not really necessary as the default is INPUT
pinMode(inputPin, INPUT);
Serial.begin(9600);
}

void loop()
{

// compare the inputState to its previous state (lastInputState)
if (inputState != lastInputState)
{
if (inputState == HIGH)
{
// if the current state is HIGH then the input
// went from not high to high:
Serial.println("Went high");

// *****************************************
// do the stuff that a HIGH input triggers
// *****************************************

}
else
{
// if the current state is LOW then the input
// went from high to not high: do nothing except print something if you want.
Serial.println("Went low");
}
}
// save the current state as the last state,
//for next time through the loop
lastInputState = inputState;
}
``````

If the precision is not too important and you don’t have to execute anything else in the mean while, you can try the below:

``````void loop()
{
//read analog value to variable 'volt'
if(volt>409)  //(2/5)*1024
if(vlotChange(volt))
digitalWrite(led,1);
//same way for the less than 2V
}

boolean voltChange(double volt)
{
int i=1;
boolean flag=true;
while(i<=10)  //for 1 sec delay. Change according to the change in delay
{
if(nvolt!=volt)
{
flag=false;
break;
}
delay(1000); //checking the change in every sec. Delay can be changed as required.
}
return flag;
}
``````

If the precision is important working with timer will be better.

logans. That is an infinite while loop.

logans: If ... you don't have to execute anything else in the mean while

I think in spite of your warning, it's not a great idea to do anything like that with a delay(). It's not difficult to implement non-blocking millis()-based timing, and makes the code more adaptable. Oftentimes one starts out with only having to say read one switch or blink one led, but as soon as you want to do two things at once you're screwed.

Much easier to get your mind round millis()-based non-blocking code early on, then use it as second nature.

Delta_G:
logans. That is an infinite while loop.

Sorry, forgot to put i++

``````.
.
.
.

boolean voltChange(double volt)
{
int i=1;
boolean flag=true;
while(i<=10)  //for 1 sec delay. Change according to the change in delay
{