Waiting for touch

This program shows 3 ways to wait for a touch led to be pressed. Is there a preferred way of doing it and why?

https://www.robotmesh.com/studio/5e27371c9802b70281d927c2

Thanks

I’m fairly new so consider that.

I believe your code will block, meaning no other execution until the condition is met. If that meets your needs then any should work although I’ve never used the “sys.wait.for” that way but assume it works OK. I’ve been exploring using the Python version of threads which would allow your code to run independently of other code.

ex.

def moveDistance():
sys.run_in_thread(runLeftMotor)
sys.run_in_thread(runRightMotor)

runs those functions without blocking other code. That would allow you to process other functions/code while still checking for and then reacting to the touch event.
PS the above code with the associated functions starts both motors nearly simultaneously where if put one after another in main code stream there is a momentary delay in starting the second motor. You could just put your touch monitoring code in a" threaded" function.

Personally, I prefer the third way just because I think it’s easiest to read. Other than that, there’s no big difference between them under the hood and it largely comes down to preference.