import uasyncio as asyncio
from machine import Pin

# Define the LED pin
led = Pin(12, Pin.OUT)

# Define a coroutine to blink the LED
async def blink_led():
    while True:
        led.value(not led.value())
        await asyncio.sleep(0.5)  # Blink every second

# Define a coroutine to read a sensor (simulated here with a print statement)
async def read_sensor():
    for n in range(5):
        # Simulate reading a sensor value
        print(f"Reading sensor value for the {n}th time...")
        await asyncio.sleep(2)  # Read every 2 seconds
    return 42

# Main coroutine to run both tasks concurrently
async def main():
    # Create tasks for blinking LED and reading sensor
    task1 = asyncio.create_task(blink_led())
    task2 = asyncio.create_task(read_sensor())
    
    # Run tasks concurrently
    #print( await asyncio.gather(task1, task2) )
    done, pending = await asyncio.wait_for([task1, task2], 100_000)
    print(done, pending)

# Run the main coroutine
asyncio.run(main())

BOOTSELLED1239USBRaspberryPiPico©2020RP2-8020/21P64M15.00TTT