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

Subroutines or My Blocks


In this section, we will program the robot to move forward until it encounters an unknown obstacle with the Touch Sensor and then attempt to steer around the obstacle. An obstacle such as a table or chair leg would be ideal. We will program the robot to take input from the brick buttons. The program we will initially write is inefficient and repetitive. It is actually a good example of how you shouldn't write a program. We will first simplify the code using the EV3 version of a subroutine or function that is called My Block. You can think of a My Block as a small program within a large program. It is called a My Block because it is a block that you create.

At the beginning, the robot will move forward until the Wait block is triggered by a change in state of the Touch Sensor. We will then program the robot to move back for one rotation of the wheels and display the following message onto the brick screen: Press left or right. The next Wait block will wait for the...