I am new to python pi development. But I have pretty good understand what is happing and happened to turn on LED from the program and from the button.
What I am trying to do
But I am trying to achieve turn on the LED from the button in a specific way. I want to Turn on LED when the button is press without use LOOP and without using wait for wait_for_edge event to occur.
HOW I DID IT
In the Following program, you can see I achieved turn ON LED for 5 sec by Adding the add_event_detect and than wait for that event to happen.
I can also do that by LOOP. I also tried without wait_for_edge and loop DIDN't worked.
HOW I WOULD LIKE IT
I will like to achieve this by only setting up GPIO add_event_detect and callback method without LOOP or wait for edge method and exist the program. Then the program will run in the background.
Code: Select all
import time
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
btn_input = 4;
LED_output = 17;
# GPIO btn_input set up as input.
GPIO.setup(btn_input, GPIO.IN)
GPIO.setup(LED_output, GPIO.OUT)
# handle the button event
def buttonEventHandler (pin):
# turn LED on/off
GPIO.output(LED_output,True)
time.sleep(5)
GPIO.output(LED_output,False)
GPIO.add_event_detect(btn_input, GPIO.RISING, callback=buttonEventHandler)
try:
GPIO.wait_for_edge(btn_input, GPIO.FALLING)
except:
GPIO.cleanup()
Thanks in advance