Bugnon / minecraft

Minecraft framework for projects with the Raspberry Pi using the GPIO.
5 stars 1 forks source link

The button detection in LEGO_Bricks/Project.py isn't stable. #5

Open MrLasagne4 opened 6 years ago

MrLasagne4 commented 6 years ago

Sometimes, when the button is pressed, the function onButtonPressed handles more than once.

AlbertGuedj commented 6 years ago

Maybe it's because you're pressing the button multiple times when you think you pressed once. You can use Ludovic's Detec_button.py in Magic_Wand it should fix the problem

rasql commented 6 years ago

It is probably because of rebounds (mechanical vibration when opening and closing the switch). To simplify your code you could use a callback function. That is function called whenever a button is pressed. (see tutorial/buttons.py)

## configure the list of input channels
button_channels = [14, 15, 18]

GPIO.setmode(GPIO.BCM)

def button_callback(channel):
    """Print the channel number to the console."""
    print("callback function for button ", channel)

## setup all channels as inputs with a pull-up resistor. When pushing a
## button the state goes from HIGH to LOW (falling edge)
for channel in button_channels:
    GPIO.setup(channel, GPIO.IN, pull_up_down=GPIO.PUD_UP)
    GPIO.add_event_detect(channel, GPIO.FALLING, bouncetime=200)
    GPIO.add_event_callback(channel, button_callback)
Ludblanc commented 6 years ago

Yes my function works well but I am happy to discovered new settings!!!!