How to program EV3 to wait for a sensor activation for at most X seconds? If the sensor is activated in the given time, begin sequence B. If it is not activated in X seconds, begin sequence A.

Explanation: My robot arrives at point X, then it waits for the ultrasonic sensor activation (if a hand arrives near the sensor - < 7 cm), but must wait only for 10 seconds. If nothing triggers the ultrasonic sensor within 10 seconds the robot must continue with sequence A. If, for example, a hand is placed in front of the ultrasonic sensor (<7CM) within 10 seconds the robot must continue with sequence B. Then when sequence B is terminated, the robot should continue with sequence A.

I have tried using loops, switches and asking Gandalf for help but with no success.

  • Hi @Zerop! I'll try to edit your question to make it a bit better readable, but I have no idea what you could mean by "Gandalf". Could you please clarify your intent? Thanks!
    – zovits
    Commented Jan 2, 2019 at 13:43
  • Hi !! sorry was only a joke. Solution is hard to find and i wrote that i have ask help else to Gandalf (lord of rings) hoping in help !!
    – Zerop
    Commented Jan 2, 2019 at 14:07
  • Okay, then I'll leave the reference intact :)
    – zovits
    Commented Jan 2, 2019 at 14:28
  • Similar question: bricks.stackexchange.com/q/7822/3498 Commented Jan 3, 2019 at 19:54

3 Answers 3


Here's some pseudocode that does what you need:

Reset timer
Loop block:
    Sensor block, output logical TRUE when condition is reached
    Timer block, output logical TRUE when X seconds have passed
    Logic block, outputs logical TRUE if sensor OR timer is true
End loop when it receives TRUE from the Logic block.
Switch block:
    if sensor = condition, SEQUENCE B
End switch

The sensor and timer blocks' logical data wires are connected to the logic block. The logic block is set to "OR", which ends the loop if either one is true. Then you use a switch to decide whether or not to execute SEQUENCE B, and after that, SEQUENCE A happens regardless.

  • Thanks for help !! I'll try tonight your solution ! thanks to all
    – Zerop
    Commented Jan 3, 2019 at 11:53
  • @Zerop No problem! Just to elaborate a bit, the key here is that you're using a loop (instead of a wait block) to wait until either logic signal is true. Then after the loop, you use a switch to act based on which condition ended the loop. You could easily extend this to more than 2 conditions, or use it to wait until BOTH conditions are true, etc. Just a cool programming trick to keep in mind for the future.
    – MindS1
    Commented Jan 4, 2019 at 3:20
  • MindS1 thanks a lot !!!!! it's worked perfectly !!!! I had almost arrived near it. What was wrong was the switch block. I used the switch block waiting for a true logical condition instead of waiting for the sensor. Now it works perfectly. Thank you so much for the invaluable help !!
    – Zerop
    Commented Jan 4, 2019 at 7:56

One way would be to use a loop with a counter and a built-in waiting period. In each iteration check the sensor readings: if the value indicates the presence of a hand, exit the loop and start sequence B. Otherwise increment a counter and wait for some time. Finally, if the value of the counter indicates that the 10 seconds have passed, execute sequence A.


In EV3 the pseudocode looks like:

MindS1's idea implemented in EV3.

Always surprises me how complex it is to represent simple ideas.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.