Mind the variable creations as they are handled. My questions are the following.
1) Why do we have 2 whiles here? Does the sleep at the end of the first part does really anything significant?
2) Whenever there is no press of the button, logically the press should be 0, but whenever I print
print(press) after the press variable, I always get 1.
3) Following up on question 2, the part
GPIO.output(lightPin, not GPIO.input(buttonPin)) uses opposite logic to check if there is a press turn on the light. Same concern here as question 2.
try: while True: while True: press = GPIO.input(buttonPin) if (press == 1): break GPIO.output(lightPin, not GPIO.input(buttonPin)) print("Led is on.") GPIO.output(lightPin, 0) sleep(sleepTime) finally: GPIO.output(lightPin, False) GPIO.cleanup()