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?



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.


def moveDistance():

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.