import machine
import time
# Define the GPIO pins for the LED and push button
LED_PIN = 0 # GP0
BUTTON_PIN = 14 # GP14
# Initialize the LED pin as an output and the button pin as an input with a pull-up resistor
led = machine.Pin(LED_PIN, machine.Pin.OUT)
button = machine.Pin(BUTTON_PIN, machine.Pin.IN, machine.Pin.PULL_UP)
# Biến để theo dõi trạng thái của đèn LED và nút bấm
led_state = False
last_button_state = button.value()
while True:
# Đọc trạng thái hiện tại của nút bấm
current_button_state = button.value()
# Kiểm tra xem nút bấm có thay đổi trạng thái từ nhả sang nhấn không
if current_button_state == 0 and last_button_state == 1:
# Đổi trạng thái của đèn LED
led_state = not led_state
if led_state:
led.on()
print("LED ON!")
else:
led.off()
print("LED OFF!")
# Cập nhật trạng thái trước đó của nút bấm
last_button_state = current_button_state
# Thêm một khoảng thời gian trễ để debounce
time.sleep(0.05)