import machine
import uasyncio
import utime
import queue
import random
# Settings
led = machine.Pin('LED', machine.Pin.OUT)
async def blink(q):
delay_ms = 0
while True:
if not q.empty():
delay_ms = await q.get()
led.toggle()
await uasyncio.sleep_ms(delay_ms)
# Coroutine: entry point for asyncio program
async def main():
# Queue for passing messages
q = queue.Queue()
# Start coroutine as a task and immediately return
uasyncio.create_task(blink(q))
# Main loop
while True:
await q.put(int(1000*random.random()))
await uasyncio.sleep_ms(1000)
# Start event loop and run entry point coroutine
uasyncio.run(main())