from machine import Pin
import time
# 初始化按键(上拉输入)
button = Pin(25, Pin.IN, Pin.PULL_UP)
# 初始化4个LED(输出模式,初始熄灭)
led_pins = [13, 12, 14, 27]
leds = [Pin(pin, Pin.OUT, value=0) for pin in led_pins]
current_led = 0 # 当前点亮的LED索引(从0开始)
def turn_on_single_led(index):
"""仅点亮指定索引的LED,其余熄灭"""
for i in range(len(leds)):
leds[i].value(1 if i == index else 0)
while True:
# 按键按下检测(消抖)
if button.value() == 0:
time.sleep_ms(20)
if button.value() == 0:
# 切换到下一个LED(循环)
current_led = (current_led + 1) % len(leds)
turn_on_single_led(current_led)
# 等待按键松开
while button.value() == 0:
time.sleep_ms(10)
time.sleep_ms(10)