I'm trying to get 2 LEDs flashing at the same time.
The Problem: I can't figure out how to await multiple task bevor continuing the code.
I don't understand the example from Peterhinch well described GitHub documentation "4.1 Awaitable classes".
Code: Select all
##### ASYINC ####
from machine import Pin
import time
import utime
import uasyncio as asyncio
redLed = Pin(5, Pin.OUT)
greenLed = Pin(4, Pin.OUT)
async def blinkRed():
'''
Blink Red LED 10 Times
'''
count = 0
print("Start: Blink Red Led 10 Times")
while count < 10:
redLed.on()
time.sleep(1)
redLed.off()
time.sleep(1)
count += 1
return print("Done: blinking Red LED {} Times".format(count))
async def blinkGreen():
'''
Blink Green LED 15 Times
'''
count = 0
print("Start: Blink Green Led 15 Times")
while count < 15:
greenLed.on()
time.sleep(1)
greenLed.off()
time.sleep(1)
count += 1
return print("Done: blinking Green LED {} Times".format(count))
async def main():
global loob
start = utime.ticks_ms()
task1 = loop.create_task(blinkGreen())
task2 = loop.create_task(blinkRed())
# await asyncio.wait([task1, task2]) // Method from standart asyinc lib
#
# await task1
#
# await yield from task1
end = utime.ticks_ms()
div = (utime.ticks_diff(end,start))/1000
return print("Finished main in {} Seconds".format(div))
# Start
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
loop.close()