Making a timer reset on a light

Viewed 29

I'm very new to code and want the led.off portion to be extendable. Basically, if I hit the button again before the 10 seconds run out. So if the button is hit before lighting up, there is another 10 second wait, starting at press 2.

from time import time, sleep
from signal import pause

led = LED(18) # or whatever pin you've got it on
button = Button(23) # again, adjust as necessary

def light_off_for_10_sec():
    led.off()
    sleep(10)
    led.on()

button.when_pressed = light_off_for_10_sec

The 10 second pause before lighting up works great, but here is what I have tried to use to stretch the led.off period.

def stretched(values):
    
    when_pressed = 10
    for value in values:
        if value:
            sleep(5)
            pressed = time()
        yield time() - pressed <= 20
        
led.source = stretched(button.pressed)
pause()```


0 Answers
Related