WCC 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

- MicroUSB Cable
- 2 x AAA Batteries

Preparation

  1. Connect the micro:bit to a computer via the microUSB cable.
  2. Go to the MakeCode website on your computer and open a new project.

Download the Code

MakeCode Editor

Click the button below to download the MakeCode editor code for this lesson. The hex file is contained in a zip folder.

Kindly 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

Click the button below to download the Dr Bit solutions  for this challenge. The Dr Bit file is contained within a zip folder.

To download Dr Bit PC software kindly click hear.

To download the Dr Bit app kindly select one of the links below.

Time to Code

As you go through these steps please note that there is an image of the completed code to the right.

  1. Begin by selecting the ‘on shake’ block from the ‘input’ tab.
  2. 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.
  3. In the ‘math’ tab there is a random number block called ‘pick random’. Select this and insert it into the variable.
  4. 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.
  5. Select the ‘pause’ block from the ‘basic’ tab and set it to 2 seconds.
  6. Finally choose the ‘clear screen’ block from the ‘basic’ tab and insert it underneath the ‘pause’ block.
  7. 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 pagePlease 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 in touch with you and as a result aid you along your digital making journey.

Becoming a digital maker subscriber means you will receive exclusive offers and we will keep you up to date with new resources and kits, as well as upcoming events and workshops.

Let us know how your digital education journey is progressing. Send us pictures and videos of your awesome creations and use the hashtag below.

#digitaleducationjourney