# how to use time in if condition?

guys i simply want to know, can i use time to execute a command in if condition? I means to say that

if ( time==10second && sensor1 works ) { execute this statement }

tell me how would i use time==10 second here in if statement

How are you determining the time?

The usual method is using millis().

Something starts Starttime = millis()

Other things happen

If ((millis() - Starttime) == 10000) Do whatever

However, you may not be at this point at exactly the time 10000. It is better to use > rather than ==.

Weedpharma

weedpharma: If ((millis() - Starttime) == 10000) Do whatever

However, you may not be at this point at exactly the time 10000. It is better to use > rather than ==.

Actually, you need to use >= for accuracy.

that start thing...

Something starts if (something != oldSomeThing) // only happens once and that way starttime is not reset every scan starttime = millis()

oldSomeThing = something // next loop they will be equal

Other things happen

If ((millis() - Starttime) >= 10000) // shown with preferred use of >= Do whatever

bgautam1991:
guys i simply want to know, can i use time to execute a command in if condition?
I means to say that tell me how would i use time==10 second here in if statement

Your question isn’t clear. 10 seconds from when?

weedpharma:
How are you determining the time?

The usual method is using millis().

Something starts
Starttime = millis()

Other things happen

If ((millis() - Starttime) == 10000)
Do whatever

However, you may not be at this point at exactly the time 10000. It is better to use > rather than ==.

Weedpharma

thanks i got what you said

{
If ((millis() - Starttime) == 10000)
Do whatever
}

thanks

Note the later reference to >=. This is important.

What happens if you program is doing something else at 10000? The next time it checks it could be 10001 so will not happen.

Weedpharma

weedpharma: Note the later reference to >=. This is important.

What happens if you program is doing something else at 10000? The next time it checks it could be 10001 so will not happen.

Weedpharma

If he did not understand the need to load millis(), once and just once, he will never get that far.

bgautam1991: thanks i got what you said

{ If ((millis() - Starttime) == 10000) Do whatever }

thanks

You almost "got what they said" ...

`````` If ( millis() - StartTime >= 10000 )
{
Do whatever
}
``````

There are two very good reasons why you should almost always use ">=" and rarely, if ever, use "==" when checking for elapsed time.