from machine import Pin
import time
# 設定 LED 引腳
led_pins = [Pin(2, Pin.OUT), Pin(16, Pin.OUT), Pin(19, Pin.OUT)] # LED 引腳
button_pins = [Pin(13, Pin.IN, Pin.PULL_UP), Pin(27, Pin.IN, Pin.PULL_UP), Pin(26, Pin.IN, Pin.PULL_UP)] # 按鈕引腳
def control_leds():
for i in range(3):
if not button_pins[i].value(): # 檢查按鈕是否被按下
led_pins[i].on() # 開啟對應的 LED
if i == 0: # 綠燈
time.sleep(3) # 持續 3 秒
elif i == 1: # 紅燈
time.sleep(1) # 持續 2 秒
elif i == 2: # 黃燈
time.sleep(2) # 持續 1 秒
led_pins[i].off() # 關閉對應的 LED
while True:
control_leds() # 控制 LED
time.sleep(0.1) # 加入一點延遲以減少 CPU 使用
#一種叫做timer,另一個enter off(中斷(?))