Toggle
import RPi.GPIO as GPIO
import time
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
LEDPIN = 24
SWITCHPIN = 23
GPIO.setup(LEDPIN, GPIO.OUT)
GPIO.setup(SWITCHPIN, GPIO.IN)
led_state = False
while True:
if GPIO.input(SWITCHPIN):
led_state = not led_state
GPIO.output(LEDPIN, led_state)
time.sleep(0.5) # Debounce delay