So far you have used the loop block to infinitely repeat your programs. Did you know that a program can be repeated until a condition evaluates to true without having to use the loop interrupt block?
To access the full video please subscribe to FLLCasts.com
- 08 Feb 2019
By clicking on any of the ends of the loop block, we select it and we can see its settings in the lower part of the window:
Similarly to the wait block which waits until a command is given, the loop block repeats the blocks in its body until the condition for exiting the loop is met.
What exiting conditions are there? There are five types of cycles in EV3 which differ according to the duration of repeating the cycle body. We can see them if we click on the drop-down menu located on the right of the Control button.
1. Forever – this is the setting by default. If chosen, the cycle body will repeat infinitely. If there are blocks after this cycle, they will never be performed! When we have such a cycle, the robot should be manually stopped by the light grey button.
2. Sensor – when choosing this setting, the body cycle will repeat until any of the sensors detects a certain value. We will focus on that setting in the next levels.
3. Time – when choosing this setting, the cycle will repeat for a definite period of time which is set in seconds.
4. Count – this is one of the settings frequently preferred. It allows us to choose how many times the cycle body will be repeated. To make the robot move in a square we will use this exact setting and will set it to 4.
5. Logic – this setting is more complex and won’t be used at this stage of training.
The following example shows how to set the loop block so that the program is repeated until the motor attached to port A turns more than 90 degrees:
As you can see, the conditional loop block can work with all the sensors you have access to.
Remember that the condition is actually a loop interrupt condition. The loop will execute a block of statements repeatedly, until a given condition evaluates to true. When the condition evaluates to true, the next block in the program will be executed.
Important: the condition will be evaluated only once per repetition (iteration) of the loop, after all blocks of the loop body are executed. If you insert a wait block into the loop, you may witness some unexpected behavior.
Courses and lessons with this Tutorial
This Tutorial is used in the following courses and lessons