Aug-24-2024, 03:04 PM
I need to have one button do different things based on how long it was pressed. It will be adapted to my "washer and dryer timer" box that initiates timers on different user's phones based on different button pressings. All buttons are currently in use, and I can't add another button. Hoping I can add a function to a button by how long it is pressed. I have reduced the code down to a very simple example. For the code below, I just want different LEDs to light based on how long the button was pressed. Short press gets only LED1 on, long press gets only LED2 on.
from gpiozero import Button, LED from signal import pause from time import sleep import time button = Button(16) LED1 = LED(18) LED2 = LED(25) # How do I get the "buttonlongpressed" to happen without # the "buttonpressed" happenening too? # Button is pressed and released within a second def buttonpressed(): print("Short pressed") LED1.on() LED2.off() # Button is pressed for more than 5 seconds and released def buttonlongpressed(): print("Long pressed") LED2.on() LED1.on() button.when_pressed = buttonpressed HERE IS WHAT I NEED = buttonlongpressed pause()