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 = Ture
def my_callback(channel):
print("button pressed")
global ledStatus
ledStatus = not ledStatus
if ledStatus:
GPIO.output(led, GPIO.HIGH)
else:
GPIO.output(led, GPIO.LOW)
GPIO.add_event_detect(bt, GPIO.FALLING, callback = my_callback, bouncetime = 200)
try:
while True:
print("I love")
time.sleep(2)
except KeyboardInterrupt:
pass
GPIO.cleanup()
标签:led,python,ledStatus,bt,callback,print,GPIO
From: https://www.cnblogs.com/fjnhyzCYL/p/17204109.html