from machine import Pin
import time
loa = Pin(19, Pin.OUT)
button = Pin(14, Pin.IN, Pin.PULL_UP)
trang_thai_loa = False
ttnutnhan_cuoi = 1
while True:
ttnutnhan_hientai = button.value()
if ttnutnhan_cuoi == 1 and ttnutnhan_hientai == 0:
trang_thai_loa = not trang_thai_loa
time.sleep(0.2) # chống dội phím
ttnutnhan_cuoi = ttnutnhan_hientai
if trang_thai_loa:
loa.value(1)
time.sleep_us(250)
loa.value(0)
time.sleep_us(250)
else:
loa.value(0)
time.sleep(0.01) # nghỉ ngắn khi tắt loa