from machine import Pin
import time
# 設定LED引腳
led_pins = [22, 2, 9]
leds = [Pin(pin, Pin.OUT) for pin in led_pins]
# 設定LED閃爍頻率
frequencies = [1, 2, 4] # Hz
periods = [1 / freq for freq in frequencies] # 每個LED的週期
while True:
current_time = time.ticks_ms() / 1000 # 獲取當前時間,轉換為秒
for i in range(len(leds)):
# 計算當前時間點LED應該的狀態(利用正弦函數週期特性)
if int(current_time * frequencies[i]) % 2 == 0:
leds[i].value(1) # 開啟LED
else:
leds[i].value(0) # 關閉LED
# 短暫休眠以減少CPU佔用
time.sleep(0.01)