I have some code which turns an led on when I hold the button. But only when I am pressing it. Could somebody help me so when I press the button then the light stays on and if I press it again the led turns off?
Here is my code -
#button.py import RPi.GPIO as GPIO from time import sleep GPIO.setmode(GPIO.BCM) sleepTime = .1 #GPIO Pin of the conponent lightPin = 4 buttonPin = 17 GPIO.setup(lightPin, GPIO.OUT) GPIO.setup(buttonPin, GPIO.IN, pull_up_down=GPIO.PUD_UP) GPIO.output(lightPin, False) try: while True: GPIO.output(lightPin, not GPIO.input(buttonPin)) sleep(.1) finally: GPIO.output(lightPin, False) GPIO.cleanup()