Created
April 10, 2020 09:57
-
-
Save oschettler/09f10cafc03d7712f419fc346675053c to your computer and use it in GitHub Desktop.
Switch light on on the press of a button. Turn it off automatically after 5s
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Energy light | |
from gpiozero import LED, Button | |
from time import sleep, time | |
from gpiozero.pins.pigpio import PiGPIOFactory | |
factory = PiGPIOFactory(host='raspberrypi') | |
led = LED(17, pin_factory=factory) | |
btn = Button(4, pin_factory=factory) | |
# Time when the light will switch off again | |
offtime = 0 | |
while True: | |
if btn.is_pressed: | |
print("You pressed me") | |
# Store time to switch light off | |
offtime = time() + 5 | |
if time() < offtime: | |
led.on() | |
else: | |
led.off() | |
sleep(0.1) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment