Is it possible to use Milli () function to calculate total time taken?

I have two conditional statement ,
for example ,

if(x<1&&y>6)
{
//get the total time(second) inside this conditional statement
}
if(x<20&&y>30)
{

//get the total time(second) inside this conditional statement
}
My question is,

is it possible use Millis () function to calculate the total time in each "if condition"??

or any other method to calculate time?

please explain.

Thanks

(deleted)

Pay attention, specially if use micros() that also the assignment and the text need some time, so the time displayed is a bit more than the time needed

spycatcher2k:
then do maths.

It is so nice to see someone else who knows that maths, being short for mathematics, is plural :wink:

please give me some code examples.

Something like this will do it.

unsigned long startTime, endTime;

if(x<1&&y>6)
{
   startTime = millis();
   //get the total time(second) inside this conditional statement  
   endTime = millis();
}
if(x<20&&y>30)
{
 //get the total time(second) inside this conditional statement  
}
Serial.print("Time in conditional statement: ");
Serial.print(endTime - startTime);
Serial.println(" milliseconds.");

FEBaily:
It is so nice to see someone else who knows that maths, being short for mathematics, is plural :wink:

I'm not convinced that mathematics is a plural. But out of interest what would you say a singular mathematic or math is? Also a physic perhaps.

But then speakers of British English (or proper English as it usually called, at least round here) are in a tiny minority in the English-speaking world so perhaps we should stick to our charming eccentricities as long as we can.

Now back to Arduinos?

Steve

slipstick:
Now back to Arduinos *?

K....

wounder1:
please give me some code examples.

Was about to code something along the lines of (identical to, in fact) what GypsumFantastic provided , then I thought to myself, Self, I'm sensing a bigger question here ....

Do you actually want to know how long it takes to run the code that's inside that if(), or do you want to know how long the condition is true?

  • or is that Arduinoes, like potatoes?
unsigned long startTime, endTime;

if(x<1&&y>6)
{
   startTime = millis();
   //get the total time(second) inside this conditional statement  
   endTime = millis();
}
if(x<20&&y>30)
{
 //get the total time(second) inside this conditional statement  
}
Serial.print("Time in conditional statement: ");
Serial.print(endTime - startTime);
Serial.println(" milliseconds.");

fist time ok..

but while entering second time inside the if condition not getting the correct time ...

wounder1:
but while entering second time inside the if condition not getting the correct time ...

Post your complete sketch maybe?

wounder1:
but while entering second time inside the if condition not getting the correct time ...

Yes. It was just an example to show you the method. Use the same method to time the second part.