树莓派样例
- import RPi.GPIO as GPIO
- import time
- def init():
- GPIO.setwarnings(False)
- GPIO.setmode(GPIO.BCM)
- GPIO.setup(23, GPIO.IN)
- GPIO.setup(16,GPIO.OUT)
- GPIO.output(16, GPIO.LOW)
- pass
- def led():
- GPIO.output(16, GPIO.HIGH)
- time.sleep(0.5)
- print "detct people led turn on"
- def detct():
- for i in range(1, 31):
- if GPIO.input(23) == True:
- print time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))+" Someone is closing!"
- led()
- else:
- GPIO.output(16, GPIO.LOW)
- print time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))+" No one!"
- time.sleep(2)
- time.sleep(2)
- init()
- detct()
- GPIO.output(16, GPIO.LOW)
- GPIO.cleanup()
|