In this project you will use coloured pixels on the Sense HAT to display a countdown timer.
You will be writing code in the Python programming language, which you may have learnt in the Python module.
Step 1: Text countdown
First let’s countdown from 5 to 0 by displaying numbers using the pixels.
Open the Countdown Timer Starter Trinket: jumpto.cc/timer-go.
The code to set up the Sense HAT has been included for you.
First you’re going to count up to 5 because that’s easier. Add the highlighted code to the bottom of your script:
sense.show_letter()displays a single letter on the Sense HAT. It doesn’t allow numbers so you have to use
strto change the number.
sleep(1)waits one second before carrying on.
range(1, 6)returns the numbers 1 to 5. You don’t have to count in ones though:
- range(1, 10, 2) would count up in twos giving 1, 3, 5, 7, and 9.
- range(5, 0, -1) counts down by taking away -1 giving 5, 4, 3, 2, 1
Change the range in your code so that it counts down to 0:
The number doesn’t have to be white, the Sense HAT can display lots of colours. It uses RGB colours (red, green and blue).
Try using green:
Challenge: Another colour
Can you change the colour to one you like?
Here’s another example that uses the colour red:
Try experimenting with the R, G and B values (from 0 to 255.) What colour is
[255, 0, 255]?
You can also look up the RGB values for a colour using jumpto.cc/colours.
Step 2: Creating a dot timer
Another way to create a timer is by turning pixels from green to red.
Open the Dot Timer Starter Trinket: jumpto.cc/dot-timer-go.
Add a variable X to use to turn pixels off - it has no red, green or blue:
Add a variable called
sfor the number of seconds you want to count.
You can give the Sense HAT a list of 64 (8 x 8) colours to display starting from the top left and working down a row at a time.
Let’s create a list of colours by creating a green dot for each second we want to count, and setting the rest of the 64 pixels to off. The
timervariable contains the list of colours to display and starts off empty:
Now let’s run the countdown by turning a dot red every second:
And how about flashing the display at the end, by turning the pixels on and off:
Challenge: Timer games
Can you create a timer for a game or challenge. Will your timer need to count up or down, can you change the colours?
Use your timer to challenge a friend. One of you should watch the timer while the other completes the challenge.
Use one of these ideas or come up with your own:
Can you recite the alphabet in 5 seconds? What about backwards?
How many times can you type Code Club in 10 seconds?
Gather together a few classroom items such as erasers and pencils. You get 20 seconds to try and memorize them all. Then you close your eyes while your partner removes an object. Can you identify the missing object in 10 seconds?