The_Official_Raspberry_Pi_-_Beginner’s_Book_Vol1,_2018 (1)

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

THE OFFICIAL RASPBERRY PI BEGINNER’S GUIDE


Joystick control
The Sense HAT’s joystick, found in the bottom-right corner, may be small, but it’s surprisingly
powerful: as well as being able to recognise inputs in four directions – up, down, left, and right



  • it also has a fifth input, accessed by pushing it down from above like a push-button switch.


Joystick control in Scratch
Start a new program in Scratch with the Sense HAT extension loaded. As before, drag a
when clicked Events block onto your script area, then drag a set all pixels to white block
underneath it and use the down arrow to change it into a set all pixels to off block.
In Scratch, the Sense HAT’s joystick maps to the cursor keys on the keyboard: pushing
the joystick up is equivalent to pressing the up arrow key, pushing it down is the same as
pushing the down arrow key, pushing it left the same as the left arrow key, and pushing it
right the same as the right arrow key; pushing the joystick inwards like a push-button switch,
meanwhile, is equivalent to pressing the ENTER key.


CHALLENGE: AUTO-ROTATE
Using what you’ve learned about the LED matrix and
the inertial measurement unit’s sensors, can you write a
program that rotates an image depending on the position
of the Sense HAT?

WARNING!
The Sense HAT joystick should only be used if you’ve fitted the
spacers as described at the start of this chapter. Without the
spacers, pushing down on the joystick can flex the Sense HAT
board and damage both the Sense HAT and the Raspberry Pi’s
GPIO header.

WARNING!
Joystick control is only available on the physical Sense HAT.
When using the Sense HAT Emulator, use the corresponding keys
on your keyboard to simulate joystick presses instead.
Free download pdf