The_Official_Raspberry_Pi_-_Beginner’s_Book_Vol1,_2018 (1)

(singke) #1

190 THE OFFICIAL RASPBERRY PI BEGINNER'S GUIDE


5 Figure 7-29: Lighting the pixels in random colours

The sparkler isn’t very interactive, though. To change that, start by dragging a wait 1 secs
block so it’s under the set pixel block but within the forever block. Drag a ● / ● Operators
block over the 1, then type 10 in its second space. Finally, drag a temperature block over the
first space in the divide Operator block.

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
wait temperature / 10 secs

Click the green flag and you’ll notice – unless you live somewhere very cold – that the
sparkler is considerably slower than before. That’s because you’ve created a temperature-
dependent delay: the program now waits the current temperature divided by 10 number of
seconds before each loop. If the temperature in your room is 20°C, the program will wait 2
seconds before looping; if the temperature is 10°C, it’ll wait 1 second; if it’s under 10°C, it’ll
wait less than a second.
If your Sense HAT is reading a negative temperature – below 0°C, the freezing point of water


  • it’ll try to wait less than 0 seconds; because that’s impossible – without inventing time travel,
    anyway – you’ll see the same effect as though it was waiting 0 seconds. Congratulations: you
    can now look at integrating the Sense HAT’s various features into programs of your own!
    For more Sense HAT projects, follow the links in Appendix D, Further reading.

Free download pdf