from machine import Pin, PWM
import time
# --- CONFIGURACIÓN DE COMPONENTES ---
contacto = Pin(22, Pin.IN, Pin.PULL_UP)
buzzer = PWM(Pin(14))
led = Pin(1, Pin.OUT) # NUEVO: Configuramos el pin 15 como salida para el LED
def puerta_abierta():
return contacto.value() == 1
def sonar(modo):
if modo == 'intermitente':
activo = (time.ticks_ms() // 300) % 12 == 0
else:
activo = False
if activo:
buzzer.freq(1000)
buzzer.duty_u16(20000)
else:
buzzer.duty_u16(0)
while True:
# Revisamos el estado de la puerta
if puerta_abierta():
modo = 'intermitente'
led.value(1) # NUEVO: Si está abierta, encendemos el LED (1 = encendido)
else:
modo = 'apagado'
led.value(0) # NUEVO: Si está cerrada, apagamos el LED (0 = apagado)
sonar(modo)
time.sleep_ms(20)