Microbit Inventor Project
Normal
60 mins
255 points
What you need:
  • Computer/laptop
  • Microbit
  • USB Cable

Microbit Pet

In this lesson we will turn our Microbit into a pet with different emotions depending on how we interact with it. We will need to feed and play with it to keep it happy!

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()
})

Join our club 😃

To view the remaining 6 steps and access hundreds of other coding projects please login or create an account.

Copyright Notice
This lesson is copyright of Coding Ireland. 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