is PulseIn() function works parallel with main program?

hi

I need to measure incoming pulse width, but I don't need to wait until arduino measure it,
In other words , if incoming pulse is 1 seconds , arduino should start measuring the width but other works should done in that 1 seconds, just after measuring finishes it return value.

something like arduino timer, that run parallel with program.
Is inbuilt pulseIn() function use internal timer and run parallel? if not any other method to do so?

thank you

frechi91:
Is inbuilt pulseIn() function use internal timer and run parallel? if not any other method to do so?

No, "pulseIn()" is a blocking function call, that will wait until

  • either a pulse is received
  • or if timeout occurred (default timeout== 1000 milliseconds)

For a non-blocking programming logic you should NOT use pulseIn().

You could try using a pin change interrupt instead, and doing the timing with millis()-based code.

Thanks both of you.

I have one problem with using millis or micros , when it overflows, answer for time difference is wrong.

what can we do to avoid that? we cant reset millis , so ??

unsigned long prevMillis=millis(); // Edit: Put this in setup
unsigned long period=5000;
void loop()
{
    if(millis()-prevMillis>=period) // no overflow problem
    {
        // Do whatever's needed when time elapses.
    }
}

I have one problem with using millis or micros , when it overflows, answer for time difference is wrong.

Only if you write your code wrong.

what can we do to avoid that?

Write your code properly, using subtraction only (NO addition of times).

we cant reset millis , so ??

Sure you can. Just reset the Arduino.

frechi91:
I have one problem with using millis or micros , when it overflows, answer for time difference is wrong.

Time differences can always be correct while there is no or one overflow between two time stamps. Depends just on the correctness of your code.

Time differences will always be wrong while there are two or more overflows between two time stamps.

So with "micros()" you can create correct time differences of times up to ca. 70 minutes.
And with "millis()" you can create correct time differences of times up to ca. 49 days.