Ultrasonic time

How i can run a code that mean if distance is bigger than 17cm for a 5 seconds do something ?

Save the time in milliseconds when the distance first exceeds 17 cm, and check the time difference when the distance is less than 17 cm.

If the difference is more than 5000 milliseconds, do something. Otherwise start over. You may need a state variable to remember that you are tracking times.

Remember to use unsigned long variables to store milliseconds.

I mean that if the distance is bigger than 17 for the entire 5 seconds not only in two point.
My project is a self parking car, i have to check if there is a blank parking slot, so i have to check if there is an square(for example) that is 15×15, the length is okay, but the width is my problem.

It is best to have, and solve, one problem per post.

Technically it is the same question in another way

Then I have given you a start on the solution. Let us know how you get on!

I will try and tell you what happen👌

Cam you explain you solution one more time ?

Study the Arduino State Change Detection and Blink Without Delay examples.

If distance is less than 17cm, restart a timer:

  if (distance <= 17)
    StartOfTimer = millis();

If the timer ever reaches 5 seconds, do something:

  if (millis() - StartOfTimer >= 5000ul)
    // Do something

Can i do 2 timers at the same time?

Yes. You can do as many timers as you have memory for.

Ok i will try, thanks🙏

Can you write the full code for me?

To hire someone to write code for you, flag your post and ask the moderators to move it to "Gigs and Collaboration".

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.