My Python code is currently looping, and before I want it to re-loop I would really like to be able to detect one of two buttons. Depending what button is pressed, a different part of the code should be executed.
For one button, this would look like this:
while True:
time.sleep(0.2)
GPIO.wait_for_edge(button_1, GPIO.FALLING)
run_button_1_code()
How would I be able to do this for two buttons? I'm thinking something a long the lines of this:
while True:
time.sleep(0.2)
GPIO.wait_for_edge(button_1, button_2, GPIO.FALLING)
if button_1 is pressed:
run_button_1_code()
elif button_2 is pressed:
run_button_2_code()
Or perhaps alternatively:
def button_1():
GPIO.remove_event_detect(button1)
print "doing my code here"
GPIO.add_event_detect(button1, GPIO.BOTH, callback=button_1, bouncetime=800)
def button_2():
GPIO.remove_event_detect(button2)
print "doing my code here"
GPIO.add_event_detect(button2, GPIO.BOTH, callback=button_2, bouncetime=800)
While True:
time.sleep(0.05)
print "waiting for button"
I can't think of any other options.. Please advise!