To practice working with sensors, you can try an easy version that simply
moves the marker all the way left or all the way right. Then move on to the
version that moves the marker different amounts, depending on how far the
micro:bit is tilted. The program isn’t very long, but it involves a new concept
called mapping. Mapping lets you take the numbers that represent what the
accelerometer measures and translate them into different numbers that
tell the servo how far to turn. See the “Accelerometer Basics” box on page
168 for a quick explanation of how it works.
1. Begin by setting the servo in the neutral position:
• Open the Servos menu (see the “Servos and MakeCode” box on
page 165 for how to do that) and drag the “set servo [P0] to [90] °”
block to the workspace.
• Place it inside the “on start” block to activate it.
2. To write the easy version of the tilt-control remote control:
• Drag an “on [shake]” block from the Input menu (light purple) into
the workspace
(Figure
A
)
.
• Click on the little triangle-shaped down arrow to open the drop-
down menu. Choose “tilt left”
(Figure
B
).
• Inside the “tilt left” block, put another “set servo” block. Open the
slider and move it a bit to the left. How far will depend on the design
of your DrawBot. You don’t want to move the marker so much
that the robot tips over. In the example here, the servo is set to 30
degrees
(Figure
C
)
.
• Add another servo block and return it to neutral (90 degrees). In
between, add a pause block from the Basic menu (blue). Set it for 1
second (1,000 ms)
(Figure
D
)
.
170
Making Simple Robots, 2nd Edition
Make_Simple_Robots_interior_FIN.indd 170Make_Simple_Robots_interior_FIN.indd 170 4/26/22 1:56 PM4/26/22 1:56 PM