Introduction to MicroBlocks Part 2
Introduction to MicroBlocks Part 2
These next steps will take you through what you would do to create
the Mr. Happy Sad program on your own. We will be using a
particular technique that we call, “coding with comments” that we
think you will find to be a nicer way to begin any program that you’ll
create in the future.
2
Comment Blocks
A comment is a block that doesn’t initiate any action; it’s just a place
where you can write some text that is useful to yourself or anyone
else who looks at your program later. In this case, we’re going to use
the comment blocks to describe the overall operation of our program.
● Create the list of the numbers and text that you expect will be
needed in your program. For example, the pin number for a
servo or LED, or the track and folder numbers for an mp3 file.
For text it might be a message that you will display, such as a
question or a prompt.
● Create named variables for each of those numbers or text.
● Create comment blocks that describe what will need to
happen in your program in a certain order, or in response to
inputs or conditions, such as a touch, or number of times
something has happened, or an amount of time that has
passed.
● Add the blocks of actual code to do what the comments have
described.
● Test and “de-bug” (fix places where the code didn’t work as
you expected) your program.
● Enjoy the results!
3
Let’s go through this process now to create your own interactive
program with light, sound, motion and interactivity.
Confirm that the side switch is on “code” (towards the back of the
MakerPort), and that the MakerPort is connected to your laptop with
the USB cable. There should be both a green and orange light
visible inside the case.
Choose New from the file icon menu to erase the Mp3 Example
program.
It will ask you to confirm that it’s ok to erase the current program:
4
If the screen now appears like this:
5
1. Click on Add Library
6
3. In Kits and Boards, choose the MakerPort
5. Click and drag the MakerPort initialize block into position
just below the when started block.
7
7. Here are the important number values that you will need for
Mr. Happy-Sad:
• What touchpoint number to use to start the music? (1)
• What track and folder number to use to play the music?
(1, 76)
• What pin number to turn on and off for the LED? (1)
• What angles of the servo to move to for “happy” and “sad”?
(100, -100)
8
A small window will appear where you can enter the name of
your variable. Let’s start with the first one on our list, the
touch pin number that will be used on the model.
When you click “OK”, the Variables list will now look like this:
The oval “TouchPin” is the variable that you can place inside
other blocks in your program. The top block, “set TouchPin to
0” is what you’ll now drag into your program below the
“MakerPort initialize” block.
9
9. Do that now, drag the “set TouchPin to 0” into your program
and put it below the “MakerPort initialize” block. Change the
0 to 1.
11.Now drag the “set Track to 0” block into your program, and
edit the “0” to be “1”.
10
12.Repeat this process to create variables for Folder (76),
LEDNumber (1), Servo1 (14), HappyAngle (100), and
SadAngle (-100)
13.With the program “when started” set up with the variables that
we expect to use, we can move on to writing the comments.
Let’s start by describing here what we’ll need the program to
do:
• When the touchpoint is touched, turn on the LED and start
the music playing.
• When the touchpoint is released, turn off the LED and stop
the music playing. However, if a touchpoint other than #1
is touched, we’ll let the music keep playing until the end of
the track. In this case, that will be for about a minute.
• When music is playing, and while it is playing, move the
servo to the “happy angle”.
• When the music is not playing, move the servo to the “sad
angle”.
11
14.You’ve just described things to happen “when” in your
program, so let’s drag four “when” blocks into the program
area.
16.In each comment block, write the text from step 13. Note:
pressing shift-return will let you write shorter lines by moving
to the next line when you press shift-return.
12
17.
18.Let’s look at the first when block, “when pin release event”. It’s
going to turn on the LED, which earlier we made a variable
for, named “LEDNumber”. On the MakerPort LED ports, there
are 12 LEDs that can be connected, numbered 1 through 12.
These are also the same as their pin numbers.
13
When you look at the categories on the left, the third one is
“pins”. Drag the “set digital pin (1)” to just below the
comment for the “pin touch event” hat.
This will turn on LED 1 when a touchpoint is touched.
20.Now let’s start the music! Choose the “Mp3 Player” category,
and drag the block “mp3 play track (1) in folder (1)” to just
below the “set digital pin” block.
21.Go back to the Variables category, and drag the Track and
Folder variables into the mp3 play block.
14
That’s it for the first when block! You’ve just programmed your
first MicroBlocks script! Scripts are what each group of blocks
are called.
22.For the 2nd when block, the one for the pin release, repeat
what you did for adding the set pin block, but this time, edit
the green “on” slider to red, which will turn the LED “off”.
Refer back to steps 18 & 19 if you need a reminder about the
process.
23.To stop the music, you can use the “pause” block in the Mp3
player category.
26.Go to the Touch category, and put “last released pin” where
the (3) is, and edit (4) to (1).
16
27.Click and hold on the “set digital pin” block for a moment
before dragging the pair of blocks to inside the “if” block.
->
28.The left side of your program will now look like this:
17
29.Let’s test it! With the MakerPort connected to your laptop
with the USB cable, if it’s not still connected, click on the
Connect icon in MicroBlocks, and choose “Connect (USB)”.
33.If you touch either of the other touchpoints, the music will
continue to play after you release the touch, because the “if”
block only lets the LED off and mp3 pause blocks happen if it
was touchpoint 1 that was released.
18
Congratulations on the first part!
This would be a good time to take a break, and reflect on what you’ve
learned so far.
That’s a lot! In fact, it’s a very good start on writing any program in
the future that you can imagine.
We still have to make the when blocks that will move the servo to set
the face to happy or sad, but for now, take a break, have a cup of tea,
and congratulate yourself on a big day of learning!
19
Print Edition March 3, 2025
The pdf with clickable links of this and all the other booklets is located
in the pdfs folder in the MakerPort USB flash drive, as well as links to
the original Google docs. Move the MakerPort code/files switch to the
“files” position to access them.
20