# Import required modules
# 導入所需模組
import machine, time
# Initialize a Pin object to interact with GPIO 21, and set it as an output pin
# 初始化一個 Pin 物件來操作 GPIO 21,並將它設為輸出腳位
led = machine.Pin(21, machine.Pin.OUT)
# Initialize another Pin object to interact with GPIO 32,
# and set it as an input pin with a pull-down resistor
# 初始化另一個 Pin 物件來操作 GPIO 32,並將它設為帶下拉電阻的輸入腳位
switch = machine.Pin(32, machine.Pin.IN, machine.Pin.PULL_DOWN)
# Run an infinite loop
# 執行一個無窮迴圈
while True:
# Read the value of the switch. When the button is pressed,
# the pin is connected to the ground and its value goes to 0.
# 讀取開關的值。當按鈕被按下時,該腳位會連接到地,其值會變為 0。
sw = switch.value()
# Set the LED value to the value of the switch
# 將 LED 的電位設為開關的值
led.value(sw)
# Pause the execution of the program for 100 milliseconds
# 暫停程式的執行 100 毫秒
time.sleep_ms(100)