Hardware Reference
In-Depth Information
selects the LED, and value , which determines if it is turned on. So digital_write(0,1)
sets the first LED to have the value 1 (on). This function will be familiar if you have ever pro-
grammed the Arduino.
Computers start counting at 0 as this makes some operations and calculations more efficient.
You should see why as you learn more about computers and programming.
TIP
Flashing a Light On and Off
Next, you're going to write a program that flashes the LED with a timer:
Create a new window in IDLE by going to the File menu and clicking New Window.
Type the following:
from time import sleep
import piface.pfio as pfio
pfio.init()
while(True):
pfio.digital_write(0,1)
sleep(1)
pfio.digital_write(0,1)
sleep(1)
Go to the Run menu and choose Run Module (or press F5).
Click OK when Python displays the message Source Must Be Saved.
Enter a filename and then click Save.
You will see a message saying RESTART, and then Python should run your code. If an
error message appears, go back and correct your code and try running it again.
You've now written the hardware equivalent of Hello World - an LED that flashes. The next
examples will show how to make the Raspberry Pi respond to the world, as you will learn how
to read inputs.
As an extension, you could try flashing a more complex pattern - change the rate the LED
flashes at or try flashing multiple LEDs.
YOUR TURN!
 
Search WWH ::




Custom Search