Student Workbook - Unit 2 Algorithms
Student Workbook - Unit 2 Algorithms
Science
Unit 2:
Algorithms
Student workbook
makecode.microbit.org
Table of Contents
Overview......................................................................................................................................... 3
Unit summary............................................................................................................................... 3
Learning goals.............................................................................................................................. 3
Lesson A: How computers function................................................................................................. 4
Lesson B: Code with event handlers............................................................................................... 6
Lesson C: Fidget cube................................................................................................................... 15
Glossary of key terms................................................................................................................... 20
Unit summary
This unit introduces the four main components that make up a computer and the concept of
input and output as it relates to programming the micro:bit. You will go through two unplugged
activities relating to algorithms and functions that will create a knowledge base for your coding
activity and final project. The coding activity starts with an explanation of pseudocode that leads
to working with events and event handlers to program your micro:bit to make faces. The project
incorporates all the new learning from this unit as you create your own fidget cube that responds
to different inputs.
Learning goals
During this unit, you will:
Understand the four components that make up a computer and their functions.
Understand that the micro:bit takes input, and after processing the input, produces output.
Learn the variety of different types of information the micro:bit takes in as input.
Apply this knowledge by creating a micro:bit program that takes input and produces an
output.
What is a computer?
All computers need electricity to power them. There are three ways to power your micro:bit:
Connect the micro:bit to a computer through the USB port at the top
Connect a battery pack to the battery connector
Through the 3V Pin at the bottom (not the recommended way to power your micro:bit)
On the top left corner, you may notice that your micro:bit has a Bluetooth antenna. This means
your micro:bit can communicate and send information to other micro:bits. We will learn more
about this feature in Unit 10: Radio communication.
In this activity, you will discover how to use the micro:bit buttons as input devices and write code
that will make something happen on the screen as output. You will also learn about pseudocode,
the MakeCode tool, event handlers, and commenting code.
Pseudocode
What do you want your program to do? The first step in writing a computer program is to create
a plan for what you want your program to do. Write out a detailed step-by-step plan for your
program. Your plan should include what type of information your program will receive, how this
input will be processed, what output your program will create, and how the output will be
recorded or presented. Your writing does not need to be written in complete sentences nor
include actual code. This kind of detailed writing is known as pseudocode. Pseudocode is like a
detailed outline or rough draft of your program. Pseudocode is a mix of natural language and
code.
For the program you will write, the pseudocode might look like this:
Now that you have a plan for your program in the form of pseudocode, let’s start creating the
real program in Microsoft MakeCode. Remember, the MakeCode tool is called an IDE (Integrated
Development Environment) and is a software application that contains everything a programmer
needs to create, compile, run, test, and even debug a program.
Event handlers
When you start a new project, there will be two blue blocks, ‘on start’ and ‘forever’ already in the
coding Workspace. These two blocks are event handlers.
In programming, an event is an action done by the user, such as pressing a key or clicking a
mouse button. An event handler is a routine that responds to an event. A programmer can write
code telling the computer what to do when an event occurs.
Tool tips
2. Block descriptions: Hover over any block until a hand icon appears and a small text box will
pop up telling you what that block does. You can try this now with the ‘on start’ and ‘forever’
blocks.
Help: You can also right-click on any block and select Help to open the reference documentation.
5. Drag the ‘clear screen’ block to the coding Workspace. Notice that the block is grayed out. If
you hover over the grayed out block, a pop-up text box will appear letting you know that
since this block is not attached to an event handler block, it will not run.
6. Go ahead and drag the ‘clear screen’ block into the ‘on start’ block. Now the block is no
longer grayed out, indicating that it will run when the event occurs, i.e., the program starts.
You now have a working program running on the micro:bit simulator! As you write your program,
MakeCode will automatically compile and run your code on the simulator. The program doesn’t
do much at this point, but before we make it more interesting, we should name our program and
save it.
7. On the bottom left of the application window, to the right of the Download button is a text box
in which you can name your program. After naming your program, select the save button to
save it.
Important: Whenever you write a significant piece of code or just every few minutes, you should
save your code. Giving your code a meaningful name will help you find it faster and let others
know what your program does.
Now to make your program a bit more interesting by adding two more event handlers.
8. From the Input menu, drag two ‘on button A pressed’ blocks to the coding Workspace. Notice
that the second block is grayed out. This is because, right now, they are the same block, both
“listening” for the same event ‘on button A pressed’.
9. Leave the first block alone for now, and using the drop-down menu within the second block,
change the A to B. Now this block will no longer be grayed out, as it is now listening for a
different event, ‘on button B pressed’.
Now we can use our LED lights to display different images depending on what button the user
presses.
10.From the Basic Toolbox, drag two ‘show leds’ blocks to the coding Workspace. Place one
‘show leds’ block into the ‘on button A pressed’ event handler and the second ‘show leds’
block into the ‘on button B pressed’ event handler.
11.Select the individual little boxes in the ‘show leds’ block that is in the ‘on button A pressed’
event handler to create the image of a happy face. Select the individual little boxes in the
‘show leds’ block that is in the ‘on button B pressed’ event handler to create the image of a
sad face.
Feel free to play around with turning LEDs on or off in the ‘show leds’ blocks until you get the
images you want.
It is good practice to add comments to your code. Comments can be useful in a number of ways.
Comments can help you remember what a certain block of code does and/or why you chose to
program something the way you did. Comments also help others reading your code to
understand these same things.
This will cause a question mark icon to appear to the left of the previous icon.
Select the question mark and a small yellow box will appear into which you can write your
comment.
In JavaScript, you can add a comment by using two forward slashes, then typing your comment.
The two forward slashes tell JavaScript that the following text (on that same line) is a comment.
Cleaning up!
13.Clean up your coding Workspace before you do a final save. What does this mean?
It means that only the code and blocks that you are using in your program are still in the
workspace.
Remove (delete) any other blocks that you may have dragged into the coding workspace
as you were experimenting and building your program.
This project is to make a fidget cube out of the micro:bit and create
a unique output for each of the following inputs:
on button A pressed
on button B pressed
on button A+B pressed
on shake
See if you can combine a maker element similar to what you created in Unit 1 by providing a
holder for the micro:bit that holds it securely when you press one of the buttons.
Project mods
Add more inputs and outputs; use more than four different types of input.
Try to use other types of output (other than LEDs) such as sound!
For an extra challenge, you can try coding this in JavaScript or Python. You can select the
Blocks tab at the top of the MakeCode editor window to switch back and forth between
Blocks, JavaScript, and Python
Project expectations
Follow the design thinking approach and make sure your project meets the required
specifications:
Assessment
1 2 3 4
elements
Inputs Fewer than two At least two At least three At least four
different inputs different inputs different inputs different inputs
are successfully are successfully are successfully are successfully
implemented. implemented. implemented. implemented.
Outputs Fewer than two At least two At least three At least four
different outputs different outputs different outputs different outputs
are successfully are successfully are successfully are successfully
implemented. implemented. implemented. implemented.
What problem did you solve or why did you decide to create this project?
What kind of input and output did you decide to use?
What does your program do? Describe how your program works (what the cause and effect
are).
Include at least one screenshot of your program working.
Publish your MakeCode program and include the URL.
Assessment
1 2 3 4
elements
Diary entry Diary entry is Diary entry is Diary entry is Diary entry
missing three missing two of missing one of addresses all
or more of the the required the required elements.
required elements. elements.
elements.
Event: Something that happens outside a program (like a screen tap or mouse click) that the
program can respond to.
Event handler: Part of a program that runs when a specific event happens (it “handles” the
event). In MakeCode, an event handler block looks like a square with a gap in the middle and
usually starts with the word “on.”
Function: A self-contained set of instructions for performing a specific task within a computer
program. Most objects have multiple functions associated with them.
Input: This is how a computer takes in information from the world. For example: keyboard,
mouse, touchscreen, camera, microphone, game controller, scanner.
Memory: How the computer remembers things. There are two types of memory:
RAM (random access memory): The computer’s short-term memory. Things that are stored
here will disappear when the computer is turned off.
Storage (also referred to as the computer “hard drive”): This is the computer’s long-term
memory, where it can store information even when power is turned off.
Processor: This is a chip inside the computer, and it’s how the computer processes and
transforms information.