2 things due to time received near ultrasound sensor at the same distance

Good day.
Please advice the following, I’m going to make an automatic flush at the toilet. I’m going to use ultrasound sensor and 2 servo. My flush system has 2 buttons, one will put a small ammount of water, another - big, due to this I’m going to use 2 servos.
I will make a program which will measure the distance from the sensor to the my hand, if my hand will be in the distance between 3 to 5 cm for 3 seconds, then arduino will give the command to first servo, if my hand will be at the same distance for 5 seconds, then the command will go to another servo.
So i need to count the time, how many seconds my hand will be near the sensor.
I don’t have at mind how to do this.

Please advice.

Thanks in advance.

I don't have at mind how to do this.

Is my hand in range? No. Check again later. Is my hand in range? No. Check again later. Is my hand in range? No. Check again later. Is my hand in range? No. Check again later. Is my hand in range? No. Check again later. Is my hand in range? Yes. Record the time. Is my hand in range? Yes. No change, so do nothing. Is my hand in range? Yes. No change, so do nothing. Is my hand in range? Yes. No change, so do nothing. Is my hand in range? Yes. No change, so do nothing. Is my hand in range? Yes. No change, so do nothing. Is my hand in range? No. A change, so record when my hand went out of range. Do something based on the difference in the recorded times.

PaulS has without a doubt, the best way of explaining this kind of situation. Crystal clear.

PaulS: Is my hand in range? No. Check again later. Is my hand in range? No. Check again later. Is my hand in range? No. Check again later. Is my hand in range? No. Check again later. Is my hand in range? No. Check again later. Is my hand in range? Yes. Record the time. Is my hand in range? Yes. No change, so do nothing. Is my hand in range? Yes. No change, so do nothing. Is my hand in range? Yes. No change, so do nothing. Is my hand in range? Yes. No change, so do nothing. Is my hand in range? Yes. No change, so do nothing. Is my hand in range? No. A change, so record when my hand went out of range. Do something based on the difference in the recorded times.

Hi, thanks for your answer. But U didn't get my question, i need to move my servos, while my hand will be at the same distance. If my hand will be at 5cm distance for 3 seconds, then I will move 1st servo, if my hand will be at 5 cm distance for 5 seconds, then I'll move 2nd servo.

Do you mean if it's there for 5 seconds, the first servo should have come on at 3 seconds followed by the second one at 5?

While the hand is there, ie all those "yesses", check the elapsed time since you started the timer, and if it's >3 do servo one stuff and later if it's >5 do servo two stuff. If the hand goes away before 3, or after 3 but before 5, "reset". If both servos have been activated, "reset" for the next customer.

You might like to look at finite state machines....

JimboZA: Do you mean if it's there for 5 seconds, the first servo should have come on at 3 seconds followed by the second one at 5?

While the hand is there, ie all those "yesses", check the elapsed time since you started the timer, and if it's >3 do servo one stuff and later if it's >5 do servo two stuff. If the hand goes away before 3, or after 3 but before 5, "reset". If both servos have been activated, "reset" for the next customer.

You might like to look at finite state machines....

No, one servo should work only, not one followed by another. For example, distance is 5 cm, hand is near the sensor for 3 seconds, after this the hand will be removed (the distance will change) - 1st servo start to work. Distance is 5 cm, hand is near the sensor for 5 seconds, after this the hand will be removed (the distance will change) - 2nd servo start to work. If the time will be for example 2 or 6 seconds and the hand will be at distance 5 cm, no action will be.

Well I’m pretty sure with PaulS’ previous input you can write a scheme along those lines and code accordingly. If you can write down what you want to do, the coding’s the easy part.

But then the question back at you is… do you know any Arduino coding yet? If not, you need to work through the examples. If you do, then draft your sketch and come back for specific help if you get stuck.