Pi/button_led.py

29 lines
605 B
Python
Raw Normal View History

#!/usr/bin/python
from time import sleep
import RPi.GPIO as GPIO
2015-12-31 13:11:37 +00:00
ledPin = 14
buttonPin = 16
def buttonHandler(channel):
GPIO.output(ledPin, not GPIO.input(ledPin))
try:
GPIO.setmode(GPIO.BCM)
2015-12-31 13:11:37 +00:00
GPIO.setup(ledPin, GPIO.OUT)
GPIO.setup(buttonPin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(15, GPIO.IN, pull_up_down=GPIO.PUD_UP)
2015-12-31 13:11:37 +00:00
GPIO.add_event_detect(buttonPin, GPIO.FALLING, callback=buttonHandler, bouncetime=300)
2015-12-31 13:11:37 +00:00
GPIO.output(ledPin, False)
while True:
2015-12-31 13:11:37 +00:00
GPIO.wait_for_edge(15, GPIO.RISING)
except KeyboardInterrupt:
2016-02-01 07:55:27 +00:00
pass
finally:
GPIO.cleanup()