Microbit
Advanced
60 mins
Teacher/Student led
255 points
What you need:
Chromebook/Laptop/PC
Microbit

Microbit Pet

In this step-by-step lesson, you'll transform your Microbit into a lifelike pet using emoji icons and sounds. You'll program it to react to different actions, such as becoming sad when shaken or giggling when tickled. You'll also create functions for it to express hunger, boredom, and happiness. Finally, you'll test your code in a simulator and download it onto your Microbit.
Learning Goals Learning Outcomes Teacher Notes Lesson Files

Live Class Feed

This is a live feed of the latest activity by your students on this lesson. It will update in real-time as they work on the lesson.
Load previous activity

    1 - Introduction

    To make our Microbits into pets we will use the different emoji icons and sounds to make them seem lifelike. 

    Our pets will start off happy and we are going to program the following:

    • They will be sad when we shake them.
    • They will giggle when we tickle them (touch the logo on the front).
    • They will go asleep when we flip the Microbit so it faces down.
    • After a random amount of time they will get hungry and ask for food.
    • After a random amount of time they will get bored and ask you to play.

    2 - Create a new Microbit project

    Go to the https://makecode.microbit.org/ website and create a new project.

    3 - Create the 'happy' function

    There will be a few times when we need to run some code to make the Microbit pet appear happy. Rather than repeat these code blocks over and over, we will create a 'happy' function with the code blocks and then call that 'happy' function whenever we want to run those code blocks.

    Create a new function called 'happy' and then add the following code blocks inside it. Then Next call the happy function when we start the Microbit by adding it into the on start block.

    function happy () {
        basic.showIcon(IconNames.Happy)
        soundExpression.happy.play()
    }
    
    happy()
    

    4 - Make it sad

    If you shake your Microbit pet then it will become sad. Add the following code to detect the shake and make it appear sad.

    input.onGesture(Gesture.Shake, function () {
        basic.showIcon(IconNames.Sad)
        soundExpression.sad.play()
    })

    5 - Make it giggle

    The yellow logo on the front of the Microbits is a sensor that can detect when it is touched. We are going to use this to detect when we 'tickle' the Microbit pet. Add the following code to detect when you touch the logo to make it giggle with a laughing face and then call the 'happy' function.

    function happy () {
        basic.showIcon(IconNames.Happy)
        soundExpression.happy.play()
    }
    input.onLogoEvent(TouchButtonEvent.Touched, function () {
        basic.showLeds(`
            . # . # .
            . # . # .
            . . . . .
            # # # # #
            . # # # .
            `)
        soundExpression.giggle.playUntilDone()
        happy()
    })

    Unlock the Full Learning Experience

    Get ready to embark on an incredible learning journey! Get access to this lesson and hundreds more in our Digital Skills Curriculum.

    Copyright Notice
    This lesson is copyright of DigitalSkills.org. Unauthorised use, copying or distribution is not allowed.
    🍪 Our website uses cookies to make your browsing experience better. By using our website you agree to our use of cookies. Learn more