Situations and environments can change as your robot moves around. The Switch block allows your robot to make a decision on the fly about what actions to take based on the situation it’s in. The Switch block runs a test based on sensor data, a variable or a calculation and picks a sequence or case to run based on the outcome of that test.
- [Voiceover] I've opened the exercise file 06_03_Switch_Colour_Start with three different sequences. If the robot starts on the blue dot on the map, I want the top sequence to run. This will make the robot move forward and display neutral eyes. If the robot starts on the green dot, I want the second sequence to run. This will make the robot turn in place and display dizzy eyes. Finally, if the robot starts on any other location on the map, my robot should run the last sequence, which will make the robot display a warning sign and play an error sound.
I can make three separate programs and manually choose which one to run. But I want my robot to make that decision itself so I only have one program to maintain. The switch block helps my robot make that decision itself while the program is running. This block picks a sequence of blocks called a case to run it based on a test condition. This test can be based on a piece of sensor data, a variable, a data wire, or any number of things.
It's evaluated at the beginning of the switch block. Then it'll pick only one of the sequences to run. I can find the switch block in the flow control pallet. Since I'm looking at color on the map, I'll pick the test option color sensor measure color. The color sensor is connected to port four, so I'll update the port selector to that port. In this first case, I want the top sequence to run if the color detected is blue. So I'll pick the value blue for this case and move the first sequence in to the case by highlighting the blocks and moving them over.
The second case will be set up for the color green and I'll move the second sequence in to that case. By default, there are two cases, but I need three for my program. To add another case, I'll click on the Add Case button. A new case is added to the bottom and I'll use this for the last sequence which is any other color. I can only pick one color for each case. I could make a separate case for every color, but that's excessive. If I need to update this sequence in here, I'll have to update it for multiple cases which can lead to errors if I miss a case.
Instead, I'll select a color and then click on the Default case button. Now, if the color detected doesn't match any of the other cases, it will execute this case by default. If I don't want anything to happen in my default case, I could leave this case empty. Before this program, I want my third sequence to execute so I'll select it and drag it over. When this runs, if the color sensor isn't plugged in, the switch block will not wait to get data from the sensor. Instead, it will immediately run the test and if a value is not available, it will use the default.
My three cases don't quite fit nicely on the screen. However if I had more cases, or longer sequences inside or more to the program beyond the switch block, things are going to get even more cramped. To help minimize screen space, the view of the switch block can be changed to tab view by clicking on the flat tab selector. Now all my cases have their own separate tab and I can click on each individual one to see the corresponding sequence.
Switching between the views, doesn't affect the function of my program. It just changes how it's shown on the screen. My program is all set. I only need to download this once and it will successfully execute the correct sequence no matter where I place it on the map. I take my robot over to the map and place it on the green dot. When the program runs, my robot runs the switch test and detects the color green, triggering the case to spin around.
Now I'll place my robot on the blue dot and run the program again. This time my robot chose the blue case from the switch block and moved forward. As the environment and situations change the switch block is a really useful tool to run the sequence of blocks on the fly.
Learn how to unpack the hardware, connect to the programming interface, run prebuilt programs, and control the motors and sensors. Chapter 6, "Adding Flow to a Program," covers advanced programming topics such as transferring data between blocks, using variables to store temporary data, programming conditional logic, and looping actions to run again and again. By the end of the course, you'll have the skills to bring your own robotic creations to life.
- Installing the LEGO Mindstorms software
- Exploring the programming interface
- Connecting to the EV3 brick
- Downloading and running prebuilt programs
- Coordinating motor movement
- Calibrating the sensors
- Transferring data with data wires
- Adding logic with switch blocks
- Looping actions
- Troubleshooting problems with LEGO Mindstorms
- Broadcasting sounds and displaying images
- Updating LEGO Mindstorms