from machine import Pin, PWM, Timer
import time
boton = Pin(0, Pin.IN, Pin.PULL_UP)
bocina = PWM(Pin(16), freq=440, duty_u16=0)
tim = Timer()
def fun_tim(tim):
global tono
if tono == 0:
bocina.freq(880)
tono = 1
else:
bocina.freq(440)
tono = 0
def fun_btn(Pin):
global tono
if boton.value() == 0: # si el boton se presionó
bocina.freq(440)
bocina.duty_u16(32767)
tono = 0
tim.init(period=400, mode=Timer.PERIODIC, callback=fun_tim)
else:
bocina.duty_u16(0)
tim.deinit()
boton.irq(trigger=Pin.IRQ_FALLING | Pin.IRQ_RISING, handler=fun_btn)
while True:
pass
Loading
pi-pico-w
pi-pico-w