from machine import Pin
import asyncio
import time
led1=Pin(13,Pin.OUT)
led2=Pin(12,Pin.OUT)
led3=Pin(14,Pin.OUT)
led4=Pin(27,Pin.OUT)
button1 = Pin(26, Pin.IN, Pin.PULL_UP)
button2 = Pin(25, Pin.IN, Pin.PULL_UP)
async def blink_led(led, interval):
while True:
led.value(not led.value())
await asyncio.sleep_ms(interval)
async def check_button(button):
while True:
if not button.value(): # כפתור נלחץ
print("Button pressed!",button)
await asyncio.sleep_ms(200) # למניעת ריטוט
await asyncio.sleep_ms(50)
async def read_sensor():
"""הדמיה של קריאת חיישן"""
while True:
# הדמיית קריאת חיישן שלוקחת זמן
print("Reading sensor...")
await asyncio.sleep_ms(1000)
print("Sensor value: ", time.ticks_ms() % 100)
async def main():
# יצירת משימות שרצות במקביל
task1 = asyncio.create_task(blink_led(led1, 250))
task2 = asyncio.create_task(blink_led(led2, 500))
task3 = asyncio.create_task(blink_led(led3, 750))
task4 = asyncio.create_task(blink_led(led4, 1000))
task5 = asyncio.create_task(check_button(button1))
task6 = asyncio.create_task(check_button(button2))
task7 = asyncio.create_task(read_sensor())
# הרצת כל המשימות במקביל
await asyncio.gather(task1, task2, task3, task4, task5, task6)
# הפעלת הלולאה האסינכרונית
try:
asyncio.run(main())
except KeyboardInterrupt:
print("Program stopped by user")