0% found this document useful (0 votes)
7 views

Introduction to MicroBlocks Part 2

This document provides a step-by-step guide for creating the Mr. Happy Sad program using the MicroBlocks editor and MakerPort. It emphasizes the technique of 'coding with comments' to facilitate understanding and organization of the code. The guide covers setting up the environment, creating variables, and implementing interactive features such as touchpoints, LEDs, and sound playback.

Uploaded by

hyperrog3810
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Introduction to MicroBlocks Part 2

This document provides a step-by-step guide for creating the Mr. Happy Sad program using the MicroBlocks editor and MakerPort. It emphasizes the technique of 'coding with comments' to facilitate understanding and organization of the code. The guide covers setting up the environment, creating variables, and implementing interactive features such as touchpoints, LEDs, and sound playback.

Uploaded by

hyperrog3810
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

MicroBlocks:

Visual Coding for


the MakerPort

Let’s Learn How to Make This Program!

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.

If you don’t already have the MicroBlocks editor open


(microblocks.fun/run-pilot) and connected to the MakerPort with the
USB cable, do that now.

As mentioned in Part 1, if you haven’t yet made the Mr. Happy-Sad


model using the other instruction booklet, “First Animatronics
Project”, you can test the general operation by attaching an angle
servo, LEDs and 3 touchpoints to the MakerPort like this:

2
Comment Blocks

In the MicroBlocks editor, the comment block in MicroBlocks is found


in the Control category (you’ll need to scroll down), and looks like
this:

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.

Coding with comments consists of approaching the coding for a new


project with these steps:

●​ 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.

It’s always easiest to start a new program with the MakerPort


attached and connected. When it is, the MakerPort libraries will
automatically be added to your new program.​

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:

Then you can skip ahead to Step 4. ​



If the MakerPort wasn’t connected when you chose “New”, just
connect it now, and do Connect while the screen is still empty, and
MicroBlocks will then add the MakerPort blocks.

Alternatively, follow Steps 1-3 to see how to manually add the


MakerPort libraries to a program after you’ve already started it.

5
1.​ Click on Add Library​

2.​ Choose Libraries, then Kits and Boards​

6
3.​ In Kits and Boards, choose the MakerPort​

4.​ MakerPort is now in the categories list and we’re ready to


start your program! ​

Click on Control, and drag the when started hat into the
program work area.​

5.​ Click and drag the MakerPort initialize block into position
just below the when started block.

7

6.​ Now let’s begin that process of “Coding by Comments”, an


important first part of which is making a list of all the variables
that you think you’ll need in your program. They’re not exactly
comments, but they are the easy to read and understand
names that will represent numbers and text in your program.​

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.​ To start making your variables, choose the Variables category,


and click on “Add a variable”.​

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.​

10.​To make the next variable, Track, choose “Add a variable”


again, and create the variable “Track”. An oval with the name
Track will appear down below. To change the “set” block
before you drag it into your program, click on the triangle at
the end of the “TouchPin” name in the block, and choose
“Track”.​

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.​

15.​Below each of those, drag the comment block​

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.​

Now we’re ready to bring in the blocks that


will make these things happen. Let’s start
on the left. Since it says “touched” and
“released”, look at the blocks in the Touch
category to see what might work for this.​



There you will find “pin touch event”; it is just what you need
for the top “when” hat. “pin release event” will be for the
bottom “when” hat. Drag those blocks into the when hats.​

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.​

19.​Now choose the Variables category, and drag the variable


“LEDNumber” into the spot where you see “1”.​


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. ​

24.​Remember that we only want to stop the music if it was


touchpoint 1 that was released? To do that, we will need what
is called a conditional block. A conditional block is kind of
like a “gate” that will only let the actions flow through when a
15
certain condition is true. Go to the Control category, and
right above the when block, you’ll see the “if” block that you
can drag into position right between the comment and the
“set digital pin” block.​

25.​To set the condition of “only if touchpin 1”, choose the


Operators category, and drag the “(3) = (4)” operator block
into the “if” block.​

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)”.​

Choose the MakerPort and then Connect.​

30.​ MicroBlocks will automatically


transfer the program to the MakerPort, and you can see the
progress in the “clock” at the upper-right of the editor window.​

31.​ Once the transfer is complete,


click on the triangle, the “Go” icon, to start and test the
program.​

32.​Touch and hold touchpoint 1 until the music starts, then


release the touch. If you are using the MakerPort by itself,
and not the assembled model, this will be the touchpoint on
the left of the 3 touchpoints just below the Touch port.​

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.

• the code/file switch; accessing resources built-in to the MakerPort


• loading an example program from the MakerPort USB flash drive
• starting a new program
the MakerPort library, the initialize MakerPort library
• variables
• comments
• the when blocks
• pins, touchpins, LED pins
• touch blocks: on touch, on release, last pin released
• how to turn LEDs on and off
• how to play a certain track in a folder (mp3 play)
• conditional blocks: if
• operator blocks: equals
• how to stop music (mp3 pause)
• how to edit and test a program!
• how variables and code can be changed in real-time while running
MicroBlocks (add something to change the folder number in the
code from 76 to 57, or maybe 76 to 1 (the fairy lights music)

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

You might also like