Microbit
Beginner
30 mins
Teacher/Student led
90 points
What you need:
Chromebook/Laptop/PC
Microbit

Microbit Dice

In this step-by-step lesson, you'll create a new Microbit project and programme it to display a random number between 1 and 6 when shaken, emulating a dice. You'll learn how to detect a shake, show a number, and finally, show a random number.
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 - Create a new Microbit project

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

    2 - Detect a shake

    We are going to program the Microbit to show a number between 1 and 6 when you shake it.

    First let's detect a shake, add the following code to your project.

    input.onGesture(Gesture.Shake, function () {
    	
    })

    3 - Show a number

    Next let's show the number 0 when you shake it.

    Add the show number 0  inside the on [shake] block.

    input.onGesture(Gesture.Shake, function () {
        basic.showNumber(0)
    })

    4 - Show a random number

    Now let's show a random number between 1 and 6, just like a dice.

    Put a pick random 1 to 6 inside the  show number 0  block.

    input.onGesture(Gesture.Shake, function () {
        basic.showNumber(randint(1, 6))
    })


    5 - Microbit

    That's it you've completed the project! If you do have a Microbit you can send your code to it and use it as a dice.

    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