Book Image

Learning Lego Mindstorms EV3

By : Gary Garber
Book Image

Learning Lego Mindstorms EV3

By: Gary Garber

Overview of this book

Table of Contents (19 chapters)
Learning LEGO MINDSTORMS EV3
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
10
Other Programming Languages
Index

Switch or two-level controller


We will now use a case structure to add a correction to the turns. In the EV3 language, a case structure or if…then…else statement is called a Switch block. The Switch can be controlled by sensors and other logic statements. Although you typically will have only two cases, you could add several other branches to the case structure. Each branch of the Switch is called a case statement and can contain several programming blocks. We will use the Gyro and rotation sensors to define the case to try and "zigzag" onto the exact 90-degree angle to produce a square. In the following screenshot, you can see the entire code. As this program is extensive, it is worth noting how the wires are used to organize the code into rows, so you can see the entire program on one screen at the same time:

Initially, we asked the Gyro Sensor to only wait until it turned 90 degrees on each leg of our square. Now we will adjust our code to ask the robot to stop turning when it reaches...