#!/usr/bin/env python
import RPi.GPIO as GPIO
IRTrackingPin = 18
OutLedPin = 16
def setup():
# Set the GPIO pins as numbering
GPIO.setmode(GPIO.BOARD)
# Set the IRTrackingPin mode is input, and (Not detected→) ON→ LOW
GPIO.setup(IRTrackingPin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
# Set the OutLedPin's mode is output
GPIO.setup(OutLedPin, GPIO.OUT)
# Set the OutLedPin high(+3.3V) to off led
GPIO.output(OutLedPin, GPIO.LOW)
def loop():
while True:
if GPIO.input(IRTrackingPin) == GPIO.HIGH:
GPIO.output(OutLedPin, GPIO.HIGH)
def destroy():
# Set the OutLedPin turn HIGH
GPIO.output(OutLedPin, GPIO.HIGH)
# Release resource
GPIO.cleanup()
# The Program will start from here
if __name__ == '__main__':
setup()
try:
loop()
# When control c is pressed child program destroy() will be executed.
except KeyboardInterrupt:
destroy()
|