The_Official_Raspberry_Pi_-_Beginner’s_Book_Vol1,_2018 (1)

(singke) #1

142 THE OFFICIAL RASPBERRY PI BEGINNER'S GUIDE


switch in your circuit, is an input rather than an output: drag a set gpio to output high block
from the More Blocks category of the blocks palette under your when clicked block. Click
on the down arrow next to ‘0’ and select the number 2 from the drop-down list, then click on
the down arrow next to ‘output high’ and select ‘input’.

when clicked

setgpio^2 to input

Next, you need to create your traffic light sequence. Drag a forever block into your
program, then fill it with blocks to turn the traffic light LEDs on and off in a pattern. Remember
which GPIO pins have which component attached: when you’re using pin 25 you’re using the
red LED, pin 8 the yellow LED, and pin 7 the green LED.

when clicked

setgpio^2 to input
forever
setgpio^25 to output high
wait 5 secs
setgpio 8 to output high
wait 2 secs
setgpio^25 to output low
setgpio^8 to output low
setgpio^7 to output high
wait^5 secs
setgpio^7 to output low
setgpio^8 to output high
wait^5 secs
setgpio^8 to output low

Click the green flag, and watch your LEDs: first the red will light, then both the red and yellow,
then the green, then the yellow, and finally the sequence repeats with the red light once more.
Free download pdf