Today we are going to be making our very own
interactive virtual pet that we have coded
ourselves using Python and to do it we are
going to use a Raspberry Pi and this Pi has
a Sense Hat attached to it which we will also
use. A sense hat has an LED matrix and this
is what we are going to use to have our little
pet avatar and it also has a whole bunch of
sensors including movement sensors. So what
we want to do is be able to shake our Raspberry
Pi and sense hat so that it triggers something
to happen.
Let's begin by first connecting the sense
hat to the Raspberry Pi. You ned to make sure
of course that your Raspberry Pi is switched
off before you do anything with the GPIO pins.
Next, you want to make sure you have the most
up to date version of Raspbian the operating
system for Raspberry Pi because this includes
all the Python files that you will need to
be able to control the sense hat. You can
download this from the Raspberry Pi website
by going to raspberrypi.org/downloads. Once
you are happy that you've got everything together
you can plug the power into your Raspberry
Pi and all of the LEDs on the sense hat will
light up and make this beautiful rainbow pattern.
Top tip: you will know if you have the software
you need correctly installed because after
a few seconds those lights will go out.
Once your Raspberry Pi has booted the first
thing you need to do is to open up Python
3 or IDLE 3 by clicking on Menu, Programming
and Python 3. Then you need to create a new
file by clicking File, New File. This is where
we are going to write our Python code. With
any python program the first thing we need
to do is to import all of the libraries and
modules that we need. So in this case, we
are going to need the sense hat library. Start
with from sense hat (with an underscore) import
sense hat. Remember capital letters in Python
are really important. We are also going to
import the time library. WE are then going
to create a variable called sense, and we
are going to use that so we can shorten the
commands used later on.
Time to get creative! You are going to need
to design your avatar. Your little pet! You
can do this in a couple of ways. You could
get some squared paper and you could draw
your avatar. You got to remember it has to
be eight squares by eight squares. Alternatively
you could use something like a spreadsheet
and just colour in cells. Here you can see
my design for a pet. I've created two versions,
one that is slightly different, you might
not be able to tell but I've moved the feet
slightly so that later on when I programme
it, it is going to look like my pet is walking.
Feel free to use as many colours as you like,
but you will need to programme them later
on so it is a good idea perhaps to just select
a few at this point.
Once you are happy with your avatar and with
the colours you have selected, a good idea
would be to go and find out the RGB colour
values for each of your colours. We are going
to need that information in your program.
You can just Google RGB colour values and
there are lots of websites just like this
one that have all the different colours. You
can find the colours that you want and you
can write down the red, green and blue values
for each of those colours.
Back to our Python program, what we need to
do now is translate those colours into our
code. So I am going to use a variable to do
this, so I am going to start with the colour
pink, I'm going to use the variable name p,
then use an equals sign, and then inside brackets
I am going to put the RGB value for that colour.
I am going to go ahead and put all my colours
in under different variable names. WE need
to be careful here of course because if you
want to use the first letter of a colour name,
like blue or black, both of those colour names
start with the same letter so we need to find
a way to differentiate between the two.
Back to your avatar drawing. Here what you
can see I've done is label every single cell
in my grid. I've labelled them with the variables
names I've used for all my colours. So if
we start with the top line, you can see I've
used e all the way across, which stands for
empty. I've got p which stands for pink, g
for green, y for yellow, and w for white.
We need to translate this grid now into our
python code. To do this we are going to use
something called a list. So I need to give
my list a name. My first avatar I'm going
to call pet1. I'm going to use a square bracket
because in Python this is how we start a list.
Then I'm going to write out the top line of
my grid. So I'm going to type e comma then
another e comma and in fact my whole top line
has empty cells in my drawing so I'm just
going to put eight e's separated by commas.
If I go back to my avatar drawing in my grid
I can see the next line starts with a pink
cell and the rest are empties and I am going
to write that line in underneath. Keep going
until you have all of your lines of your avatar
written out. Don;t forget indentation is very
important in Python, you should use four spaces
at the start of the line to indent. To finish
your list use a closed square bracket.
So far in your code you've done a lot of setting
up. IF we were to run this program at the
moment nothing would actually happen on your
sense hat. So what we need to do is to add
a command that is going to make the avatar
appear on the LEDs on the sense hat. So this
is the line that we are going to use. Sense
dot get underscore pixels and inside the brackets
you need to put the name of your list. My
list is called pet1. Then you need to save
your file, give it a name that you'll remember
so I've called mine pixel pet and then you
can click run, and run module to run your
code. What you should see on your sense hat
is your avatar.
Now you've got one of the avatars coded what
you need to do next is underneath the first
list is create your second avatar. Remember
most of my cells were the same I just changed
the feet. Remember though just typing out
your list is not going to make it appear on
your sense hat. So what we need to do is to
adapt our code down the bottom which actually
makes things happen. So underneath my sense
set pixels pet1 I'm going to use time dot
sleep and in brackets I am going to put a
period of time. So 0.5 is half a second. Then
I am going to use the sense dot set pixels
command again this time in brackets thought
I am going to put pet2. This code will start
by showing the first avatar and then after
30 seconds will show the second avatar. We
can test that this works by saving the file
and then clicking on run and run module. You
will see on your sense hat it will start with
your first avatar and after half a second switch
to the second avatar. Of course the problem
now is that it will never go back to the first
one! So I need to change my code so that it
repeats and of course in Python we can do
this by using a loop. In fact we are going
to use a for loop so that we can set the number
of times that it goes around. I'm going to
type for i in range and then inside brackets
I am going to use a number to represent how
many times I want this to repeat. I want it
to repeat ten times. You are going to follow
that with a colon and then you need to indent
the rest of your code inside the loop by four
spaces. We are also going to need to add another
time dot sleep to complete the loop. So then
there is always half a second between every picture
change. After the loop has finished, after
it has gone ten times, actually what I want
the sense hat to do is I want it to go clear.
At the moment it will just stop on one of
the avatar pictures, so I am going to use
this command outside of the loop, so make
sure that your indentation is correct and
you are going to type sense dot clear open
and close bracket. Save and run your file,
you should see your avatar changing. You should
see this happen ten times before the screen
goes clear.
So the first half of my program is complete
now, I've got my avatars working. What I want
to do now is to start using the sensors on
the sense hat to be able to trigger my program,
so the first thing I am going to do is put
my loop inside a function. I can do this by
first defining my function, so I am going
to write def and then I am going to give my
function a name, so I have used walking, open
and close brackets and colon, and then I am
going to put my loop inside the function by
indenting it. We want to use one of the sensors
on the sense hat. We are going to use the
accelerometer and we need to start thinking
about the three axis that this sensor will
detect movement on and so that's these three
axis here. We can write them in our code as
x y and z. Just to think a little more about
perhaps what we have learned in maths.
We need to start by finding out what our the
values that the sense hat currently has. I
am going to use this line here that says sense
dot get underscore accelerometer underscore
raw open close brackets dot values open close
brackets. What this line will do is say hello
sense hat can you tell me at the moment what
are your accelerometer readings and if your
sense hat is sat on a table then those readings
will be zero zero and zero. Because we want
to use the movement as a trigger what we need
to do is create a while loop here. We want
to make sure that nothing happens until the
values of the accelerometer have gone above
the value of two, so if it is sat on the desk
it is going to be zero, if I am shaking it
those values are going to be higher and that
is what is going to trigger my program. Of
course I've stored the program inside the
function called walking so I need to make
sure that I put this inside this new loop
at the bottom. Check that your code looks
right, save it, run it and test if it works.
There you have it, you have made your very
own interactive sense hat pixel pet. So far
all we have done is program the walking command,
if you wanted to turn this into a much more
interactive experience you might want to think
of a way of using one of the other sensors
to feed your pet. If pets are not your thing
then there are lots of other artistic things
you can do with the sense hat. Here I've used
steve from minecraft and programmed him to
change to a creeper. Alternatively why not
take a look at raspberrypi dot org. There
is tons of stuff to help get you started with
a raspberry pi sense hat.
You may have heard that Raspberry Pi are sending
two of these up to the international space
station as part of the Astro Pi project and
the astronauts on board the international
space station are going to use it to run some
experiments and some code written by children
in the UK. Which is very exciting! You can
get your hands on exactly the same device
that they will have on the international space
station just by buying a Raspberry Pi and
a sense hat. All the software that you need
can be downloaded from raspberry pi dot org.
So what are you waiting for? Get coding. My
name is Carrie Anne and remember I'm just
a mouse click away.
