Interactive Pixel Pet
Using sensors and output devices is a great way to make your computer programs more interactive. The Raspberry Pi Sense HAT contains a whole set of sensors that can be used to detect movement, which will be used in this activity to take a digital pet for a walk. You can watch a video tutorial to accompany this resource here.
Draw your Pixel Pet
- Open a Terminal by clicking on Menu, Accessories and Terminal.
cd RPi_8x8GridDrawand press Enter on the keyboard.
python3 sense_grid.py. This will run an application which you can use to draw your space pet avatar.
Simply select the colour you wish to use from the grid with your mouse pointer, and then select the circle in the grid to change it to that colour.
Alternatively, you may wish to draw your picture out on squared paper with coloured pencils instead like this:
You will need another pet design, preferably one that is very similar to the first, so that we can animate your pet. Here you can see the image is almost identical to the one above, but the feet are in a different place:
Later, when you code your animation, you will create the illusion that the pet is walking.
Label each pixel of your image
- Think of a letter from the alphabet to represent each colour in your pixel pet image. e.g.
wfor white or
If you are using squared paper for your design, you can write the letters on top like this:
If you are using the 8x8GridDraw editor then you can write out your squares on paper, representing each colour with a letter and separating them with a comma. Alternatively, you could type them into a text editor like Leafpad which you can find by clicking on Menu, Accessories and Text Editor.
You'll end up with something that looks like this:
e, e, e, e, e, e, e, e, p, e, e, e, e, e, e, e, e, p, e, e, p, e, p, e, e, p, g, g, p, y, y, e, e, g, g, g, y, w, y, g, e, g, g, g, g, y, y, e, e, g, e, g, e, g, e, e, e, e, e, e, e, e, e, e
You'll notice that we have eight rows and eight columns of letters, each separated by a comma, to make up the LED matrix on the Sense HAT.
Repeat this step for your second pet design so that you end up with two grids of letters.
Can you think of any problems that might arise when only using one letter to label different colours? How might you solve this problem?
Code your pet in Python 3
Now that you have your designs represented as letters in a grid or array, you can start to code them in Python.
- Click on Menu then Programming, followed by Python 3 (IDLE). This will open the Python 3 shell window.
- Next, click on File and New File to open an empty text editor window.
- Save this empty file as
First you will need to import all the modules and libraries you will need for this project in your code by typing:
from sense_hat import SenseHat import time
sense = SenseHat()
Note that capital letters, full stops and commas are very important in Python. Your code might not work if you do not include these.
Next, create a
Variablefor each colour label in your pet design like this:
p = (204, 0, 204) # Pink g = (0, 102, 102) # Dark Green w = (200, 200, 200) # White y = (204, 204, 0) # Yellow e = (0, 0, 0) # Empty
The numbers used here inside the brackets are
Red, Green and Bluevalues. A mixture of these colours make different colours. The higher the number, the more of that colour it will contain. For example,
(255, 0, 0)would make a solid red colour, whereas
(0, 255, 0)would create a vivid green colour.
You can change these numbers in your code to get the colours that you want.
Next, use a
listto store your pixel pet design like this:
pet1 = [ e, e, e, e, e, e, e, e, p, e, e, e, e, e, e, e, e, p, e, e, p, e, p, e, e, p, g, g, p, y, y, e, e, g, g, g, y, w, y, g, e, g, g, g, g, y, y, e, e, g, e, g, e, g, e, e, e, e, e, e, e, e, e, e ]
Here you have created a variable called
pet1and stored a list of labels for each colour by using
[at the start of each letter and
]at the end.
Repeat for the second pixel pet design, using a different variable name like
If you ran your code now nothing would happen, because so far you have only told the program to store information. To make something happen, you will need to write a command to call on that data and display your colours in the right order on the Sense HAT LED matrix. Type this command underneath your lists:
Save your code by pressing
Son the keyboard followed by
Note what happens. Why did only one of your pet designs display? It's because you have only called
pet1in your command.
Add a delay using the
sleepfunction, and then call the second picture using the same command as before like this:
Save and run your code to see your pet.
Animate your pet using a loop
So far, your pixel pet only changes once. To animate it fully, you will need to switch repeatedly between the pictures with a time delay.
You could write the commands out over and over again but it makes more sense to put them into a loop.
Move to the end of your program and locate the
sense.set_pixels(pet1)part. Amend it to look like this:
for i in range(10): sense.set_pixels(pet1) sleep(0.5) sense.set_pixels(pet2) sleep(0.5)
Don't forget to add the extra
sleep(0.5)on the last line and remember to indent the lines after
for i in range(10):. Indenting those lines means that they are inside the
forloop with the
rangefunction will repeat the indented code ten times and then stop.
Save and run your code to watch the animation.
You will notice that after the animation has completed, you are left with the same image still displayed on the LED matrix. There is a great function that you can use that will clear the LEDs. Add this line above your new loop to clear the LEDs when you first run your program:
Create a walking function
function is a piece of code that you can use over and over. As the goal is to trigger the walking animation later on, it makes sense for us to put the animation code into a function that can be called when an action has been sensed by the hardware.
To put your code into a function, you simply need to add this line above your
forloop and indent the lines beneath like this:
def walking(): for i in range(10): sense.set_pixels(pet1) sleep(0.5) sense.set_pixels(pet2) sleep(0.5)
The use of
defhere means that you are defining a function which you have called walking.
Now you need to call the function so at the bottom of your code type:
Shake to trigger action
It's time to use the Sense HAT's movement sensors, in particular its
accelerometer to trigger the walking function to make the project more interactive.
Underneath your walking function, but above the function call line of
x, y, z = sense.get_accelerometer_raw().values() while x<2 and y<2 and z<2: x, y, z = sense.get_accelerometer_raw().values() walking()
The first line will get current movement readings from the Sense HAT on its x, y, and z coordinates. As your Raspberry Pi is presumably sitting still on a desk, those readings will have a very low value.
whileloop is introduced to continually check the accelerometer values, to see if they have changed to above or equal to the value
2. You can help the Sense HAT have an accelerometer reading of above
2by shaking it!
Check your code, especially the indentation against this version here.
- Save your code and run it. Nothing should happen until you shake your Raspberry Pi.
- Do you need to make use of the
sense.clear()function to make your program work the way you want?
- What other sensors could you use to trigger functions that are on the Sense HAT? Can you create more actions in order to look after your space pet, like feeding or petting?
- Could you make a dice or Simon Says game like the ones in the Getting Started with the Sense HAT resource?