You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When running the C code for chapter 14 everything works as expected. When running the Python code, the button has a duplicate event after the bouncetime. I was wondering if I was the only person to experience this behavior.
I modified the code as follows (see sections titled ### New code to test duplicate events) and for 10 seconds I see ~33 Skipping button press... output lines (corresponding to the bounce time of 300 and the 10 second ignore).
I've got a RPi 3 B+. Thanks for any insight you might be able to provide.
#!/usr/bin/env python3######################################################################### Filename : Relay.py# Description : Button control Relay and Motor# Author : freenove# modification: 2018/08/02########################################################################importRPi.GPIOasGPIOfromdatetimeimportdatetime, timedeltarelayPin=11# define the relayPinbuttonPin=12# define the buttonPinrelayState=False### New code to test duplicate events:lastEventTime=datetime.now() -timedelta(seconds=10)
### End new code.defsetup():
print ('Program is starting...')
GPIO.setmode(GPIO.BOARD) # Numbers GPIOs by physical locationGPIO.setup(relayPin, GPIO.OUT) # Set relayPin's mode is outputGPIO.setup(buttonPin, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Set buttonPin's mode is input, and pull up to highdefbuttonEvent(channel):
globalrelayState### New code to test duplicate events:globallastEventTimeprint ('buttonEvent GPIO%d'%channel)
iflastEventTime+timedelta(seconds=10) >datetime.now():
print('Skipping button press...')
returnlastEventTime=datetime.now()
### End new code.relayState=notrelayStateifrelayState :
print ('Turn on relay ... ')
else :
print ('Turn off relay ... ')
GPIO.output(relayPin,relayState)
defloop():
#Button detectGPIO.add_event_detect(buttonPin, GPIO.FALLING, callback=buttonEvent, bouncetime=300)
whileTrue:
passdefdestroy():
GPIO.output(relayPin, GPIO.LOW) # relay offGPIO.cleanup() # Release resourceif__name__=='__main__': # Program start from heresetup()
try:
loop()
exceptKeyboardInterrupt: # When 'Ctrl+C' is pressed, the child program destroy() will be executed.destroy()
Output looks like this with a single button push:
Program is starting...
buttonEvent GPIO12
Turn on relay ...
buttonEvent GPIO12
Skipping button press...
buttonEvent GPIO12
Skipping button press...
...
buttonEvent GPIO12
Turn off relay ...
If I unplug the motor, there aren't any duplicate events.
The text was updated successfully, but these errors were encountered:
This is because the motor is a high-power electrical device, the power supply voltage is pulled down at the start of the moment, resulting in GPIO18 pin signal changes, the program misjudged, and then closed the relay. Debounce way with the language of C and Python are not the same, which leads to the differences.
We decided to modify the circuit and separate the control part from the load part to solve this problem.
Hello,
When running the C code for chapter 14 everything works as expected. When running the Python code, the button has a duplicate event after the
bouncetime
. I was wondering if I was the only person to experience this behavior.I modified the code as follows (see sections titled
### New code to test duplicate events
) and for 10 seconds I see ~33Skipping button press...
output lines (corresponding to the bounce time of 300 and the 10 second ignore).I've got a RPi 3 B+. Thanks for any insight you might be able to provide.
Output looks like this with a single button push:
If I unplug the motor, there aren't any duplicate events.
The text was updated successfully, but these errors were encountered: