Servos and MakeCode
To program a servo motor with MakeCode,
follow these steps:
1. MakeCode has special blocks to use with
servos, but you have to add them to the
list of categories. To find them:
Go to the bottom of the category list
and click on “Advanced”
(Figure
A
).
Scroll down to the bottom and click
on “Extensions.
You’ll jump to a new page, with
many different extras you can add
to MakeCode. Look for “servo” (with
a green-and-blue
drawing of a servo).
Click on it and you
will jump back to the
MakeCode workpage
(Figure
B
)
. The menu
for Servos in dark
green will appear
in the middle of the
other categories.
2. Open the Servos menu
and drag the “set servo
[P0] to [90] °” block to the
workspace. Place it inside
a block like “on start” to
activate it
(Figure
C
)
.
A
B
C
Chapter 5: Making Robots Playful 165
Make_Simple_Robots_interior_FIN.indd 165Make_Simple_Robots_interior_FIN.indd 165 4/26/22 1:56 PM4/26/22 1:56 PM
3. Click on the number 90 to open a slider that lets you rotate the
servo horn to anywhere within a half-circle
(Figure
D
)
. Positions
are measured using a scale of zero to 180 degrees. (The teensy
circle next to the number 90 is the symbol for “degrees.” In
math, a circle is divided up into 360 slices called degrees.
They’re like the minute marks on a clockface. Half a circle has
half of 360 degrees, or 180.) The midpoint is 90 degrees, where
the servo shaft points forward on the servo. This is the servo’s
neutral position, which means the motor is at rest. So always
start and end with your servo at 90 degrees.
4. To move the servo to a different position, add more servo blocks
to the stack, and change the position of the servo horn by using
the slider or typing in the number of degrees. To avoid straining
the little servo motor, don’t make it go all the way to the ends.
Limit its movement so it only goes between around 10 degrees
and 170 degrees
(Figures
E
and
F
)
.
5. To give the servo time to move before the program goes onto the
next step, put a pause from the Basic menu (blue) after every
D
E F
166
Making Simple Robots, 2nd Edition
Make_Simple_Robots_interior_FIN.indd 166Make_Simple_Robots_interior_FIN.indd 166 4/26/22 1:56 PM4/26/22 1:56 PM
servo block
(Figure
G
)
. Set the pause for around one second
(1000 ms)
(Figure
H
)
.
6. Remember to end with the servo at 90 degrees
(Figure
I
)
.
That’s it!
G
H
I
Chapter 5: Making Robots Playful 167
Make_Simple_Robots_interior_FIN.indd 167Make_Simple_Robots_interior_FIN.indd 167 4/26/22 1:56 PM4/26/22 1:56 PM
Accelerometer Basics
An accelerometer detects tilt and speed by comparing its readings
to Earth’s gravity. The pull of gravity on something sitting still on
the Earth’s surface is equal to 1g, which stands for “one gravity.”
When you move in any direction, you’re adding to, or subtracting
from, the pull of gravity, so the g-force goes up or down. On a
micro:bit, the accelerometer measures forces in milli-gravities:
1,000mg equals 1g.
The micro:bit’s accelerometer can also tell you which direction the
movement is pointed in. Each direction is measured along an axis,
like the kind used in graphs in math. The directions compared to the
front of the micro:bit are:
x-axis: tilt side to side
y-axis: tilt forward and backward
z-axis: up and down (compared to the floor)
On a graph, each axis is shown as a line, and all the lines cross
in the middle. That point is zero on every axis. When you move to
the top or to the right on the graph, or up towards the ceiling, the
168
Making Simple Robots, 2nd Edition
Make_Simple_Robots_interior_FIN.indd 168Make_Simple_Robots_interior_FIN.indd 168 4/26/22 1:56 PM4/26/22 1:56 PM
numbers get bigger (1, 2, 3 and so on). Move to the bottom, to the
left, or down to the floor, and the numbers get smaller. They are
negative numbers (-1, -2, -3, and so on) because they are less than
zero.
For this project, you will be using readings along the x-axis in
micro-gravities (mg). To show different amounts of tilt, the micro:bit
translates those readings into numbers between -1023 (tilted all the
way left) and 1023 (tilted all the way right). Zero is level (laying flat).
You will have to take the micro:bit’s number and translate it again,
to degrees between 0 and 180 that the servo will understand. (See
the “Servo Basics” box on page 162 for how that works.)
Why 1023 and -1023? Computers are basically a big collection of
switches, which can be either on or off. To do their calculations, they
use only two digits: 0 and 1. (That’s called the binary system.) The
micro:bit is a tiny computer that has a limited amount of memory,
so the designers assigned 210 (two multiplied by itself 10 times)
for the accelerometer. That equals 1024, but since computers start
counting at zero instead of one, you have to subtract one from 1024.
The answer is 1023!
INSTRUCTIONS FOR PROGRAMMING THE DRAWBOT
WITH MAKECODE
To make your DrawBot scribble back and forth as it jiggles around, you will
write code that turns the micro:bit board into a wired remote control. The
micro:bit will use its built-in tilt sensor — known as an accelerometer — to
tell the servo to swing to the right or the left, which will move the DrawBots
marker.
Chapter 5: Making Robots Playful 169
Make_Simple_Robots_interior_FIN.indd 169Make_Simple_Robots_interior_FIN.indd 169 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