10. First, go to the Input menu (light purple) and scroll down to the bottom
(Figure
O
)
. Then:
Find the “on [loud] sound” block and drag it into the workspace.
Notice that a little image of a microphone lights up on the simulated
board. This means you have turned on the sound sensor.
Next to the light is the number 128, and below that is a scale that
looks like a thermometer. These show what sound level is being
detected by the sound sensor. The numbers go from 0 to 255, so
128 is in the middle. You will use these later to test the sound part of
your program.
11. To make the robot wait before it responds to a voice or sound:
Go to the Basic menu (blue), and scroll down to find a “pause (ms)
[100]” block.
Drag it into the “on [loud] sound” block.
The length of the pause is measured in milliseconds (written as
“ms”). One thousand milliseconds equals 1 second
(Figure
P
)
.
To make the pause longer:
Click on the arrow next to the “100” to open a menu.
O
142
Making Simple Robots, 2nd Edition
Make_Simple_Robots_interior_FIN.indd 142Make_Simple_Robots_interior_FIN.indd 142 4/26/22 1:56 PM4/26/22 1:56 PM
Choose “2 seconds,” or 2,000ms
(Figure
Q
)
. (You can adjust this
later, when you test the program.)
12. To pick a random number that tells the robot which noise to make, you
need a variable. A variable is used in computer programming and in
math equations to stand for a piece of information (such as a number)
that can change at different times. (You already used a kind of variable
block in Scratch, a counter that went up every time it ran.) To create a
variable:
Open the Variables menu (red). Click on “Make a Variable…
(Figure
R
).
Give your variable a name by typing it in the box
(Figure
S
)
. The
name should describe what it stands for, such as “Which Noise?”
Blocks will appear with the name of the variable you have created.
P
R
Q
S
Chapter 4: Making Robots Likable 143
Make_Simple_Robots_interior_FIN.indd 143Make_Simple_Robots_interior_FIN.indd 143 4/26/22 1:56 PM4/26/22 1:56 PM
Drag a “set [Which Noise?] to [0]” block inside the “on [loud] sound”
block. Make a duplicate, and stack it underneath the first
(Figure
T
)
.
Duplicate the pause block, and drag it between the variable blocks.
This will give the program time to pick a random number, then set
the variable back to zero so it doesn’t keep playing over and over.
Go back to the first variable block. In the Math menu (purple), scroll
down to find the “pick random [0] to [10]” block
(Figure
U
)
. It’s oval-
shaped.
Drag the “random” block over the oval space on the variable block.
To make it easier, MakeCode will join the block to the space with
little red dots and a line between them
(Figure
V
)
.
Set the numbers in the random block to 1 and 3. This will let you
have the robot play up to three different noises
(Figure
W
)
. (You can
change the number to add more!)
U
T
144
Making Simple Robots, 2nd Edition
Make_Simple_Robots_interior_FIN.indd 144Make_Simple_Robots_interior_FIN.indd 144 4/26/22 1:56 PM4/26/22 1:56 PM
13. Finally, create one more stack of blocks to help the robot decide which
noise to play. It will use an “if-then-else” block to tell the micro:bit what
to do when the variable is set to 1, 2, or 3. The steps are:
Find the “forever” block that was there when you opened the
project. If you can’t find it, grab a new one from the Basic menu
(blue).
Go to the Logic menu (teal, a kind of dark greenish-blue). From the
Conditionals section, drag an “if [true] then” block into the “forever”
block
(Figure
X
)
. (It doesn’t matter whether you use the block with
or without the “else” mouth — you can always click on the plus and
minus icons on the block to add or delete an “else.”)
V
X
W
Chapter 4: Making Robots Likable 145
Make_Simple_Robots_interior_FIN.indd 145Make_Simple_Robots_interior_FIN.indd 145 4/26/22 1:56 PM4/26/22 1:56 PM
Z
Aa
Y
146
Making Simple Robots, 2nd Edition
Make_Simple_Robots_interior_FIN.indd 146Make_Simple_Robots_interior_FIN.indd 146 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
3.144.230.82