In this lesson, you'll create a digital dice roller using the Micro:bit. This interactive project simulates rolling a traditional six-sided dice. By shaking the Micro:bit (or using the simulator's shake button), it will generate a random number from 1 to 6 and display it as a pattern of dots on the LED display, just like the face of a real dice.
You'll use Python to handle shake detection, random number generation, and image display, building skills in programming logic, loops, and functions. By the end of this lesson, you'll have a fun, working dice roller that you can use or expand upon.
Open the Micro:bit Python editor at python.microbit.org and start a new project.
In this step, we'll import the necessary modules. We need the microbit module for hardware access, including the accelerometer and display, and the random module to generate a random dice roll.
Start by adding the following complete code to your editor:
from microbit import *
import random
Next, we'll create a list to store the dice faces as images. Each face is an Image object representing the dots for numbers 1 through 6 on a dice. Lists are perfect for holding these related items, and we'll use this list to select a random face later.
Update your code to the following complete version:
from microbit import *
import random
dice_faces = [
Image('00000:' '00000:' '00900:' '00000:' '00000'), # 1 - single dot in the center
Image('90000:' '00000:' '00000:' '00000:' '00009'), # 2 - dots in top-left and bottom-right corners
Image('90000:' '00000:' '00900:' '00000:' '00009'), # 3 - dots in top-left, center, and bottom-right
Image('90009:' '00000:' '00000:' '00000:' '90009'), # 4 - dots in top-left, top-right, bottom-left, and bottom-right corners
Image('90009:' '00000:' '00900:' '00000:' '90009'), # 5 - dots in four corners and the center
Image('90009:' '00000:' '90009:' '00000:' '90009') # 6 - three rows of two dots each (left and right positions)
]
display.show(dice_faces[0])
dice_faces
holds six Image
objects.display.show(dice_faces[3])
to show '4'. This practises accessing list elements. Note: Images are strings representing brightness levels (0-9) for each LED in the 5x5 grid.Now, let's detect when the Micro:bit is shaken using the accelerometer. We'll use a loop to continuously check for the 'shake' gesture.
Update your code to this complete version:
from microbit import *
import random
dice_faces = [
Image('00000:' '00000:' '00900:' '00000:' '00000'), # 1
Image('90000:' '00000:' '00000:' '00000:' '00009'), # 2
Image('90000:' '00000:' '00900:' '00000:' '00009'), # 3
Image('90009:' '00000:' '00000:' '00000:' '90009'), # 4
Image('90009:' '00000:' '00900:' '00000:' '90009'), # 5
Image('90009:' '00000:' '90009:' '00000:' '90009') # 6
]
while True:
if accelerometer.was_gesture('shake'):
display.scroll('Rolling!')
sleep(100)
was_gesture
method, which is a control structure for event detection. Test it multiple times.To make it feel like a real dice roll, we'll add an animation using a loop to show a sequence of images that simulate rolling dots.
We'll define a list of animation frames and loop through them when a shake is detected.
Update your code to this complete version:
from microbit import *
import random
dice_faces = [
Image('00000:' '00000:' '00900:' '00000:' '00000'), # 1
Image('90000:' '00000:' '00000:' '00000:' '00009'), # 2
Image('90000:' '00000:' '00900:' '00000:' '00009'), # 3
Image('90009:' '00000:' '00000:' '00000:' '90009'), # 4
Image('90009:' '00000:' '00900:' '00000:' '90009'), # 5
Image('90009:' '00000:' '90009:' '00000:' '90009') # 6
]
animation = [
Image('00900:' '00000:' '00000:' '00000:' '00000'),
Image('00000:' '00900:' '00000:' '00000:' '00000'),
Image('00000:' '00000:' '00900:' '00000:' '00000'),
Image('00000:' '00000:' '00000:' '00900:' '00000'),
Image('00000:' '00000:' '00000:' '00000:' '00900')
]
while True:
if accelerometer.was_gesture('shake'):
for frame in animation:
display.show(frame)
sleep(100)
display.clear()
sleep(100)