I've connected red and green leds via GPIO and I can successfully switch them on/off using RPi.GPIO
- now I'd like to:
- start an led blinking on/off every 1 second
- have the script continue its execution
- stop the led from blinking on another signal
A similar example is the project Raspberry Pi E-mail Notifier Using LEDs, except in that case the leds are lit solidly, and the script just waits until the next check:
if newmails > NEWMAIL_OFFSET:
GPIO.output(GREEN_LED, True)
GPIO.output(RED_LED, False)
else:
GPIO.output(GREEN_LED, False)
GPIO.output(RED_LED, True)
time.sleep(MAIL_CHECK_FREQ)
Is it possible to do something like this pseudo-code?
cont = True
while cont:
check for mail
if new mail:
flashLed(green)
else:
flashLed(red)
do other stuff while the relevant light blinks (not just time.sleep)
def flashLed(colour):
stop blinking the other colour
start blinking this colour
return to the calling program
For this to work I imagine that the flashLed
function would need to start the relevant led flashing, then return execution to the main script body.
Is this possible using Python? Is there a better approach?