from machine import Pin
import time
# 定義按鈕和 LED 的 GPIO 腳位
buttons = [Pin(12, Pin.IN, Pin.PULL_UP),
Pin(14, Pin.IN, Pin.PULL_UP),
Pin(32, Pin.IN, Pin.PULL_UP)]
leds = [Pin(13, Pin.OUT),
Pin(15, Pin.OUT),
Pin(33, Pin.OUT)]
try:
while True:
for i in range(len(buttons)):
if not buttons[i].value(): # 檢查按鈕是否被按下
leds[i].value(1) # 點亮對應的 LED
else:
leds[i].value(0) # 熄滅對應的 LED
time.sleep(0.1) # 避免 CPU 佔用過高
except KeyboardInterrupt:
# 在結束程式時確保所有 LED 都熄滅
for led in leds:
led.value(0)