Teachers Introduction Course to LEGO® Mindstorms NXT & EV3
(version 4.6)
The Wait block is one of the simplest blocks in the EV3 Software, and provides one of the simplest flow control structures. Its basic function is to pause the execution of a program until a particular condition is met, and then continue with the program. For example, “wait until the touch sensor is pressed”, “wait until the color sensor detects black” or “wait for three seconds”.
But what if you want a program to wait until either of two (or more) possible conditions occur? For example, “wait until EITHER the color sensor detects black OR the touch sensor is pressed”.
This learning sequence explains how the Wait Block is built in the EV3 software, its advantages and limitations, and how to expand its programming functions to accommodate multiple conditions.
In the following example, the program waits until the touch sensor is pressed to start a motor for one rotation.
Wait until the touch sensor is pressed to start a motor
Let us debug the program to understand how its blocks are internally processed. When the previous program starts executing the first block, it immediately stops its execution to wait until the touch sensor is pressed. Then, immediately after the sensor is pressed, the program continuous its sequence, in this case, it starts a motor for one rotation and stops.
Although this flow control structure is very useful for keeping a simple program short, interestingly, many other programming languages do not have anything directly equivalent.
How could we achieve the same function without using the wait block? The answer is simple.
Tip: A Wait block is actually just a loop that repeats until a particular condition is met. It is hidden from the user, but if you could see how the Wait Touch block is implemented within the EV3 Software, it would be equivalent to the Loop block in this example...
Implementation of the Wait Touch Block using an empty Loop to start a motor
Note that a loop can be empty!
If we want to run a motor forever, can we do it with a wait block? The answer is yes although not elegantly, we need to put the Motor block before the Wait block that is set to a condition that will be never met, like shown below.
Run a motor forever or wait until the touch sensor is pressed
If we want to use the touch sensor to stop the motor accurately, we can improve this program by adding, after the Wait Touch block, a Motor Off block set to Brake.
Run a motor until the touch sensor is pressed then brake the motor
How can we achieve the same functionality with a loop?
Run a motor until the touch sensor is pressed
Tip: A Loop block can also be set to Unlimited. That is, the loop will never end (unless a Loop Interrupt block is used in another part of the program).
Run a motor forever using an empty unlimited loop
This solution is perhaps more elegant than the earlier one using a Wait block, why? Because it makes explicit that the loop runs forever… Note that a Wait block cannot be configured for an unlimited wait!
Consider the following program and how it differs from the previous one.
Run a motor forever using an unlimited loop
Both programs are equivalent in fucntion, so what are the differences? The first one only runs the Motor block once, whereas the second runs the Motor block at each iteration of the loop. Although this is less efficient, it is perhaps easier to read and understand. This programing style may be useful to debug long programs.
Let us say we want to wait until EITHER a touch sensor is pressed OR a black color is detected, THEN start a motor. Can we wait for more than one condition using Wait blocks? We could do this, but it is not elegant and has its limitations. We need to have a separate program sequence for each sensor and use Stop Program blocks to force the program to finish once either of the sequences is done…
Wait for the touch and color sensor conditions to start a motor, using wait blocks
This program might work fine for some situations, but it has some problems...
So how can we write a better program? We need to use a loop. Making the loop for a wait condition explicit is very useful if we have more than one condition.
Tip: A Loop block can be set to take a Logic value. This way you can use Logic blocks to control it. This is particularly useful if we want to handle more than one condition.
For example, to wait until EITHER a touch sensor is pressed OR a black color is detected, we can wire the results of the sensors into a Logic OR block that, in turn, is wired into the terminating condition of the Loop structure…
Wait for the touch OR color sensor conditions to start a motor
Alternatively, we could wait until both conditions are met using a Logic AND block.
Wait for the touch AND color sensor conditions to start a motor
If we want to add more conditions, we just need to use more Logic blocks to connect in a chain the sensor outputs of their compare results to the Logic blocks, and connect the last one to the logic input plug of the Loop block.
Acquired knowledge: With this learning unit students will have a deeper understanding of how a wait block works and how it is implemented in the Mindstorms EV3 software. In addition, they will learn how to use loops as flow control structures either associated to a sensor, unlimited or logic, parallel program sequences and one specific use of the stop program block.
You can also find this learning unit at the legoengineering.com webpage.