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.
JavaScript
x
8
1
def blink_green():
2
blue.on()
3
for i in range(5):
4
green.toggle()
5
time.sleep(0.5)
6
green.toggle()
7
time.sleep(0.5)
8
To execute this function, this is the code
JavaScript
1
3
1
while True:
2
blink_green()
3
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
JavaScript
1
10
10
1
def blink_green2():
2
red1.on()
3
sleep_duration = 0.5
4
for i in range(5):
5
green2.toggle()
6
time.sleep(sleep_duration)
7
green2.toggle()
8
time.sleep(sleep_duration)
9
sleep_duration -= 0.01
10