Student Workbook - Unit 2 Algorithms
Student Workbook - Unit 2 Algorithms
UNIT 2: ALGORITHMS
Student workbook
makecode.microbit.org
CONTENTS
Overview........................................................................................................................................................................................................... 2
Unit summary............................................................................................................................................................................................. 2
Learning goals............................................................................................................................................................................................ 2
Lesson A: How computers function........................................................................................................................................................ 3
Lesson B: Code with event handlers....................................................................................................................................................... 5
Lesson C: Fidget cube................................................................................................................................................................................ 13
Glossary of key terms................................................................................................................................................................................ 16
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.
1. The Processor – this is usually a small chip inside the computer, and it’s how the computer processes and
transforms information. Have you heard of the term “CPU”? CPU stands for Central Processing Unit. You
can think of the processor as the Brains of the computer - the faster the processor, the more quickly the
computer can think.
2. The Memory – this is how the computer remembers things. There are two types of memory:
RAM (random access memory) - you can think of this as the computer’s short-term memory, things that
are stored here will disappear when the computer is turned off. Can you think of examples of things
that are stored in our short-term memory? Things that you forget after you go to sleep?
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. Can you think of examples of things that
are stored in our long-term memory? Things we never forget?
3. Inputs – this is how a computer takes in information from the world. In humans, our input comes in
through our senses, such as our ears and eyes. What are some Computer Inputs? Keyboard, Mouse,
Touchscreen, Camera, Microphone, Game Controller, Scanner
4. Outputs – this is how a computer displays or communicates information. As humans, we communicate
information by using our mouths when we talk. What are some examples of communication that don’t
involve talking? Blushing, sign language. What are some examples of Computer outputs? Monitor/Screen,
Headphones/Speakers, Printer
All computers need electricity to power them. There are three ways to power your micro:bit:
Connecting the micro:bit to a computer through the USB port at the top
Connecting 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.
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
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
Experiment with the following options.
1. 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.
2. Help - You can also right-click on any block and select Help to open the reference documentation.
Clear screen
Looking at our pseudocode, we want to make sure to start a program with a clear screen. We can do this by
going to the Basic menu - more and choosing a ‘clear screen’ block.
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.
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 from a list of programs and will let others
know what your program does.
Show LEDs
Now we can use our LED lights to display different images depending on what button the user presses.
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.
Click on 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. Click on 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.
Click on the question mark icon again to close the comment box when you are done.
Click on the question mark icon whenever you want to see your comment again or to edit it.
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.
Project expectations
Follow the design thinking approach and make sure your project meets the required specifications:
Uses at least four different inputs
Uses at least four different outputs
Uses event handlers in a way that is integral to the program
The program compiles and runs as intended and uses meaningful comments
Includes the written Reflection Diary entry (which we’ll talk about after you complete your project)
Assessment 1 2 3 4
elements
Inputs Fewer than two At least two At least three At least four different
different inputs different inputs different inputs 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
different outputs different outputs different outputs outputs are
are successfully are successfully are successfully successfully
implemented. implemented. implemented. implemented.
micro:bit program micro:bit program micro:bit program micro:bit program micro:bit program:
lacks all of the lacks two of the lacks one of the
Uses event
required elements. required elements. required elements.
handlers in a way
that is integral to
the program
Compiles and runs
as intended
Uses meaningful
comments in code
Reflection Diary
Expectations
Write a reflection of about 150–300 words, addressing the following points:
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 addresses all
missing three or missing two of the missing one of the elements.
more of the required elements. required elements.
required elements.