import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
led=21
bt=20
GPIO.setup(led,GPIO.OUT)
GPIO.setup(bt,GPIO.IN,pull_up_down=GPIO.PUD_UP)
ledStatus=False
n=1
try:
while True:
time.sleep(0.01)
if(GPIO.input(bt)==GPIO.LOW):
time.sleep(0.03)
if(GPIO.input(bt)==GPIO.HIGH):
print('button pressed',n)
n=n+1
ledStatus=not ledStatus
if ledStatus:
GPIO.output(led,GPIO.HIGH)
else:
GPIO.output(led,GPIO.LOW)
except KeyboardInterrupt:
pass
GPIO.cleanup()
标签:led,python,ledStatus,bt,sleep,time,GPIO
From: https://www.cnblogs.com/fjnhyzCYL/p/17204038.html