Roll a Dice Lesson
Overview
Everyone has played a game that involves rolling a dice at some point in their life. Why not create your own dice using the Micro:bit! This activity will explore how the shake function can be used as an input to trigger a random number generator and eventually display a number or image to represent a dice.
Aims
- Use the micro:bit on shake function to simulate a six sided dice roll by showing numbers on the LED matrix.
Kit Required
Micro:bit
Wearable Kit
- Micro USB Cable
- 2 x AAA Batteries
Download the Code
MakeCode Editor
Download the MakeCode editor code for this lesson. The hex file is contained in a zip folder.
Refer to our download the code page for instructions on how to upload your code onto the micro:bit. Please use instructions set A.
This code can also be uploaded into the MakeCode editor.
Dr Bit Code
Download the Dr Bit solutions for this challenge. The Dr Bit file is contained within a zip folder.
Download Dr Bit PC software.
Download the Dr Bit app.
Preparation
- Assemble the Wearable kit with the micro:bit. (Click here to find instructions on how to assemble the Wearable kit).
- Connect the micro:bit to a computer via the microUSB cable.
- Go to the MakeCode website on your computer and open a new project.
Time to Code
As you go through these steps please note that there is an image of the completed code to the right.
- Begin by selecting the ‘on shake’ block from the ‘input’ tab.
- Create a new variable in the ‘variables’ tab. Name this variable ‘random’. Then select the ‘set random’ block from the ‘variables’ tab and attach it into the the ‘on shake’ block.
- In the ‘math’ tab there is a random number block called ‘pick random’. Select this and insert it into the variable.
- To display the the generated number select the ‘show number’ block from the ‘basic tab’. Next select the ‘random’ variable from the ‘variables’ tab and insert it into the show number block.
- Select the ‘pause’ block from the ‘basic’ tab and set it to 2 seconds.
- Finally choose the ‘clear screen’ block from the ‘basic’ tab and insert it underneath the ‘pause’ block.
- The code is now finished! Download your code and upload it onto the micro:bit. The instructions for this can be found on the download the code page. Please use instructions set B.
What Happens
When the micro:bit has been shaken it will randomly generate a number between 1 and 6. This number will be displayed on the LED matrix for 2 seconds before the micro:bit screen clears and the programme is reset.
Taking it Further
- To make the dice more realistic try displaying the randomly generated number as dots instead of a number.
- A standard dice in theory should have the same probability of landing on any number. Try creating a dice that has a higher prodability of landing on an even number than an odd number.
Keep in touch
We would love to keep you up to date with all of our latest news, offers and new products.
If you would like to subscribe to our mailing list please click the button below.
Contact Us
ScienceScope Limited
Downside School
Stratton-on-the-Fosse
Radstock
BA3 4RJ
enquiries@sciencescope.uk