from machine import Pin, Timer
count = 0
led = Pin(2, Pin.OUT)
# Hàm xử lý bật/tắt LED
def ledStatus(timer): # bắt buộc phải có đối số 'timer' khi dùng làm callback
if led.value() == 1:
led.off()
else:
led.on()
# Hàm in thông báo, sử dụng biến toàn cục
def PrintMSG(timer):
global count
count += 1
print("Timer1 được gọi lần(PERIODIC):", count)
# Khởi tạo timer0 - nhấp nháy LED mỗi 1 giây
timer0 = Timer(0)
timer0.init(period=1000, mode=Timer.PERIODIC, callback=ledStatus)
# Khởi tạo timer1 - gọi một lần sau 2 giây
timer1 = Timer(1)
timer1.init(period=2000, mode=Timer.ONE_SHOT, callback=PrintMSG)