Another example:If button1 and button5 is pressed, after max 10 ms (+ other blocking function calls) it's counted as pressed. Then you have to release both buttons until you can press both again.
If you want to program a two-hand control, you should also check the time between the two buttons.
Then you can change the code a bit:Only if lesser than 500 ms between the active_states will count.
Code:
import timefrom gpiozero import Button####### for testing without hardware ########## remove this if you test on real hardware #from random import uniformfrom threading import Threadfrom gpiozero import Devicefrom gpiozero.pins.mock import MockFactorypin_factory = MockFactory()Device.pin_factory = pin_factorydef test(): """ Set button1 and button5 to pressed, then back to released """ while True: time.sleep(uniform(0, 1)) button1.pin.drive_low() time.sleep(uniform(0, 0.5)) button5.pin.drive_low() time.sleep(uniform(0, 2)) button5.pin.drive_high() time.sleep(uniform(0, 0.5)) button1.pin.drive_high()Thread(target=test).start()##############################################def other_stuff(): """ Placeholder for other actions during runtime """# by default it's using PULL_UPbutton1 = Button(12)button5 = Button(13)count = 0pressed = Falselast_state = button1.value, button5.valuewhile True: # this is you mainloop, don't block it time.sleep(0.01) current_state = button1.value, button5.value if last_state != current_state: last_state = current_state if not pressed and all(current_state): pressed = True count += 1 print(f"Both buttons are pressed. {count=}") elif pressed and not any(current_state): pressed = False print("Both buttons released") # calling other functions may block the loop until they are done other_stuff()If you want to program a two-hand control, you should also check the time between the two buttons.
Then you can change the code a bit:
Code:
if not pressed and all(current_state) and abs(button1._last_changed - button5._last_changed) < 0.5:Statistics: Posted by DeaD_EyE — Tue Mar 25, 2025 1:13 pm