I'm working on programming a LED strip lights application that is activated with a momentary push button and if the lights are not turned off by the push button, then they automatically turn off after 30 minutes.
There are automatic timers out there, but this timer only starts after the button is pressed and the button can take it back to a standby state before the 30 minutes have elapsed.
I am good with the Raspberry Pi 3 GPIOs and the relay required, but seeking some guidance on the code
First button press: Lights activate. Have the option to move to the second button press or if the button is not pressed a second time, then the side lights will turn off automatically after 30 minutes.
Second button press: Lights turn off and python is waiting for the first button press (again).
Below is one version that I been working on but due to working through various attempts I'm sure it requires rework. .
The auto-shutoff is required to ensure they aren’t left on for days at time. :-)
It is possible that I need to pull in another module, but I can’t seem to hack my way into figuring out the shutoff timer or where to place it. Thanks.
import RPi.GPIO as GPIO from time import sleep GPIO.setmode(GPIO.BCM) LEDPin = 26 buttonPin = 5 # Setup the pin the LED is connected to GPIO.setup(LEDPin, GPIO.OUT) # Setup the button GPIO.setup(buttonPin, GPIO.IN, pull_up_down = GPIO.PUD_UP) buttonPress = True ledState = False while (1): print("Press it") buttonPress = GPIO.input(buttonPin) if buttonPress == False and ledState == False: GPIO.output(LEDPin, True) print("LED ON") ledState = True sleep(.25) elif buttonPress == False and ledState == True: GPIO.output(LEDPin, True) ledState = False sleep(0.25) sleep(0.15)