from machine import Pin
import time
# 定义 LED 引脚
red_led = Pin(25, Pin.OUT)
green_led = Pin(26, Pin.OUT)
yellow_led = Pin(27, Pin.OUT)
# 定义按钮引脚
button_red = Pin(14, Pin.IN, Pin.PULL_UP)
button_green = Pin(12, Pin.IN, Pin.PULL_UP)
button_yellow = Pin(13, Pin.IN, Pin.PULL_UP)
while True:
# 检查红色按钮
if button_red.value() == 0: # 按钮按下时值为0
red_led.value(1) # 打开红色 LED
else:
red_led.value(0) # 关闭红色 LED
# 检查绿色按钮
if button_green.value() == 0: # 按钮按下时值为0
green_led.value(1) # 打开绿色 LED
else:
green_led.value(0) # 关闭绿色 LED
# 检查黄色按钮
if button_yellow.value() == 0: # 按钮按下时值为0
yellow_led.value(1) # 打开黄色 LED
else:
yellow_led.value(0) # 关闭黄色 LED
# 稍微延时以减少 CPU 占用
time.sleep(0.01)