from machine import Pin
import time
# LED 腳位設定
led_pins = [27, 22, 17]
leds = [Pin(pin, Pin.OUT) for pin in led_pins]
# 按鈕 GPIO 26,使用內部下拉電阻
button = Pin(26, Pin.IN, Pin.PULL_DOWN)
# 變數
debounce_time = 0
index = 0
last_press_time = None # 記錄最後一次按下時間
def reset():
global index, last_press_time
for led in leds:
led.value(0)
index = 0
last_press_time = None
print("已重設")
print("系統啟動,等待按鈕觸發...")
while True:
now = time.ticks_ms()
# 按鈕按下(防彈跳)
if button.value() and time.ticks_diff(now, debounce_time) > 300:
debounce_time = now
last_press_time = now
print("按鈕按下")
if index < len(leds):
leds[index].value(1)
index += 1
else:
reset()
# 若超過 3 秒未再按下 → 重設
if last_press_time and time.ticks_diff(now, last_press_time) > 3000:
reset()
time.sleep(0.05)