Button-handling via callback
This commit is contained in:
parent
71b29ed3d5
commit
517877559d
@ -4,23 +4,27 @@ from time import sleep
|
||||
|
||||
import RPi.GPIO as GPIO
|
||||
|
||||
ledPin = 14
|
||||
buttonPin = 16
|
||||
|
||||
def buttonHandler(channel):
|
||||
print "button pushed"
|
||||
GPIO.output(ledPin, not GPIO.input(ledPin))
|
||||
|
||||
try:
|
||||
GPIO.setmode(GPIO.BCM)
|
||||
GPIO.setup(4, GPIO.OUT)
|
||||
GPIO.setup(14, GPIO.IN, pull_up_down=GPIO.PUD_UP)
|
||||
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)
|
||||
|
||||
last_state = True
|
||||
last_led_state = False
|
||||
GPIO.add_event_detect(buttonPin, GPIO.FALLING, callback=buttonHandler, bouncetime=300)
|
||||
|
||||
GPIO.output(ledPin, False)
|
||||
|
||||
while True:
|
||||
input_state = GPIO.input(14)
|
||||
if input_state == False and input_state != last_state:
|
||||
print 'Button pressed'
|
||||
GPIO.output(4, ~last_led_state)
|
||||
last_led_state = ~last_led_state
|
||||
last_state = input_state
|
||||
sleep(0.1)
|
||||
GPIO.wait_for_edge(15, GPIO.RISING)
|
||||
print "blah"
|
||||
except KeyboardInterrupt:
|
||||
GPIO.output(4, False)
|
||||
GPIO.output(ledPin, False)
|
||||
finally:
|
||||
GPIO.cleanup()
|
||||
|
10
led.py
10
led.py
@ -6,17 +6,19 @@ import RPi.GPIO as GPIO
|
||||
|
||||
print 'blah'
|
||||
|
||||
pin = 14
|
||||
|
||||
try:
|
||||
GPIO.setmode(GPIO.BCM)
|
||||
GPIO.setup(4, GPIO.OUT)
|
||||
GPIO.setup(pin, GPIO.OUT)
|
||||
|
||||
while True:
|
||||
GPIO.output(4, 1)
|
||||
GPIO.output(pin, 1)
|
||||
sleep(1)
|
||||
GPIO.output(4, 0)
|
||||
GPIO.output(pin, 0)
|
||||
sleep(1)
|
||||
except KeyboardInterrupt:
|
||||
GPIO.output(4, 0)
|
||||
GPIO.output(pin, 0)
|
||||
finally:
|
||||
GPIO.cleanup()
|
||||
|
||||
|
Reference in New Issue
Block a user