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_timer(tim):
global tono
if tono == 0:
bocina.freq(880)
tono = 1
else:
bocina.freq(440)
tono = 0
def fun_boton(boton):
if boton.value() == 0: # Boton presionado
global tono
bocina.freq(440)
bocina.duty_u16(32767)
tono = 0
tim.init(period=100, mode=Timer.PERIODIC, callback=fun_timer)
else: # Boton se liberó
bocina.duty_u16(0)
tim.deinit()
boton.irq(trigger=Pin.IRQ_FALLING|Pin.IRQ_RISING, handler=fun_boton)
while(True):
pass