EasyManuals Logo

Freenove Ultimate Starter Kit User Manual

Freenove Ultimate Starter Kit
286 pages
To Next Page IconTo Next Page
To Next Page IconTo Next Page
To Previous Page IconTo Previous Page
To Previous Page IconTo Previous Page
Page #70 background imageLoading...
Page #70 background image
Chapter 2 Button & LED
70
www.freenove.com
support@freenove.com
37
38
39
40
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.

Table of Contents

Questions and Answers:

Question and Answer IconNeed help?

Do you have a question about the Freenove Ultimate Starter Kit and is the answer not in the manual?

Freenove Ultimate Starter Kit Specifications

General IconGeneral
BrandFreenove
ModelUltimate Starter Kit
CategorySingle board computers
LanguageEnglish