loop()
e xcept KeyboardInterrupt: # When 'Ctrl+C' is pressed, the subprogram destroy() will
be executed.
destroy()
RPi.GPIO provides us with a simple and effective function to eliminate the jitter, that is
GPIO.add_event_detect(). It uses callback function. Once it detect that the buttonPin has a specified action
FALLING, execute the specified function buttonEvent(). In the function buttonEvent, each time the ledState is
reversed, the state of the LED will be updated.
def buttonEvent(channel):
g lobal ledState
p rint 'buttonEvent GPIO%d'%channel
ledState = not ledState
if ledState :
print ('Turn on LED ... ')
e lse :
print ('Turn off LED ... ')
GPIO. output(ledPin,ledState)
def loop():
#Button detect
GPIO. add_event_detect(buttonPin,GPIO.FALLING,callback = buttonEvent,bouncetime=300)
w hile Tr ue:
pass
Of course, you can also use the same programming idea of C code above to achieve this target.
GPIO.add_event_detect(channel, GPIO.RISING, callback=my_callback, bouncetime=200)
This is an event detection function. The first parameter specifies the IO port to be detected. The second
parameter specifies the action to be detected. The third parameter specified a function name, the function
will be executed when the specified action is detected. And the fourth parameter is used to set the jitter
time.