Chapter 4. working with programming blocks: action blocks

Chapter 3 taught you the basics of how to create a new program and transfer it to the EXPLOR3R robot. In this chapter, you’ll learn how to use programming blocks to create working programs and how to make the EXPLOR3R move.

You’ll also learn how to make your robot play sounds and display text or images on the EV3 screen, and you’ll learn how to control the EV3 brick’s colored light. After you’ve had a chance to practice a bit with the sample programs in this chapter, you’ll be challenged to solve some programming puzzles by yourself!

EV3 programs consist of a series of programming blocks, each of which makes the robot do one particular thing, such as move forward for one second. Programs run the blocks one by one, beginning with the first block on the left. Once the first block finishes running, the program continues with the second block, and so on. Once the last block finishes running, the program ends.

Each programming block has one or more modes and several settings for each mode. You can modify a block’s actions by changing its mode and settings. For example, in Figure 4-1, both blocks are Move Steering blocks, but they’re in different modes. Because the blocks use different modes and different settings, they make the robot perform two different actions.

There are many types of programming blocks, each with its own name and unique icon so you can tell them apart. Different types of blocks are meant for different purposes, with similar blocks grouped together by color in the Programming Palette. In this chapter I’ll focus on using Action blocks (the green blocks in the Programming Palette).

The Move Steering block controls the movement of a robot’s motors. By using it in your program, you can make the EXPLOR3R move forward or backward and steer left or right. We used a Move Steering block in Chapter 3 to make the EXPLOR3R move forward for a short while.

Before we get into how the Move Steering block works, let’s make a small program to see it in action. This program will make the EXPLOR3R drive backward until the wheels make two full rotations and then spin quickly to the right for three seconds. Because these are two different actions, you’ll use two Move Steering blocks.

  1. Create a new project named EXPLOR3R-4. You’ll use this project for all of the programs you make in this chapter. Change the name of the first program to Move.

  2. Pick two Move Steering blocks from the Programming Palette and place them on the Programming Canvas, as shown in Figure 4-2.

  3. By default, the blocks you’ve just placed are configured to make the robot go forward for a little while, but we want to change the first Move Steering block so that it makes the robot drive backward for two wheel rotations. To accomplish this, change the settings on the first block, as shown in Figure 4-3.

  4. Now you want to modify the settings on the second block. This block will make the EXPLOR3R spin quickly to the right for three seconds. First, change its mode to On for Seconds, as shown in Figure 4-4.

  5. To make the robot spin quickly to the right for three seconds, change the settings on the second block, as shown in Figure 4-5.

  6. Once you’ve configured both Move Steering blocks, you can download the program to your robot and run it. You’ll know it’s working if the EXPLOR3R goes backward until both wheels make two rotations and then turns around quickly for three seconds.

Let’s look more closely at the settings on the blocks to better understand how that sample program really worked. Each block’s action is determined by its mode and settings. Figure 4-6 shows several ways to configure the Move Steering block, besides the ones we’ve already used.

The Move Steering block has several modes, each of which can be selected by clicking the Mode Selector and each of which makes the block do something slightly different. For example, the first block in our program was in On for Rotations mode, which allows you to choose the number of motor rotations the robot will move for, while the second was in On for Seconds mode, which allows you to specify how long the motors will move in seconds.

The modes in the Move Steering block are as follows:

  • On: Switch on motors.

  • Off: Stop the motors.

  • On for Rotations: Turn on motors for a specified number of complete rotations; then stop.

  • On for Seconds: Turn on motors for a specified number of seconds; then stop.

  • On for Degrees: Turn on motors for a specified number of degrees of rotation; then stop.

You’ll learn to use the On and Off modes in the on and off modes in move blocks.

When you use a Move Steering block to make the robot turn 90 degrees, you might think you need to set the Degrees setting to 90, but this is not true. In fact, the Degrees setting specifies only how many degrees the motors—and, as a result, the wheels—turn. The actual number of degrees the motors should turn so the robot makes a 90-degree turn is different for every robot. Discovery #2 gets you started finding the appropriate number of degrees for your robot.

It’s fun to make programs that make the EXPLOR3R move, but things get even more fun when you program the EV3 to make sounds using the Sound block. Your robot can play two types of sounds: a simple tone, like a beep, or a prerecorded sound, such as applause or a spoken word like “hello.” When you use a Sound block in your programs, the robot will seem more interactive and lifelike because it can “talk.”

Even though every programming block allows the robot to do something different, all blocks are used in the same way. In other words, you can simply pick a Sound block from the Programming Palette and place it on the Programming Canvas as you did with the Move Steering block. Once the block is in place, select the mode and then adjust the settings to specify what sound you want to hear.

But before we create a program with Sound blocks, let’s briefly explore the different modes and settings for this block, as shown in Figure 4-10. The four modes of the Sound block are as follows:

  • Play File: Play a prerecorded sound, like “hello.”

  • Play Tone: Play a tone at a certain frequency (pitch) for a specified amount of time.

  • Play Note: Play a note from a piano for a specified amount of time.

  • Stop: Stop any sound that’s currently playing.

Now let’s create a program called SoundCheck that makes the robot move and play sounds so you can see how the Sound block works.

To begin, create the new program shown in Figure 4-11. To create this program and the other examples throughout this book, follow these steps for each block in the program:

  1. Locate the block on the Programming Palette using its color and icon and then place it on the canvas. For example, the Sound block is green, and its icon resembles a speaker.

  2. Select the correct mode by looking at the icon shown in the Mode Selector. For example, the piano icon on the third block indicates that you should select Play Note.

  3. Finally, enter the remaining settings in the block. For example, configure the Steering and Power settings in the Move Steering blocks. Your block should now look exactly the same as the one in the example program.

Once you’ve finished creating your program, download it to your robot and run it.

In addition to moving the robot around and playing sounds, an EV3 program can control the EV3’s display. For example, you could create a program that makes the EV3 screen look like Figure 4-12. The screen is 178 pixels wide and 128 pixels tall. (Pixels are small dots that make up what you see on a screen.)

It’s fun to play with the Display block, but perhaps more importantly, it’s an effective way to test your programs. For example, you can display a sensor measurement on the screen to see whether the sensor is working properly, as you’ll see in Part V of this book.

You can use the Display block to display an image (like a light bulb), text (like “Hello!”), or a shape (like a solid circle) on the EV3 screen. A single Display block can’t put multiple images or text lines on the screen at once, so you would need to use a series of blocks to create the display shown in Figure 4-12.

Once a Display block has put something on the EV3 screen, the program moves on to the next block—let’s say a Move block. The EV3 screen keeps showing the image until another Display block is used to display something else. So, in this example, the image would remain on the screen while the robot moves.

When a program ends, the EV3 returns to the menu immediately, which means that if the last block in your program is a Display block, you won’t have time to see what’s on the display because the program will have finished. To see what’s on the display, add a block—such as a Move block—to keep the program from ending instantly.

Figure 4-13 shows the four main modes of the Display block:

  • Image: Display a selected image, such as a happy face, on the screen.

  • Shapes: Display a line, circle, rectangle, or dot on the screen.

  • Text: Show a text line on the screen.

  • Reset Screen: Erase the screen and show the MINDSTORMS logo that you normally see when you run a program without Display blocks.

Some modes have sub modes. When selecting Shapes mode in the Display block, for example, you’ll have to choose among four sub modes—Line, Circle, Rectangle, and Point—as shown in Figure 4-13. Choosing Shapes – Circle mode makes the Display block show a circle shape on the EV3 screen, and you use the settings on the block to configure the circle’s position, radius, fill, and color.

In Text mode, enter a line of text that you want to display, such as MINDSTORMS, in the Text field, as shown in Figure 4-13. The text line may include numbers, and you can adjust the size of the text by setting the Font Size value to 0 (small), 1 (bold), or 2 (big).

Let’s test the functionality of the Display block by creating a program that puts things on the EV3 screen while the robot moves. Create a program called DisplayTest, and place three Display blocks and two Move Steering blocks on the Programming Canvas, as shown in Figure 4-14. Then, configure each block as shown. Once you’ve configured all the blocks, transfer the program to your robot and run it.

The brick status light surrounding the EV3 buttons is normally green. It blinks while a program is running. With the Brick Status Light block, you can override this behavior and choose what this light does. Its three modes, shown in Figure 4-16, are as follows:

  • On: Switch on the light and choose a Color: Green (0), Orange (1), or Red (2). The Pulse setting lets you choose whether the light should blink (true) or just stay on (false).

  • Off: Switch off the light.

  • Reset: Show the blinking green light that you normally see when a program runs.

Now make the ButtonLight program to test this functionality, as shown in Figure 4-17. The buttons should light up red when the robot says “Red” and green when it says “Green.”

Now that you have the hang of programming with some Action blocks, you’re ready to examine the On and Off modes found in the Move Steering block and several other blocks. A block in On mode will switch on the motors and then instantly move to the next block. As the program continues, the motors keep running until the program reaches a block that tells them to stop or do something else. The Off mode turns the motors off.

To see why this is useful, create the OnOff program shown in Figure 4-18.

When you run the program, the robot should begin to move and say “LEGO MINDSTORMS” at the same time. When it’s done speaking, it should stop moving and play a tone. The motors stop moving exactly when the robot finishes saying “LEGO MINDSTORMS,” even though you didn’t say beforehand how long they should stay on. This allows you to make the robot move until you stop it with the Off mode, regardless of what happens in between.

If you were to create a program with just one Move block in On mode, you might think that the robot would go forward indefinitely. In fact, this block switches on the motors, but then the program ends because it finishes running all of its blocks; when the program ends, the motors stop.

Besides the Move Steering blocks, there are three more blocks that you can use to make motors move. The first is the Move Tank block, which you can use to control a vehicle with two tank treads or two wheels, such as the EXPLOR3R, much as you did before. But instead of choosing steering and power for the whole robot, you can now choose the power of each motor independently. By using different combinations of power for the left and right wheels, you can drive EXPLOR3R in different directions (see Figure 4-7).

The Tank program demonstrates how the Move Tank block works (see Figure 4-19). This block has essentially the same functionality as the Move Steering block, but you control the settings differently. Use whichever block you prefer when making your programs for these types of vehicles.

If one of the motors is programmed to turn faster than the other (as in the Tank program), the block ends as soon as the fastest motor reaches the indicated number of rotations or degrees. (The same holds true for the Move Steering block.)

You use the Large Motor block to control individual Large Motors. This is useful for mechanisms that use only one motor, such as a claw that grabs an object. The modes and settings are the same as they are for the Move Tank block, but now they apply only to the selected motor.

The program in Figure 4-20 uses two Large Motor blocks to drive the left (B) and right (C) motors respectively, one at a time.

The Medium Motor block is the same as the Large Motor block, except you use it to control the single Medium Motor that comes with your EV3 set. (You’ll see it in action in Chapter 12.)

You’ve learned the basics of LEGO MINDSTORMS EV3 programming. Congratulations! You should now know how to program robots to make them move, play sounds, blink a light, and display text and images on the EV3 screen. Chapter 5 will teach you more about using programming blocks, including how to use blocks to pause a program and repeat a set of blocks.

But before you move on, try solving some of the Discoveries that follow to further fuel your programming skills.