Let's write LED blinking code in Python. Connect one external LED in series with a resistor to pin P8_10 like we did in Chapter 3, Blinking External LEDs. Type the following program in Cloud9, save it as blink_external_LED.py
and run. You should be able to see an LED blinking each second:
The code for blink_external_LED.py
is as follows:
#!/usr/bin/python from time import sleep ##Needed to call sleep() import Adafruit_BBIO.GPIO as GPIO led = "P8_10" GPIO.setup(led,GPIO.OUT) while True: GPIO.output(led,GPIO.HIGH) sleep(1) GPIO.output(led,GPIO.LOW) sleep(1)
Now, let's go through the above-mentioned code snippet one step at a time. If you observed, there is no semicolon (;
) at the end of lines in Python. There are no curly braces to indicate statements inside a while
loop. Statements inside a loop are indented. Here is a line-by-line explanation of the code.
We declared that the interpreter of this program in
/usr/bin/python
. Now...