In this chapter, you’ll learn how to set up your programs for the StarterBot you’ve just built. You’ll also experiment with basic movements and learn to verify the results.

Setup and the main program

Each Pybricks programs consists two two parts:

  • Device setup: Here you describe how your robot is built and how the devices are connected to the hub.
  • Program: Here you describe what the robot should do.

To better understand how this works, let’s build an example program using the steps below. Connect to your robot, and run the finished program.

Setup step 1
Setup blocks
Motor setup
Set up two motors
1. Create a new program called 'setup_demo'. From the setup tab, pick two motor setup blocks on and place them on the canvas.
Setup step 2
Give each
motor a name.
Left motor turns
counterclockwise
to make the robot
drive forward.
You can delete
the Print block.
2. Give each motor a name. Since they'll be driving the wheels on the Starter Bot, let's call the one on port A left wheel and the one on port B B right wheel. If you look at the left wheel, you'll notice that it must turn counterclockwise to make the robot drive forward. Change the setting as shown above.
Setup step 3
Drive base setup block
Select both motors
Choose wheel diameter and distance
between wheel ground contacts.
3. Place the drive base setup on the canvas. Configure it to use the left and right wheels. Measure the diameter of your wheels and and the distance between the wheels and enter them as shown. Delete the existing Print block.
Setup step 4
Drive block
Drive forward by 250 mm
Enter -250 to reverse by 250 mm
4. From the output tab, pick two Drive blocks and attach them to the main program block. Leave the first one unchanged. In the second block, change the distance to -250 millimeters.

Verifying and troubleshooting

Connect to the robot and run your program. Your robot should drive forward by 250 mm (about 10”) and then reverse, ending up where it started. Otherwise:

  • If it didn’t move at all, be sure to check the cables and the ports you selected, as shown below.
  • If it started by turning or reversing instead of moving straight forward and back, check the directions in step 2 above.
  • Place a ruler next to the robot. If it drove too far or not far enough, measure the wheel diameter again.
Setup step 5
A motor is not
connected correctly
5. Error messages can help you find some mistakes early. To try it, run the program again with one of the cables in the wrong port.

Running a very basic program with just a few movements is a good way to check that your setup is configured properly.

This setup will be a big time saver when you create larger programs later on. If you change your design including the cables, all you have to do is change one port in the setup, rather than changing it throughout your entire program.

Changing parameters

The blocks you’ve placed under the yellow program block run one by one, from top to bottom. Each block is one command for the robot to execute. Different blocks make the robot do different things. Each block has parameters that change how it executes that command. For example, you can change how far it drives and use a negative value to reverse.

We’ll get to each of these settings in the chapters to come, but this is a good time to give some of the other options a try. For example, you can change the Drive block to make a turn, as shown below.

Setup step 6
Choose different
drive methods
You can change the options on a block to change what that block does.

Once you’ve changed settings, you can see what that block does by reading it from left to right: The robot (that you’ve set up) drives straight (your choice) for 250 mm (your distance value) and then actively holds the motors in place (your choice).

When changing the settings, it’s best to do so from left to right as well, because other settings may change when you do. For example, if you select straight, an option appears to choose how many millimeters. But if you select turn, an option appears to choose how many degrees.

The comment block

No matter how simple your program is, it is always a good idea to document your work. Code comments help you document your work for yourself and others. Even if a design choice appears obvious today, a comment will help you remember a few weeks later.

Comment block
Other blocks
Comment block
Document your work
Comments help you document your work. When you review your code later, you'll be able to see how it works and why you made it that way.

Some comments can be descriptive. They might summarize what a sequence of blocks do. Other comments can describe why you’ve done it in a particular way. You can also describe the required conditions that are needed to run your program. Comments are filtered out before your code is sent to the robot, so comments have no impact on performance or robot code size.

It is possible to leave a comment block floating on the canvas if you need to, but it is best practice to attach it like a normal block. This way, it moves along with your code automatically when you add other blocks above or below it.

Further exploration

In this chapter, you’ve learned to build a program to make your robot move. You’ve learned how to change settings and to document your work with comments. The following challenge will help you practice your skills.

Challenge #3A: Living on the edge ⸺ Pick a fixed starting point on your desk and measure the distance to the edge of your desk. Tell the robot to drive exactly that distance and back again. Tip: On your first try, to reduce the value slightly. Be prepared to catch you robot if it falls. Safety first! When you’re away from the computer, you can stop the program using the hub button.

Challenge #3B: Spell your name ⸺ Can you put together a sequence of Drive blocks so that it drives along the first letter of your name? What about your whole name? Hint: Besides in-place turns, you can also choose a curve with a given angle and radius. When you’re satisfied with the result, try attaching a pen to the robot so it draws your name as it moves.

Challenge #3C: Satnav subtitles ⸺ Previously, you’ve experimented with the Print block. Add print blocks to your program to make the robot indicate exactly what it will do. For example, make it say “Turning 90 degrees!” Where do you put these Print blocks? Should they go before or after each Drive block?