All Resources

Micro:bit Virtual Pet

In this activity, students will learn the basics of coding as they explore events, conditional statements, variables, and inputs. They will familiarize themselves with Microsoft MakeCode and learn to program their micro:bits to become interactive virtual pets that require attention to remain happy!

If you do not have micro:bits and are local, sign up for access for access to our loaner kits!

Definitions:

  • Algorithm: a set of instructions that are followed to solve a problem
  • Input: an action the user gives to the computer (eg. pressing the "on" button on your TV remote)
  • Output: an action the computer gives to the user (eg. your TV turning on)
  • Event: a condition that triggers a set of code to happen
  • Conditional statement: an if/then statement that checks if certain conditions are met. Different outcomes result from different conditions being met.
  • Variable: a value that can change, depending on conditions or information passed to the program

Objectives

  • Navigate and use Microsoft MakeCode

  • Create and code a timer using variables

  • Code a conditional statement to check how much time has passed

  • Code different inputs to reset a timer and change the pet’s mood

Materials

  • Micro:bit with battery pack

  • Laptop with microUSB cord
    OR
    Tablet with Micro:bit app installed

Key Questions

  • What was the hardest part about using MakeCode to program code onto your micro:bit?
  • What other features could you try and code into the functionality of this pet?
  • How is this virtual pet similar to a real pet? How is it different?
  • What else might you use conditional statements for when coding?
  • What else could you code a micro:bit to act like?

What To Do

If this is your first time using a micro:bit, we recommend you follow familiarize yourself with the First Steps as outlined on the micro:bit website here

Note: If coding on computers or laptops, we highly recommend using Micro:bit classroom which allows you to lock student’s screens, send them code, and edit their code. More information on using micro:bit classroom here

Coding Introduction

  1. Ask the learners, “What is coding? Why do we code?” (Give learners an opportunity to activate their prior knowledge and share their experiences with coding)
  2. Once they have shared their ideas, offer a simple definition of coding. Eg: “Coding is a language that we [humans] use with computers to get them to do what we ask them to.
  3. Have learners pick up their micro:bit and make observations about what they think it can do. In particular, have students locate the following:
    • Button A and Button B
    • The display LED lights on the front
    • The reset button on the back
    • The speaker on the back
    • The micro:USB plug
    • The plug for the battery pack
  4. Introduce the activity: “Today we are going to code our micro:bits to act like a virtual pet that requires attention to stay happy! We will code our pet in three different phases today: first, we will code our pet to wake up, then we will code our pet to respond to being ignored, then we will code some interactions to keep our pet happy!”

 

Phase one: Waking the pet up

  1. Show students the starting code that we will build from in the activity today:

    *Note: the ON START section will be phase one, FOREVER section will be phase two, and the three pink inputs (BUTTON A, BUTTON B, SHAKE) will be phase three. 
  2. Have learners read through the code in ON START and get them to explain what is happening. Notice that:
    • There is a loop (repeat) with two different icons inside – this creates a little animation similar to how you draw different frames of movement and flip between them to animate pictures
    • There is a sound
    • There is a starting face (you can use that block to make your own designs or use icons of pre-made ones
    • Right away we create a timer that our pet will use to keep track of how long it has been ignored
  3.  Give students time to customize the wake up phase of their pet. They can change the animation, sound, and face or add additional blocks from BASIC and MUSIC as they desire.
    • Remind them not to touch/change any of the other code or to remove the set timer to 0 block in ON START.
  4. Download the code and test it out!

 

Phase two: Coding the pet’s moods when ignored

  1. Review the code currently sitting in FOREVER and ask students how long 1000 ms is? Particularly note that the code is doing the following:
    • Waiting for one second before increasing the timer by 1. Then it will check how many seconds have passed of being ignored.
    • Right now all three timer checks are 0 seconds. Have students select three different amounts of times to check. We recommend 3, 5, and 10 seconds.
    • Each gap after a check needs to be filled with the pet’s response to being ignored for that amount of time.
      • Will it make a sad face? An animation? A different sound? Will it display words? All of those?
      • Maybe it starts out sad after 3 seconds, then gets tired after 5 seconds, then falls asleep after 10 seconds.
  2. Give students time to choose the times and code the responses to being ignored for that long. They will use blocks from BASIC and MUSIC.
  3. Download the code and test it out!

Example code:

 

Phase three: Coding the interactions

  1. The last thing we need to do is code some interactions to keep the pet happy! Notice there are three different inputs that we can use to interact with our pet: button A, button B, shake.
  2. There is one block of code that all of these inputs will need: set timer to 0! (found in VARIABLES) Ask learners why this is needed (because we’re interacting with the pet, it doesn’t need to keep counting how long it’s been ignored, we can reset that count so it doesn’t get sad!)
  3. Give learners a minute to brainstorm what three interactions they will code for their pet and how to best represent them with the code available.
    • Examples: feeding the pet, playing with the pet, taking the pet for a walk, etc.
  4. Give learners time to fill each input with a set timer to 0 and a representation of their interaction using blocks from BASIC and MUSIC
    • They can code messages to display, icons, animations, sounds, etc.
  5. Download and test it out!

About the sticker

Survivors

Artist: Jeff Kulak

Jeff is a senior graphic designer at Science World. His illustration work has been published in the Walrus, The National Post, Reader’s Digest and Chickadee Magazine. He loves to make music, ride bikes, and spend time in the forest.

About the sticker

Egg BB

Artist: Jeff Kulak

Jeff is a senior graphic designer at Science World. His illustration work has been published in the Walrus, The National Post, Reader’s Digest and Chickadee Magazine. He loves to make music, ride bikes, and spend time in the forest.

About the sticker

Comet Crisp

Artist: Jeff Kulak

Jeff is a senior graphic designer at Science World. His illustration work has been published in the Walrus, The National Post, Reader’s Digest and Chickadee Magazine. He loves to make music, ride bikes, and spend time in the forest.

About the sticker

T-Rex and Baby

Artist: Michelle Yong

Michelle is a designer with a focus on creating joyful digital experiences! She enjoys exploring the potential forms that an idea can express itself in and helping then take shape.

About the sticker

Buddy the T-Rex

Artist: Michelle Yong

Michelle is a designer with a focus on creating joyful digital experiences! She enjoys exploring the potential forms that an idea can express itself in and helping then take shape.

About the sticker

Geodessy

Artist: Michelle Yong

Michelle is a designer with a focus on creating joyful digital experiences! She enjoys exploring the potential forms that an idea can express itself in and helping then take shape.

About the sticker

Science Buddies

Artist: Ty Dale

From Canada, Ty was born in Vancouver, British Columbia in 1993. From his chaotic workspace he draws in several different illustrative styles with thick outlines, bold colours and quirky-child like drawings. Ty distils the world around him into its basic geometry, prompting us to look at the mundane in a different way.

About the sticker

Western Dinosaur

Artist: Ty Dale

From Canada, Ty was born in Vancouver, British Columbia in 1993. From his chaotic workspace he draws in several different illustrative styles with thick outlines, bold colours and quirky-child like drawings. Ty distils the world around him into its basic geometry, prompting us to look at the mundane in a different way.

About the sticker

Time-Travel T-Rex

Artist: Ty Dale

From Canada, Ty was born in Vancouver, British Columbia in 1993. From his chaotic workspace he draws in several different illustrative styles with thick outlines, bold colours and quirky-child like drawings. Ty distils the world around him into its basic geometry, prompting us to look at the mundane in a different way.