Microbit
Easy
20 mins
90 points
What you need:
  • Computer/laptop

Microbit Dice

Program a Microbit to be a dice and show a number between 1 and 6.

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.

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