# Verify a condition is met.

I want to implement a logic on my Arduino UNO but I am not sure how to proceed.
The case is the following.

I have a condition: (y>=50) for a variable y that goes from 10 up to 60.
So, if the condition is met a.k.a. if (y>=50), I want to trigger an output (say pin(13, HIGH)).

The thing is I want to make sure the condition is met before triggering pin(13) because it is possible that due to voltage fluctuations y becomes more than 50, but as a mistake.

So, I want to trigger pin(13), 3 seconds after y became >=50 for the first time and after re checking that y is still >=50.

I am thinking of using millis to count time somehow, but becomes rather complicated, because I have to pass the initial millis to a SUB that will count 3 sec and would then check if y>=50 in order to issue the command pin(13,HIGH).
If I do something like that, I loose control over the rest of the program as nothing will going to be executed within those 3 seconds and we don't want that.

Any help appreciated.

When you first detect exceeding the threshold, record the time, and set a flag "armed".
If "armed" AND the current time minus the start time is greater than three seconds, do what you need.
If "armed" AND current reading is less than threshold, reset "armed"

Check out the blinkWithoutDelay example to get your 3 second window or whatever it is. Then use an

if (y>=limit) {//turn on pin 13}

You could even set limit above any value of y you might get in order to turn off the if conditional.

AWOL, thank you very much on your approach.
I think that if I follow your way, I have to build a loop checking if current time is 3secs greater than start time, which brings us to the issue that we wouldn't have control over the 3 secs period.
Correct me if I am wrong.
...

You're misunderstanding AWOL - you put those tests into the loop() function - this is the special Arduino function where you put the code that runs continually. Behind the scenes, your sketch will repeatedly run whatever is in loop(). So just put those checks into loop() and make sure loop runs reasonably fast (ie, that there aren't any delay()'s and such in it). This is very common practice on Arduino - your loop() function calls a bunch of functions that check a set of conditions, and do something if they're met (and millis times that things happened at are stored in global variables)

ok, thank you both for your replies.

So, I came up with a fast & dirty code:

``````byte val = false;
int current_millis=0;
byte c=0;
bool flag = false;
bool one_time = false;
int c_millis_dt = 0;
void setup()
{

pinMode(13, OUTPUT);
pinMode(4, INPUT);
pinMode(11, OUTPUT);
digitalWrite(11, LOW);
digitalWrite(4, LOW);
Serial.begin(9600);
}

void loop()
{

if (val == HIGH && one_time == false) {
current_millis = millis();
}
if (val == HIGH) {
one_time = true;
}
if (val == HIGH && one_time == true) {
yStart(current_millis);

}
else {
c = 0;
current_millis = 0;
one_time = false;
}
delay(1000);

}

void yStart(int current_millis) {
c = c + 1;
c_millis_dt = (int(millis() / 1000)) - (int(current_millis/1000));
Serial.println(c_millis_dt);
if (c_millis_dt>3) {
digitalWrite(11, HIGH);
c_millis_dt = 0;
one_time = false;

}
}
``````

but the thing is that the timer is not operating correctly.

Look at the Serial output:

Port closed
Opening port
Port open
<Button is pressed aka condition is met. Bellow is the timer output ’ Serial.println(c_millis_dt); ’ from above>

0
2
2
0
0
2
0
1

the timer seems to jump from 0 to 2 or 3 without reason. It may output something like:
0
1
1
2

…or

0
3
4

…or correctly:

0
1
2
3

Any ideas why this is happening?

If you need to check that something exceeds a threshold and stays above that threshold for a period of time you need code something like this

``````myVal = analogRead(analogPin);
if (myVal < threshold) {
lastReadMillis = millis();   // value below threshold so reset clock
}
if (millis() - lastReadMillis > = interval) {
// value has been above threshold for longer than interval
}
``````

…R