from machine import Pin, PWM
from time import sleep
# Configurar los pines para los botones y el LED
boton1 = Pin(14, Pin.IN)
boton2 = Pin(15, Pin.IN)
led = Pin(12, Pin.OUT)
buzzer = PWM(Pin(4), freq=440, duty=0) # Pin 4 para el buzzer
# Definir una función para emitir sonido
def sonido(freq, duracion):
buzzer.freq(freq)
buzzer.duty(512)
sleep(duracion)
buzzer.duty(0) # Apagar el sonido después de la duración
# Iniciar el ciclo infinito
try:
while True:
if boton1.value() == 1:
led.on() # Encender el LED si se presiona el botón 1
sonido(440, 0.5) # Emitir un sonido si se presiona el botón 1
if boton2.value() == 1:
led.off() # Apagar el LED si se presiona el botón 2
buzzer.duty(0) # Apagar el buzzer si se presiona el botón 2
except KeyboardInterrupt:
# Si se presiona Ctrl+C, se detiene el bucle y se apaga el buzzer y el LED
buzzer.duty(0)
led.off()