Start by creating a new project. Visit the Microbit coding website at https://makecode.microbit.org/ and click on 'New Project'. Give the project a suitable name, such as 'Digital Dice'.
Go to the Makecode.com Microbit website using the link below and click on the 'New Project' button underneath the 'My Projects' heading.
Install the micro:bit app on your iPad or tablet.
Open the app, tap 'Create code' and then create a new project.
We will need a variable to store what number we roll. Create a variable called roll and we will use this in the next step.
In the Variables toolbox, create a new variable by clicking the 'Make a Variable' button.
Once you click this button a box will appear asking what you want to call your variable. Give it a name that reminds you what you will be using it for. For example, if you wanted to keep track of your score in a game, you would create a variable called 'score'.
In this step, you will code the Microbit to generate a random number when it's shaken. This simulates the action of shaking a dice to get a number. Add the following code:
input.onGesture(Gesture.Shake, function () { let roll = Math.randomRange(1, 6) })
This code uses an input event tied to the 'Shake' gesture, and generates a random number between 1 and 6, storing it in the variable 'roll'.
Next, you will code the Microbit to display the number that was generated from the dice roll. Add the following code:
input.onGesture(Gesture.Shake, function () { let roll = Math.randomRange(1, 6) basic.showNumber(roll) })
This code extends the previous one by using the 'showNumber' function to display the 'roll' variable on the Microbit's LED matrix.
Now, it's time to test your digital dice! If you have a physical Microbit, you can download the code onto it. This step is optional. To download, click the 'Download' button in the MakeCode editor and follow the instructions. If you don't have a Microbit, use the simulator on the website to test your code. Shake the Microbit (or click 'shake' in the simulator) and see if a random number displays on the LED matrix.
Give your project a name and send it to your Microbit by following these steps: