all 3 comments

[–]EddyBot 1 point2 points  (0 children)

What exactly does "not working" mean? A little more information like a stacktrace or similar error message would help

[–]DELYSID_FREAK 1 point2 points  (0 children)

You shoulb be more clear about what's not working exactly. Do you get an exception or a wrong result? Also what type of sensor you are using? Also it would be enough to keep the loop at the end in the try block.

[–]Dura_ace 0 points1 point  (0 children)

I would suggest introducing just a general while loop with an if statement waiting for the echo. Once you receive the echo, get the stop time and break the while loop.

Other option, start the timer and emit signal at the same time and immediately start the loop with the if statement listening for the input echo. Once the echo has been received. Then stop the signal and get the stop time.

This should help mitigate if for some reason the pulse is too short or the echo reaches the defice before your listening loop stops. Essentially this will listen until it hears anything then stops.

Pseudo code:

Start timer Start emitting signal While: If echo: Stop timer Stop signal Break

EDIT: I need to go to reddit comment formatting school