Chapter 9. using the brick buttons and rotation sensors

In addition to the Touch, Color, and Infrared Sensors, the EV3 contains two types of built-in sensors: Brick Buttons and Rotation Sensors. You can use the Brick Buttons on the EV3 brick to control or influence a program while it’s running. For example, the program can ask you to press one of the buttons to choose what the robot should do next.

Each of the EV3 motors has a built-in Rotation Sensor that determines the position of the motor, allowing you to precisely control wheels or other mechanisms. The sensor also measures the motor speed, making it possible to detect when a motor is moving slower or faster than intended.

You can use the EV3 brick’s Up, Down, Left, Right, and Center buttons in your programs just as you use the Touch Sensor. You can make your robot respond by playing a sound when you press a particular button, for example. You can also make the robot wait for the button to be released or bumped (a press followed by a release).

One interesting way to use multiple buttons in a program is to create a menu on the EV3 screen, letting you choose the next action in the program. The ButtonMenu program in Figure 9-1 plays one of three sounds based on which button the user presses.

Two Display blocks show a simple menu on the screen, asking the user to choose whether the robot should say “Hello,” “Okay,” or “Yes.” Then, a Wait block (in Brick Buttons – Compare mode) pauses the program until the user presses either the Left, Center, or Right button.

Next, a Switch block (in Brick Buttons – Measure mode) determines which button is being pressed, and the robot plays the requested sound. After the Wait block completes, the Switch block runs so quickly that the button is still pressed by the time the Switch checks the button state, even if you release it right away.

When you tell the robot to move forward for three rotations with the Move Steering block, the vehicle knows when to stop moving because the Rotation Sensor in each EV3 motor tells the EV3 how much it has turned. The program can also tell you how fast a motor is currently turning by measuring how fast the motor position changes.

You can use Wait, Loop, and Switch blocks in Motor Rotation mode to measure motor position (Degrees mode or Rotations mode) and motor speed (Current Power mode).

The motor position tells you how much a motor has turned since you started the program. Use the Port View app on the EV3 brick, navigate to motor B or C, and rotate the motors with your hands to see the sensor values change.

When you first start Port View (or your own program), the sensor value is 0. The value becomes positive when you rotate a motor forward; it becomes negative if you turn it backward past 0, as shown in Figure 9-2. For example, if you rotate the motor forward by 90 degrees and then backward for one rotation (360 degrees), the motor should report a position of −270 degrees.

You can use the position measurement to create a program that plays a sound if you turn one wheel 180 degrees forward by hand, as shown in Figure 9-3. A Wait block in Motor Rotation – Compare – Degrees mode waits until the Rotation Sensor value is greater than or equal to () 180 degrees. Because these sensors are built into the EV3 motors, they are always connected to output ports (you use the motor on output port B in this program).

The Rotation Sensor calculates how fast a motor turns as a value between −100% and 100% based on the rate at which the motor position changes. The value is positive when the motor turns forward (blue arrow in Figure 9-2), negative when the motor turns backward (green arrow), and 0 when the motor is not turning.

For the Large Motor, a Current Power sensor value of 50% corresponds to a rotational speed of 85 rotations per minute (rpm). You can reach this speed by rotating a motor with your hands or by using any of the Move blocks with its Power setting at 50%.

So far, you’ve been using several kinds of green Move blocks to make your robot move. These blocks make the motors turn at a constant, regulated speed. When the motors slow down because of an obstacle or an incline, the EV3 supplies some extra power to the motor to keep it going at the desired speed. The Power setting on these blocks actually specifies the speed that the motors try to maintain. That is, a Large Motor turning at 20% speed (34 rpm) while performing a heavy task might consume more power than a motor doing a light task at 40% speed (68 rpm).

When you don’t want the EV3 to supply that extra power to maintain constant speed, you can use unregulated speed.

Now that you’ve learned how to work with all of the sensors in the EV3 set, you can create robots that interact with their environment. The EXPLOR3R is, of course, only one example. As you continue reading this book, you’ll build several robots with sensors, each of which will use sensors differently.

So far, you’ve learned to use the components that are essential to create a working robot: the EV3, the motors, the sensors, and the programming software. The following chapters will explore each of these subjects in more detail so that you’ll be able to create increasingly sophisticated (and fun!) robots. In the next chapter, you’ll begin looking at how you can use the Technic building elements in the EV3 set to construct your own robots.

The following Discoveries will help you explore more possibilities with the sensors you’ve seen in this chapter.