The_Official_Raspberry_Pi_-_Beginner’s_Book_Vol1,_2018 (1)

(singke) #1
Chapter 7 Physical Computing with the Sense HAT 189

THE OFFICIAL RASPBERRY PI BEGINNER’S GUIDE


Scratch project: Sense HAT Sparkler
Now you know know your way around the Sense HAT, it’s time to put everything you’ve learned
together to build a heat-sensitive sparkler – a device which is at its happiest when it’s cold and
which gradually slows down the hotter it gets.
Start a new Scratch project and bring in the Sense HAT extension from the More Blocks
category. As always, begin with two blocks: when clicked and set all pixels to off ,
remembering you’ll have to change the latter from its default setting.
Start by creating a simple, but artistic, sparkler. Drag a forever block onto the script area,
then fill it with a set pixel 0 0 to R 255 G 255 B 255 block. Rather than using set numbers, fill
in each of the white sections of that block with a pick random 1 to 10 Operators block.
The values 1 to 10 aren’t very useful here, so you need to do some editing. The first two
numbers in the set pixel block are the X and Y coordinates of the pixel on the LED matrix,
which means they should be number between 0 and 7 – so change the first two blocks to read
pick random 0 to 7. The next three blocks are the amount of red, green, and blue to apply to
the LED, and should be a number between 0 and 255 – so change the next three blocks to read
pick random 0 to 255.


when clicked
setallpixelstooff
forever
setpixel pickrandom 0 to 7 ,pickrandom 0 to 7 toRpickrandom 0 to 255 Gpickrandom 0 to 255 B pickrandom 0 to 255

Click the green flag and you’ll see the LEDs on the Sense HAT begin to light up in random
colours (Figure 7-29, overleaf). Congratulations: you’ve made an electronic sparkler!


FINAL CHALLENGE
Can you use what you’ve learned to draw an image to
the screen, then have it rotated in whatever direction the
joystick is pushed? Can you make the middle input switch
between more than one picture?
Free download pdf