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
A
B
D
C
Chapter 5: Making Robots Playful 171
Make_Simple_Robots_interior_FIN.indd 171Make_Simple_Robots_interior_FIN.indd 171 4/26/22 1:56 PM4/26/22 1:56 PM
To make the bot go right when the board is tilted the other way,
you can just make a copy of the first stack of blocks and change
the settings. Right click on the “tilt left” block to open a drop-down
menu, and choose “Duplicate”
(Figure
E
).
Then change the copy
to “tilt right” and move the servo to the other side
(Figure
F
)
. The
example here shows 150 degrees.
Download the MakeCode to the board (see Chapter 4 if you need a
refresher) and see if you like the way the bot works!
3. The advanced version of tilt control works a little differently. You have to
take the measurements produced by the accelerometer and translate
them into degrees the servo will turn. See the “Accelerometer Basics
box on page 168 for a quick explanation of how it works
(Figure
G
)
.
4. Go to the Variables menu (red) to make a variable. Call it “angle.” This
variable will hold the number of degrees the servo shaft should turn.
Then drag a “set [angle] to [0]” block inside a forever block
(Figure
H
)
.
5. Below Advanced on the list of menus, find the Pins menu (brick red).
Drag the fat oval “map [0]” block to the workspace. Insert it into the “set
[angle]” block, right over the zero. Let it snap into place. Then change the
values in the white openings on the “map [0]”
(Figure
I
)
block as follows:
E F
172
Making Simple Robots, 2nd Edition
Make_Simple_Robots_interior_FIN.indd 172Make_Simple_Robots_interior_FIN.indd 172 4/26/22 1:56 PM4/26/22 1:56 PM
G
H
I
Chapter 5: Making Robots Playful 173
Make_Simple_Robots_interior_FIN.indd 173Make_Simple_Robots_interior_FIN.indd 173 4/26/22 1:56 PM4/26/22 1:56 PM
Go to the Input menu and find the oval-shaped “acceleration (mg)
[x]” block. Drag it over the first zero, next to “map,” and let it snap
into place
(Figure
J
)
.
The next two lines are the values from the accelerometer. Change
“from low” to -1023. Don’t forget the negative sign! Then change
“from high” to 1023
(Figure
K
)
.
The last two lines will translate the old low and high numbers to
new ones. They will tell the servo to move to the left and right, but
not all the way. Change “to low” to 30, and “to high” to 150. These
are the numbers that worked best with the example DrawBot, but
you can change them if your design is different.
J
K
174
Making Simple Robots, 2nd Edition
Make_Simple_Robots_interior_FIN.indd 174Make_Simple_Robots_interior_FIN.indd 174 4/26/22 1:56 PM4/26/22 1:56 PM
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.29.224