So, I absolutely LOVE the Interactive Pixel Pet activity from the Raspberry Pi website, and while I was playing with the Sense Hat the other day, I realised it was possible to imitate it using Scratch. So far I’ve only got it running as an animation, so next step is to get the shake function working as we’ve just figured out how easy it is to use the other sensors on the hat using Scratch.
I had a play and managed to get a very cool dancing cat on my LED matrix – I’m not going to lie, I was super excited and may have run around showing everyone in a slightly excited manner. Fortunately, my colleagues were also excited, although their contributions of dancing ‘poo emojis’ weren’t quite what I had in mind.
Here’s a bit of background on the Sense Hat… for those of you who don’t already know, the Sense Hat was created by the Raspberry Pi Foundation and launched as Astro Pi – a competition to get your pupils’ code into space. It has an 8×8 neopixel array, a mini joystick and a load of amazing sensors like humidity, pressure, gyroscope and accelerometer.
So, the first thing you always need to remember when using Scratch GPIO is that you have to turn on the GPIO server on and, if you’re using a hat, you’ll need to let it know which hat it is by using the command “set AddOn to”.
This is pretty important for anything using the SenseHat and it’s good practice to run it before you go any further in your code as by running it, Scratch will realise you have access to all of the sensors on the hat and allow you to access them through the drop down menu in the blue ‘sensor value’ block.
Firstly you will need to delete the Scratch Cat so that you can draw you own sprite.
In the paint editor, you need to zoom right in as far as you can and select the smallest brush size.
You have four squares in total to draw your image – I’ve shown this here by making the area black (you don’t need to do this, but it can help as ‘black’ represents the neopixel being turned off).
Now you can draw your image – you have exactly 64 pixels to draw with and, as you may have guessed, one pixel on the screen represents one neopixel on the sense hat. By the way, a neopixel is a very bright LED which can be any colour depending on the mix of red green and blue. The lighter your colour, the brighter it will appear on your neopixels so try to avoid dark browns and blues etc.
Next you need to create a second image – you need to use the duplicate command to create a second version of your image.
Then click on the costumes tab to be able to edit it a little bit so that you can make your second sprite slightly different, thus giving the appearance of animation.
Finally, you need some simple Scratch code to get your image moving – I’ve put a couple of broadcast commands in here to clear the SenseHat before you start and to make sure that the background is black (so turned off).
You can experiment if you want by changing the background colour, although this will only make a difference if your sprite is ‘backgroundless’ (but you have to make sure it’s still only 8 pixels/2 squares wide).
<edit> Kirk and Stuart have successfully managed to get shake working and are now celebrating with chocolate cake
Part two of this project can be found here.
All thanks to Albert Hickey for his advice with this project – he is a Scratch and SenseHat guru!!
Watch this space for some more projects using Scratch soon!