from machine import Pin, PWM, Timer
boton = Pin(0, Pin.IN, Pin.PULL_UP)
bocina = PWM(Pin(16), freq = 440, duty_u16 = 0 )
tim = Timer()
tono = 0
def fun_tim(tim):
global tono
if tono == 0:
bocina.freq(880)
tono = 1
else:
bocina.freq(440)
tono = 0
def fun_boton(boton):
global tono
if boton.value() == 0: # Botón presionado
bocina.freq(440)
bocina.duty_u16(32767)
tono = 0
tim.init(period=300, mode=Timer.PERIODIC, callback=fun_tim)
else: # Botón se liberó
bocina.duty_u16(0)
tim.deinit()
boton.irq(trigger=Pin.IRQ_FALLING | Pin.IRQ_RISING, handler=fun_boton)
while True:
pass