Skip to content

Instantly share code, notes, and snippets.

@yarogniew
Created November 15, 2023 19:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yarogniew/430d5c8d8b7eca833cb04f75e0c08afe to your computer and use it in GitHub Desktop.
Save yarogniew/430d5c8d8b7eca833cb04f75e0c08afe to your computer and use it in GitHub Desktop.
Program w MicroPython używa biblioteki uasyncio, aby wykonywać permanentnie trzy zadania w różnych odstępach czasu.
import uasyncio as asyncio
from machine import Pin
async def task1(led):
while True:
print("Zadanie 1 wykonuje się co 3 sekundy")
led.toggle()
await asyncio.sleep(3)
async def task2(led):
while True:
print("Zadanie 2 wykonuje się co 8 sekund")
led.toggle()
await asyncio.sleep(8)
async def task3(led):
while True:
print("Zadanie 3 wykonuje się co 12 sekund")
# led.toggle()
await asyncio.sleep(12)
async def main():
# Inicjalizacja diody LED na pinie GP25 jako wyjście
led = Pin(25, Pin.OUT)
# Uruchomienie obu zadań niezależnie
task1_handler = asyncio.create_task(task1(led))
task2_handler = asyncio.create_task(task2(led))
task3_handler = asyncio.create_task(task3(led))
# Oczekiwanie na zakończenie obu zadań
await asyncio.gather(task1_handler, task2_handler, task3_handler)
# Utworzenie pętli asyncio
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment