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