From a595f07038b89235c074dfadfd21f523e5f1be32 Mon Sep 17 00:00:00 2001 From: JonOfUs Date: Wed, 21 Aug 2024 14:36:59 +0200 Subject: [PATCH] Fix --- main.py | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/main.py b/main.py index a81724a..b1315ac 100644 --- a/main.py +++ b/main.py @@ -28,6 +28,9 @@ button_pin = 18 toggle_switch_left_pin = 15 toggle_switch_right_pin = 14 +# Cleanup GPIO to avoid "channel already in use" errors +GPIO.cleanup() + GPIO.setmode(GPIO.BCM) GPIO.setup(button_pin, GPIO.IN, pull_up_down=GPIO.PUD_UP) GPIO.setup(toggle_switch_left_pin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) @@ -237,4 +240,25 @@ def button_pressed(channel): animation_NR = (animation_NR + 1) % anzahl_animationen reset_animation = True -GPIO.add_event_detect +GPIO.add_event_detect(button_pin, GPIO.FALLING, callback=button_pressed, bouncetime=200) + +# Ensure GPIO cleanup on exit +import atexit +atexit.register(GPIO.cleanup) + +# Main loop +try: + while True: + if animation_NR == 0: + licht_aus() + elif animation_NR == 1: + fernlicht_an() + elif animation_NR == 2: + rainbow() + elif animation_NR == 3: + police() + elif animation_NR == 4: + kit() + time.sleep(0.1) +except KeyboardInterrupt: + GPIO.cleanup() \ No newline at end of file