From fa282a98ede4d8b54811520f6588781f27891b04 Mon Sep 17 00:00:00 2001 From: s84716 <s84716@bht-berlin.de> Date: Tue, 10 Dec 2024 16:22:10 +0100 Subject: [PATCH] led switch --- led_switch2.py | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 led_switch2.py diff --git a/led_switch2.py b/led_switch2.py new file mode 100644 index 0000000..66466c9 --- /dev/null +++ b/led_switch2.py @@ -0,0 +1,38 @@ +#!/usr/bin/python3 + +import RPi.GPIO as GPIO +import time, sys + +# use pin numbers +GPIO.setmode(GPIO.BOARD) + +# use pin 12 for output (=GPIO18) +GPIO.setup(12, GPIO.OUT) +ledOn = False +GPIO.output(12, ledOn) + +# use pin 22 for input (=GPIO25) +GPIO.setup(22, GPIO.IN) + +# callback function to switch LED output +def switch_led(pin): + global ledOn + ledOn = not ledOn # switch + # write to pin 12 + GPIO.output(12, ledOn) + return + +# detect the event of rising signal level at pin 22 +GPIO.add_event_detect(22, GPIO.RISING, bouncetime=200) +# in case of an event switch LED output using +#the callback function switch_led(pin) +GPIO.add_event_callback(22, switch_led) + +try: + while True: + time.sleep(1) +# interrupt using <Ctrl>+<c> +except KeyboardInterrupt: + # release used pins and return + GPIO.cleanup() + sys.exit() -- GitLab