Hello,
after my previous problem was solved so fast ( viewtopic.php?t=374427 ) i have now another kind of problem getting a push button to work:
I've connected a button to GPIO PIN 8 and GROUND to my pi5. In addition, after a long trial and error, I switched a 10K Ohm resistor between pin 8 and 3.3V, because I thought at times that would improve something.
The goal is to automatically monitor the button after system startup, and when it is pressed (pin 8 and ground are connected) a bash script should be executed. I first started also with a bash script for monitoring, but switched over to python very quickly. And this worked so far, but after the bash script was finished, it was started again without pressing the button.
On the way to achieving that the script only starts once per button press, the rather short Python script is now relatively complex. And in addition, I asked an AI for suggestions for improvement and it has implemented the debounce time and a trick to run the script when button is released again and not pressed anymore, but it didn't improve anything...
However i think i stuck here.. Maybe someone can help me out?
after my previous problem was solved so fast ( viewtopic.php?t=374427 ) i have now another kind of problem getting a push button to work:
I've connected a button to GPIO PIN 8 and GROUND to my pi5. In addition, after a long trial and error, I switched a 10K Ohm resistor between pin 8 and 3.3V, because I thought at times that would improve something.
The goal is to automatically monitor the button after system startup, and when it is pressed (pin 8 and ground are connected) a bash script should be executed. I first started also with a bash script for monitoring, but switched over to python very quickly. And this worked so far, but after the bash script was finished, it was started again without pressing the button.
On the way to achieving that the script only starts once per button press, the rather short Python script is now relatively complex. And in addition, I asked an AI for suggestions for improvement and it has implemented the debounce time and a trick to run the script when button is released again and not pressed anymore, but it didn't improve anything...
However i think i stuck here.. Maybe someone can help me out?
Code:
import gpiodimport subprocessimport timeCHIP = 'gpiochip4' LINE = 8DEBOUNCE_TIME = 0.2chip = gpiod.Chip(CHIP)line = chip.get_line(LINE)line.request(consumer='button_monitor', type=gpiod.LINE_REQ_EV_BOTH_EDGES)button_pressed = Falselast_event_time = 0try: while True: event = line.event_read() current_time = time.time() if event.type == gpiod.LineEvent.FALLING_EDGE: button_pressed = True elif event.type == gpiod.LineEvent.RISING_EDGE and button_pressed: if current_time - last_event_time > DEBOUNCE_TIME: subprocess.run(['/opt/backup.sh'], check=True, shell=True) button_pressed = False last_event_time = current_time time.sleep(0.01)except KeyboardInterrupt: print("Beendet durch Benutzer.")line.release()
Statistics: Posted by crabler — Sun Aug 04, 2024 9:38 pm