Skip to content
Advertisement

Raspberry Pi – Increase Blinking Speed LED

I have two LEDs, blue and green. I want to gradually increase the blinking speed of the green LED while the blue LED is also on at the same time for 5 seconds. Here is my code so far.

def blink_green():
    blue.on()
    for i in range(5):
      green.toggle()
      time.sleep(0.5)
      green.toggle()
      time.sleep(0.5)

To execute this function, this is the code

while True:
  blink_green()

It somehow works but the green LED is only blinking at a constant rate. Any suggestions on how I can make the green LED blink at an increasing rate?

Advertisement

Answer

Keep the duration in a variable and decrease the duration in every loop

def blink_green2():
    red1.on()
    sleep_duration = 0.5
    for i in range(5):
      green2.toggle()
      time.sleep(sleep_duration)
      green2.toggle()
      time.sleep(sleep_duration)
      sleep_duration -= 0.01
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement