Microbit Lesson 123
Microbit Lesson 123
BBC micro:bit
Lesson 1-3
Developed by Microsoft
INTRODUCTION
When we first started teaching computer science, we discovered two important things.
We found that existing curriculum for beginners focused mostly on solving math
problems or constructing geometric shapes and that there was a certain type of student
that signed up for computer science classes and these students were almost always
boys. We wondered whether a different approach to teaching the basics of computer
programming would be more engaging and also attract a larger variety of different types
of students, both boys and girls.
We decided to focus on what knowing how to program allowed you to do and create.
Ultimately all programs are created to solve a problem or serve a purpose. The problem
may be local or global, the purpose may be anything from helping doctors treat patients
to pure entertainment. By starting with interesting problems the students wanted to
solve, they were much more engaged in learning to code. They saw coding skills as an
important part of building creative solutions.
With this approach, we found that not only did we get more girls taking the course, we
also got a more diverse group of boys. Opportunities for collaboration increased, and all
the students got to see where their talents and skills meshed with others’ interests and
experiences, to make a whole that was greater than the sum of its parts.
We are now at the point where a third of the students taking computer science are
girls, and more importantly, students are coming out of the course not only with an
understanding of code, but also knowing how to read through professionally written
code, and take an idea from brainstorming through prototyping to build something that
matters.
Micro:bits may be purchased from these resellers: future study. They will develop powerful
http://microbit.org/resellers(you will need 1 design skills that they can use in future
micro:bit per student for this course). The “Micro:bit projects of all types, whether they are
Go Kit” includes a battery pack and USB cable as designing 3D printed prototypes or creating
well. apps that serve a real world purpose.
Each lesson also contains a micro:bit activity, which Where applicable, we have mapped each of
we informally refer to as a “birdhouse” activity, after the lessons to the Computer Science Teachers
the innumerable wooden birdhouses so many of us Association (CSTA) K-12 Standards, which are US
made in wood shop as a way to master basic skills. nationally recognized standards for computer science
Each lesson’s micro:bit activity is an example that education.
walks students step-by-step through building a
project that demonstrates that lesson’s topic. By the
time students finish the activity, they will have written
code that they can use in a different project of their
own design.
REFERENCES
We have included some additional reference books and materials if you are
interested in learning more about Maker Education, Physical Computing or Design
Thinking in the classroom.
• Invent To Learn
Making, Tinkering, and Engineering in the Classroom by Sylvia Libow Martinez
& Gary Stager
• Launch
Using Design Thinking to Boost Creativity and Bring Out the Maker in Every
Student by John Spencer and AJ Juliani
• The Innovator’s Mindset
Empower Learning, Unleash Talent, and Lead a Culture of Creativity by George
Couros
• The Big Book of Makerspace Projects
Inspiring Makers to Experiment, Create, and Learn by Colleen Graves
If you have feedback for the Microsoft MakeCode team, you can fill our their survey
form here: https://aka.ms/microbitfeedback
This Lesson introduces the Micro:bit as a piece of hardware that has a specific size and weight, and
generally must be supported and incorporated as an essential component of a tangible artifact. Focus on
incorporating the physical Micro:bit into a basic making activity.
Lesson Objectives
…
• Exercise creativity and resourcefulness by coming up with ideas for using simple household materials
’ z
• Test and iterate using different materials and sizes in order to create an optimal design to house the
micro:bit and battery pack
• Learn how to download programs and move them to the Micro:bit file to run on the Micro:bit.
• Use the design thinking process to develop an understanding for a problem or user need.
• “ ”
Introduction
The Micro:bit is a great way to teach the basics of programming and computer science. The Microsoft
MakeCode block-based coding environment is a powerful and intuitive way to make the Micro:bit react to
all sorts of input, and you can introduce fundamental concepts such as iteration, conditional statements,
and variables using MakeCode.
Students often focus primarily on the 5x5 LED screen for providing output. Although this is the most
directly accessible way to see a reaction to some kind of input, there are many more creative possibilities
“ ” ,
creations.
01.Making Page 8
’ I’
common household supplies. Because the micro:bit is so lightweight, and supports so many sensors, it can
be incorporated easily into a physical design as long as students plan ahead for its size and weight. One of
q “W ?”
I ’ , “ ”
We purposely start this course with a lesson on Making and the physical nature of the micro:bit, because it
is important to set the tone for the whole course that this is a class about making, building, crafting and
construction. It helps if you have an art room available where kids can work, or arts and crafts supplies in
your classroom that kids can use to build.
01.Making Page 9
Unplugged: Design Thinking
Objective: To introduce a process of design that starts with talking to one another. Whatever
you build with code should serve a purpose or fill a need. Sometimes what you build will make
the world more beautiful, or help somebody else. Our design process, based on a process called
design thinking, can give students a specific framework for thinking purposefully about design.
Overview: In this activity, students will interview each other about their ideal pet. They should
take notes.
’ ,
create prototypes that get you closer and closer to the best solution.
Materials:
Pairs of students, something to take notes on
Getting started:
Pair students up with each other. One is Student A, the other is Student B. The goal of this
activity is to gather information from their partner that will help them to design a Micro:bit pet
for their partner.
5 minutes: Student A interviews Student B. The goal is to find out what Student B considers to
be their ideal pet. Student A should mostly listen, and ask questions to keep Student B talking
for the entire time. Here are some questions to start with:
q “W ?”
01.Making Page 10
q “W ?”
as possible. Your partner will tell you about his or her ideal pet, but you are really finding out
’ W ,
people. So we need to start with understanding them first.
5 minutes: Student A and Student B review their notes, and circle anything that seems as if it will
be important to understanding how to create the ideal pet for their partner. Circle ideas, advice,
anything that could be helpful when they start building. Then, they should use what they have
discovered about their partner to fill in the blanks:
This definition statement should draw some conclusions about their partner's need based on the
conversation they have had with that person.
5 minutes: Student A and Student B sketch at least 5 ideas of pets that would meet their
partner's needs. Stick figures and diagrams are okay. At this point, quantity is more important
than quality. Students shouldn't limit themselves to real animals; unicorns and mashups are
totally fine!
Make sure students keep their notes and sketches! They will use them in the project for this
lesson.
Examples
01.Making Page 11
01.Making Page 12
01.Making Page 13
Activity: Installing a Program
Overview: Students will create a simple program in Microsoft MakeCode and download it to
their Micro:bit using a USB cable.
For this activity, students will each need a Micro:bit, a micro-USB cable, a computer, and a
battery pack.
microbit-Mi
croPet
Open a browser window to makecode.com, and select the micro:bit code editor
From the top left corner of the screen, select the Projects Menu, and click on Import File.
Select the file that you saved on your computer in the previous step.
01.Making Page 14
Select the file that you saved on your computer in the previous step.
basic.forever(() => {
basic.showIcon(IconNames.Happy)
basic.pause(5000)
basic.showIcon(IconNames.Sad)
basic.pause(5000)
})
MicroPet
01.Making Page 15
Tour of Microsoft MakeCode
• Simulator - on the left side of the screen, you will see a virtual micro:bit that will show what
your program will look like running on a micro:bit. This is helpful for debugging, and
instant feedback on program execution.
• Toolbox - in the middle of the screen, there are a number of different categories, each
containing a number of blocks that can be dragged into the programming workspace on
the right.
• Workspace - on the right side of the screen is the Programming Workspace where you will
create your program. Programs are constructed by snapping blocks together in this area.
The color of the blocks identifies their category. All of the blocks that make up the program
above come from the Basic Toolbox category, which is light blue.
01.Making Page 16
a USB flash drive.
Click the purple Download button in the lower left of the MakeCode screen. This will download
the file to your computer, to the location where your browser is set to save downloads.
To move the program to your micro:bit, drag the downloaded "microbit-xxxx.hex" file to the
MICROBIT drive, as if you were copying a file to a flash drive. The program will copy over, and it
will begin running on the micro:bit immediately.
The micro:bit will hold one program at a time. It is not necessary to delete files off the micro:bit
before you copy another onto the micro:bit; a new file will just replace the old one.
For the next project, your students should attach the battery pack (it takes 2 AAA batteries) to
the micro:bit using the white connector. That way they can build it into their design without
having to connect it to the computer.
01.Making Page 17
01.Making Page 18
Project: Micro:pet
Project
This project is an opportunity for students to create a micro:pet for the partner they interviewed in the
Unplugged activity. They should review their notes and try to summarize what their partner finds appealing in a
pet. Then, they should use whatever materials are available to create a prototype of a pet their partner would
like.
We often ask students to sketch a few designs on paper first, then consult with their partner to see which
aspects of those designs they find most appealing. The purpose of prototyping is to gather more feedback to
“I I , I I B…”
Your design should use whatever materials are available to support the micro:bit so that its face is showing. You
can be creative and decide how to mount the board, and how to decorate your critter.
Reflection
Have students write a reflection of about 150–300 words, addressing the following points:
• Summarize the feedback you got from your partner on your idea. How would you revise your design, if
you were to go back and create another version?
• What was it like to have someone designing a pet for you? Was it a pet you would have enjoyed? Why or
why not? What advice did you give them that might help them redesign?
• What was it like to interview your partner? What was it like to be listened to?
• What was something that was surprising to you about the process of designing the micro:pet?
• Describe a difficult point in the process of designing the micro:pet, and explain how you resolved it.
Rubric
For creative projects such as these, we normally don’t use a qualitative rubric to grade the creativity or the
match with their partner’s needs. We just check to make sure that the micro:pet meets the required
specifications:
Program properly downloaded to micro:bit
01.Making Page 19
• Program properly downloaded to micro:bit
• Micro:bit supported so the face is showing
• Micro:bit can be turned on and off without taking critter apart
• Turned in notes on interview process
• Written reflection (prompt is above)
Micro:Pet Examples
Dog
01.Making Page 20
Pink Piggy
Ladybug
01.Making Page 21
Caterpillar
Fox
01.Making Page 22
Robot
01.Making Page 23
Standards
• 2-A-2-1 Solicit and integrate peer feedback as appropriate to develop or refine a program
• 2-A-6-10 Use an iterative design process (e.g., define the problem, generate ideas, build,
test, and improve solutions) to solve problems, both independently and collaboratively.
01.Making Page 24
Algorithms
This lesson introduces a conceptual framework for thinking of a computing device as something that uses code to
process one or more inputs and send them to an output(s).
Lesson Objectives
…
• 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.
Introduction
What is a micro:bit?
The micro:bit was created in 2015 in the UK by the BBC to teach computer science to students. The BBC gave away a
micro:bit to every Year 7 student in the UK. You can think of a micro:bit as a mini computer.
http://microbit.org
What is a computer?
There are 4 main components that make up any computer:
02.Algorithms Page 25
1. The Processor – this is usually a small chip inside the computer, and it’s how the computer processes and
transforms information. Has anyone 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
○ Storage (also referred to as the “hard drive”) - this is the computer’s long-term memory, where it can store
information even when power is turned off
3. Inputs – this is how a computer takes in information from the world. On 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
All computers need electricity to power them. There are 3 ways to power your micro:bit:
• Through the USB port at the top
By connecting a battery pack to the battery connector
02.Algorithms Page 26
• By 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 the Radio Lesson.
02.Algorithms Page 27
Unplugged: What's Your Function & Crazy Conditionals
Materials
• Pencils
• Paper (or index cards)
‘ ’ , ,
‘ ’
A function machine takes an input, processes the input, and then delivers an output.
The inputs and their outputs are usually recorded in an input output table, where the value of x represents the
input and the value of y represents the output. See example.
02.Algorithms Page 28
input and the value of y represents the output. See example.
A common math problem is to determine what processing is happening to the input that results in the given
output. In the example above, each input is being doubled (multiplied by 2) to produce the corresponding output.
Direct the students how you would like them to record their work.
They can use pencil and paper or index cards.
On paper, they can keep track of inputs and outputs in a table (see example above).
With index cards, Player A can write each input on one side of an index card, hand the card to Player B, who then
writes the corresponding output on the other side of the card.
To begin:
• Player B decides on a mathematical function or bit of processing* that will be done on whatever input she
receives from Player A.
• Player B should write down the function or bit of processing and set it aside, out of sight of Player A.
• Player A then gives Player B a number to process.
• Player B processes the number and returns an output to Player A.
• Player A can then state what function or bit of processing she thinks Player B is using on the input to produce
the given output. One try per round of input/output.
• If Player A states the correct function, Player B confirms that it is correct by showing the previously hidden
function and the players switch roles and start the game over.
• If Player A does not guess correctly, Player A provides another input that Player B processes and provides an
output for.
• The goal is for Player A to figure out what function or bit of processing Player B is using in the fewest number
of rounds of input/output possible.
• After each student has had at least one chance to be the function machine, play more rounds as time permits.
Notes:
• The difficulty level of the possible functions should be determined by the teacher and shared with the
students ahead of playing. Alternately, the teacher can provide function cards that are handed out at random
to be used by the players, rather than the players creating their own.
The player providing the input should not just guess what the function is. She should be able to explain why
02.Algorithms Page 29
• The player providing the input should not just guess what the function is. She should be able to explain why
she thinks her input resulted in the given output.
• ‘ ’
○ Add 8
○ Subtract 6
○ Multiply by 3
○ Divide by 2
• Examples of more difficult functions:
○ Multiply by 2 and then subtract 1
○ Square the input
○ Return 20% of the input
Preparation:
• Print & cut into strips with one conditional on each strip
• Note that some of the same conditionals can be given to multiple students, while other conditionals are to be
given to just one student.
• ‘B GIN’ , RIN ID DOWN
• B ‘B GIN’ ‘ O ’ ,
this is that it is challenging for a student to keep track of a lot of different conditionals, though not so for a
computer! : )
Notes:
• Some of the same conditionals can be given to multiple students, while other conditionals are to be given to
just one student.
• Technically these conditionals are all event handlers because the students are simply waiting for a specific
event to trigger them into action.
• Unless instructed otherwise, students do not speak or make noise during this activity.
Extensions/Variations:
• Add AND, OR, AND/OR statements to the conditionals.
• IF’
• L IF’
• Relate this activity to a system and have the students create the conditionals that would end in a product of
some kind or the completion of some task.
=================================================================
02.Algorithms Page 30
Give these 6 conditionals to multiple students.
• These 6 conditionals may be triggered more than once.
• Walk around the classroom during the activity to trigger some of these conditionals.
IF ‘ ’,
THEN ‘ !’
=================================================================
02.Algorithms Page 31
Activity: Happy Face, Sad Face
In this activity, we 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. We 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 we will write, the pseudocode might look like this:
• Start with a blank screen
• Whenever the user presses button A, display a happy face.
• Whenever the user presses button B, display a sad face.
Microsoft MakeCode
Now that you have a plan for your program, in the form of pseudocode, let's start creating the
real program. In a browser window, open the Microsoft MakeCode for micro:bit tool
(https://makecode.microbit.org). 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.
02.Algorithms Page 32
Event handlers
W , ,‘ ’ ‘ ’
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.
One fun unplugged activity you can do with kids to reinforce the idea of an action that waits for
an event is the Crazy Conditionals activity.
Notes:
• Tooltips - Hover over any block until a hand icon appears and a small text box will pop up
Y ‘ ’ ‘ ’
blocks. Notice that it also shows you the equivalent code in JavaScript.
• Help/Documentation - You can also right-click on any block and select Help to open the
reference documentation.
02.Algorithms Page 33
reference documentation.
• Deleting blocks - Click on the 'forever' block and drag it left to the Toolbox area. You
should see a garbage can icon appear. Let go of the block and it should disappear. You
can drag any block back to the Toolbox area to delete it from the coding workspace. You
can also remove a block from your coding window by selecting the block and then
pressing the "delete" key on your keyboard (or command-X on a mac).
Looking at our pseudocode, we want to make sure to start a program with a clear screen.
• D ‘ ’ W
N ‘ ’ I ‘ ’ ,
box will appear letting you know that since this block is not attached to an event handler
block, it will not run.
• G ‘ ’ ‘ ’ N
longer grayed out, indicating that it will run when the event, the program starts, occurs.
basic.clearScreen()
02.Algorithms Page 34
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, press 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 from a list of
programs and will let others know what your program does.
• Leave the first block alone for now, and using the drop-down menu within the second
, ‘ ’ ‘B’ N ,
,‘ B ’
input.onButtonPressed(Button.A, () => {
})
input.onButtonPressed(Button.B, () => {
})
Show LEDs
Now we can use our LED lights to display different images depending on what button the user
presses.
• F B , ‘ ’
• ‘ ’ ‘ ’
‘ ’ ‘ B ’
input.onButtonPressed(Button.A, () => {
basic.showLeds(`
. . . . .
. . . . .
02.Algorithms Page 35
. . . . .
. . . . .
. . . . .
. . . . .
`)
})
input.onButtonPressed(Button.B, () => {
basic.showLeds(`
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
`)
})
• ‘ ’ ‘
’
• ‘ ’ ‘ B
’
input.onButtonPressed(Button.A, () => {
basic.showLeds(`
. . . . .
. # . # .
. . . . .
# . . . #
. # # # .
`)
})
input.onButtonPressed(Button.B, () => {
basic.showLeds(`
. . . . .
. # . # .
. . . . .
. # # # .
# . . . #
`)
})
02.Algorithms Page 36
Commenting your code
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.
• Click on the question mark and a small yellow box will appear into which you can write
your comment.
• 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.
Notes
• When you right-click on the icon that appears before the words on a block, notice that
there are other options available to you that allow you to duplicate and delete blocks, as
well as get help. Feel free to explore and use these as you code.
02.Algorithms Page 37
well as get help. Feel free to explore and use these as you code.
• 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.
// Display a happy face when button A is pressed.
Cleaning up!
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.
HappySadFace
02.Algorithms Page 38
02.Algorithms Page 39
Project: Fidget Cube
A fidget cube is a little cube with something different that you can manipulate on each surface.
, , , “ ” ,
pull, press, and play with it. In this project, students are challenged to turn the micro:bit into
“ ”
Discussion questions
• Do any of your students fidget?
• What kinds of things do they fidget with? Spinning pens, fidget spinners, rings, coins?
• There are many different versions of fidget cubes available now. Do any students have
any?
• Have they seen them before?
• What are the types of fidget activities?
• If students could add or modify features of the fidget cube, what would they choose to
do?
• What would make the ultimate fidget cube?
Remind students that a computing device has a number of inputs, and a number of outputs. The
code that we write processes input by telling the micro:bit what to do when various events
occur.
Project
Make a fidget cube out of the micro:bit, 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 Lesson 1 by providing a
holder for the micro:bit that holds it securely when you press one of the buttons.
02.Algorithms Page 40
Sample Fidget Cube designs
Project Mod
• Add more inputs and more outputs - use more than 4 different types of input. Try to use
other types of output (other than LEDs) such as sound!
Assessment
4 3 2 1
Inputs At least 4 different At least 3 At least 2 Fewer than 2
inputs are different inputs different inputs different inputs are
successfully are successfully are successfully successfully
implemented implemented implemented implemented
Outputs At least 4 different At least 3 At least 2 Fewer than 2
outputs are different outputs different outputs different outputs
successfully are successfully are successfully are successfully
implemented implemented implemented implemented
Micro:bit Micro:bit program: Micro:bit program Micro:bit program Micro:bit program
program • uses event lacks 1 of the lacks 2 of the lacks all or of the
handlers in a way required elements required elements required elements
that is integral to
the program
• compiles and runs
as intended,
• includes
meaningful
comments
Collaboration Reflection piece Reflection piece Reflection piece Reflection piece
reflection includes: lacks 1 of the lacks 2 of the lacks 3 of the
• brainstorming required elements required elements required elements
ideas
02.Algorithms Page 41
ideas
• construction
• programming
• beta testing
02.Algorithms Page 42
Standards
02.Algorithms Page 43
Variables
This lesson introduces the use of variables to store data or the results of mathematical
operations. Students will practice giving variables unique and meaningful names. And we will
introduce the basic mathematical operations for adding subtracting, multiplying, and dividing
variables.
Lesson Objectives
…
• Understand what variables are and why and when to use them in a program.
• Learn how to create a variable, set the variable to an initial value, and change the value of
the variable within a micro:bit program.
• Learn how to create meaningful and understandable variable names.
• Understand that a variable holds one value at a time.
• Understand that when you update or change the value held by a variable, the new value
replaces the previous value.
• Learn how to use the basic mathematical blocks for adding, subtracting, multiplying, and
dividing variables.
• Apply the above knowledge and skills to create a unique program that uses variables as
an integral part of the program.
Introduction
Computer programs process information. Some of the information that is input, stored, and
used in a computer program has a value that is constant, meaning it does not change
throughout the course of the program. An example of a constant ‘ ’ ‘ ’
has one value that never changes. Other pieces of information have values that vary or change
during the running of a program. Programmers create variables to hold the value of
information that may change. In a game program, a variable may be created to hold the
’ , !
game.
Ask the students to think of some pieces of information in their daily life that are constants and
03.Variables Page 44
Ask the students to think of some pieces of information in their daily life that are constants and
others that are variables.
• W ’
day (constants)?
• What pieces of information have values that do change during the course of a single day
(variables)
Constants and variables can be numbers and/or text.
Examples
I …
• , , ’ , ’
• Variables: The temperature/weather, the current time, the current class, whether they are
…
Variables hold a specific type of information. The micro:bit's variables can keep track of
numbers, strings, booleans, and sprites. The first time you use a variable, its type is assigned to
match whatever it is holding. From that point forward, you can only change the value of that
variable to another value of that same type.
• A number variable could hold numerical data such as the year, the temperature, or the
degree of acceleration.
• A string variable holds a string of alphanumeric characters such as a person's name, a
password, or the day of the week.
• A boolean variable has only two values: true or false. You might have certain things that
happen only when the variable called gameOver is false, for example.
• A sprite is a special variable that represents a single dot on the screen and holds two
separate values for the row and column the dot is currently in.
03.Variables Page 45
Unplugged: Keeping Score
To experience creating and working with variables, have students pair up and play Rock Paper
Scissors.
Students will keep track of how many times each player wins as well as the number of times the
players tie.
Play: Have students play Rock Paper Scissors for about a minute. When done, ask the students
‘ ’
Play again: Tell students they will now start over and play again for another minute. When
, ‘ ’
Ask some students to share how they kept track of player scores.
, ’
, ‘ ’
made a separate place for recording ties.
03.Variables Page 46
Sample Score-keeping sheet
Ask the students what parts of the score sheet represent constants, values that do not change
’
Ask the students what parts of the score sheet represent variables, values that do change
’
03.Variables Page 47
Activity: Scorekeeper
This Micro:bit activity guides the students to create a program with three variables that will keep score for their Rock
Paper Scissors game.
Tell the students that they will be creating a program that will act as a scorekeeper for their next Rock Paper Scissors
game. They will need to create variables for the parts of scorekeeping that change over the course of a gaming
session. What are those variables?
• The number of times the first player wins
• The number of times the second player wins
• the number of times the players tie
Creating and naming variables: Lead the students to create meaningful names for their variables.
• What would be a unique and clear name for the variable that will keep track of the number of times Player A
wins?
• ‘ W’, ’, ‘ B ’, ‘ B ’, ‘ W ’
• Discuss why (or why not) different suggestions make clear what value the variable will hold. In general, variable
names should clearly describe what type of information they hold.
In MakeCode, from the Variables menu, make and name these three variables: PlayerAWins, PlayerBWins, PlayersTie
03.Variables Page 48
let PlayerAWins = 0
let PlayerBWins = 0
let PlayersTie = 0
We already initialized these variables and now need to code to update the values at each round of the game.
• Each time the scorekeeper presses button A to record a win for Player A, we want to add 1 to the current value of
‘ W ’
• Each time the scorekeeper presses button B, to record a win for Player B, we want to add 1 to the current value
‘ BW ’
• Each time the scorekeeper presses both button A and button B at the same time to record a tie, we want to add
1 to the current value of the variable PlayersTie
F I , ‘ ’
L ‘ ’ - ‘B’ ‘ +B’
third block.
F V , ‘ ’
Place one change block into each of the Button Pressed blocks.
Choose the appropriate variable from the pull down menus in the change blocks.
03.Variables Page 49
input.onButtonPressed(Button.A, () => {
PlayerAWins += 1
})
input.onButtonPressed(Button.B, () => {
PlayerBWins += 1
})
input.onButtonPressed(Button.AB, () => {
PlayersTie += 1
})
User feedback
Whenever the scorekeeper presses button A, button B, or both buttons together, we will give the user visual feedback
acknowledging that the user pressed a button. We can do this by coding our program to display:
• ‘ ’ ,
• ‘B’ ‘B’ B,
• ‘ ’ B
W ‘ ’, ‘B’, ‘ ’ ‘ ’ ‘ ’
I , ‘ ’
03.Variables Page 50
input.onButtonPressed(Button.A, () => {
PlayerAWins += 1
basic.showLeds(`
. # # # .
. # . # .
. # # # .
. # . # .
. # . # .
`)
basic.clearScreen()
})
input.onButtonPressed(Button.B, () => {
PlayerBWins += 1
basic.showLeds(`
. # # . .
. # . # .
. # # # .
. # . # .
. # # . .
`)
basic.clearScreen()
})
input.onButtonPressed(Button.AB, () => {
PlayersTie += 1
basic.showLeds(`
. # # # .
. . # . .
. . # . .
. . # . .
. . # . .
`)
basic.clearScreen()
})
03.Variables Page 51
N ‘ ’ ‘ ’, ‘B’, ‘ ’
What do you think would happen if we did not clear the screen? Try it.
let PlayersTie = 0
let PlayerBWins = 0
let PlayerAWins = 0
input.onButtonPressed(Button.A, () => {
PlayerAWins += 1
basic.showLeds(`
. # # # .
. # . # .
. # # # .
. # . # .
. # . # .
`)
basic.clearScreen()
})
input.onButtonPressed(Button.B, () => {
PlayerBWins += 1
basic.showLeds(`
. # # . .
. # . # .
03.Variables Page 52
. # . # .
. # # # .
. # . # .
. # # . .
`)
basic.clearScreen()
})
input.onButtonPressed(Button.AB, () => {
PlayersTie += 1
basic.showLeds(`
. # # # .
. . # . .
. . # . .
. . # . .
. . # . .
`)
basic.clearScreen()
})
input.onGesture(Gesture.Shake, () => {
basic.showString("Wins:")
basic.showLeds(`
. # # # .
. # . # .
. # # # .
. # . # .
. # . # .
`)
basic.showNumber(PlayerAWins)
basic.pause(1000)
basic.showLeds(`
. # # . .
. # . # .
. # # # .
. # . # .
. # # . .
`)
basic.showNumber(PlayerBWins)
basic.pause(1000)
basic.showString("Ties:")
basic.showNumber(PlayersTie)
basic.pause(1000)
basic.clearScreen()
})
PlayerAWins = 0
PlayerBWins = 0
PlayersTie = 0
Scorekeeper
03.Variables Page 53
Try it out!
Download the Scorekeeper program to the micro:bit, and have the students play one last round of Rock Paper Scissors
using their micro:bits to act as the Scorekeeper!
’ , ‘ ’
this, we can add the values stored in the variables we created to keep track of how many times each player won and
how many times they tied.
In order to do this, we can add the code to our program under the 'on shake' event handler .
• First, display a string to show the player that the following sum represents the total number of rounds played.
• Our program will add the values stored in the variables 'PlayerAWins', 'PlayerBWins', and 'PlayersTie' and then
display the sum of this mathematical operation.
• The blocks for the mathematical operations adding, subtracting, multiplying, and dividing are listed in the Math
section of the Toolbox.
Note: Even though there are 4 blocks shown for these 4 operations, you can access any of the four operations
from any of the four blocks, and you can also access the exponent operation from these blocks.
03.Variables Page 54
• Replace the default values of zero with the names of the variables we want to add together.
Notice that because we are adding three variables together we need a second math block. First we add the
values for 'PlayerAWins' and 'PlayerBWins', then add 'PlayersTie'.
input.onGesture(Gesture.Shake, () => {
basic.showString("Total rounds played:")
basic.showNumber(PlayerAWins + PlayerBWins + PlayersTie)
• Save, download, and try the program again to make sure that it runs correctly and displays the correct numbers
for each variable.
Remember that the micro:bit is a device that processes input and displays it as output in some way. By storing values
in variables, you can perform mathematical operations on that data that provides you with useful information.
What other math operations could provide valuable information from the values stored in these variables?
Examples:
’
03.Variables Page 55
• ’
• Calculate a display the number of tied games as a percentage of all rounds played.
03.Variables Page 56
Project: Everything Counts
This is an assignment for students to come up with a Micro:bit program that counts something.
Their program should keep track of input by storing values in variables, and provide output in some visual and useful way.
Students should also perform mathematical operations on the variables to give useful output.
Input
Remind the students of all the different inputs available to them through the Micro:bit.
Project Ideas
’
In baseball during an at-bat, umpires must keep track of how many pitches have been thrown to each batter. Use Button A to record the
number of balls (up to 4) and the number of strikes (up to 3).
Extra Mod: Create a way to reset both variables to zero, create a way to see the number of balls and strikes on the screen at the same time.
Shake counter
Using the 'On Shake' block, you can detect when the micro:bit has been shaken and increment a variable accordingly. Try attaching the
micro:bit to a lacrosse stick and keep track of how many times you have successfully thrown the ball up in the air and caught it.
Extra Mod: Make the micro:bit create a sound of increasing pitch every time you successfully catch the ball.
Pedometer
See if you can count your steps while running or doing other physical activities carrying the micro:bit. Where is it best mounted?
D ’
Calculator
Create an adding machine. Use Button A to increment the first number, and Button B to increment the second number. Then, use Shake or
Buttons A + B to add the two numbers and display their sum.
Extra Mod: Find a way to select and perform other math operations.
03.Variables Page 57
Homemade Top with Micro:bit Revolution Counter
Process
In any design project, it's important to start by understanding the problem. You can begin this activity by interviewing people around you
who might have encountered the problem you are trying to solve. For example, if you are designing a wallet, ask your friends how they store
03.Variables Page 58
who might have encountered the problem you are trying to solve. For example, if you are designing a wallet, ask your friends how they store
their money, credit cards, identification, etc. What are some challenges with their current system? What do they like about it? What else do
they use their wallets for?
If you are designing something else, think about how you might find out more information about your problem through interviewing or
observing people using current solutions.
Then start brainstorming. Sketch out a variety of different ideas. Remember that it's okay if the ideas seem far-out or impractical. Some of the
best products come out of seemingly crazy ideas that can ultimately be worked into the design of something useful. What kind of holder can
you design to hold the Micro:bit securely? How will it be used in the real world, as part of a physical design?
Use the simulator to do your programming, and test out a number of different ideas. What is the easiest way to keep track of data? If you are
designing for the accelerometer, try to see what different values are generated through different actions (you can display the value the
accelerometer is currently reading using the 'Show Number' block; clear the screen afterward so you can see the reading).
basic.forever(() => {
basic.showNumber(input.acceleration(Dimension.X))
basic.showLeds(`
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
`)
})
Reflection
Have students write a reflection of about 150–300 words, addressing the following points:
• What was the problem you were trying to solve with this project?
• What were the Variables that you used to keep track of information?
• What mathematical operations did you perform on your variables? What information did you provide?
• Describe what the physical component of your Micro:bit project was (e.g., an armband, a wallet, a holder, etc.)
• How well did your prototype work? What were you happy with? What would you change?
• What was something that was surprising to you about the process of creating this project?
• Describe a difficult point in the process of designing this project, and explain how you resolved it.
Assessment
4 3 2 1
Variables At least 3 different variables At least 2 variables are At least 1 variable is No variables are
are implemented in a implemented in a meaningful implemented in a meaningful implemented.
meaningful way way way
Variable names All variable names are The majority of variable names A minority of variable names None of the variable
unique and clearly describe are unique and clearly describe are unique and clearly describe names clearly describe
what information values the what information values the what information values the what information values
variables hold. variables hold. variables hold. the variables hold.
Mathematical Uses a mathematical Uses a mathematical operation Uses a mathematical operation No mathematical
operations operation on at least two on at least one variable in a incorrectly or not in a way that operations are used.
variables in a way that is way that is integral to the is integral to the program
integral to the program program
Micro:bit Micro:bit program: Micro:bit program lacks 1 of Micro:bit program lacks 2 of Micro:bit program lacks 3
03.Variables Page 59
Micro:bit Micro:bit program: Micro:bit program lacks 1 of Micro:bit program lacks 2 of Micro:bit program lacks 3
program • Uses variables in a way that the required elements the required elements or more of the required
is integral to the program, elements
• Uses mathematical
operations to add, subtract,
multiply, and/or divide
variables,
• Compiles and runs as
intended,
• Meaningful comments in
code
Collaboration Reflection piece addresses Reflection piece lacks 1 of the Reflection piece lacks 2 of the Reflection piece
reflection all prompts. required elements. required elements. lacks 3 of the required
elements.
03.Variables Page 60
Standards
• CL.L2-03 Collaborate with peers, experts, and others using collaborative practices such as
pair programming, working in project teams, and participating in group active learning
activities
• 2-A-5-7 Create variables that represent different types of data and manipulate their values.
03.Variables Page 61