from machine import Pin
import time
# Sensor PIR
pir = Pin(14, Pin.IN)
# Buzzer
buzzer = Pin(15, Pin.OUT)
# Pines del LED RGB (cátodo común)
led_red = Pin(16, Pin.OUT)
led_green = Pin(17, Pin.OUT)
led_blue = Pin(18, Pin.OUT)
# Función para controlar el color del LED RGB
def set_color(r, g, b):
led_red.value(r)
led_green.value(g)
led_blue.value(b)
print("Sistema de alarma activado. Esperando movimiento...")
# Estado inicial - Azul (esperando movimiento)
set_color(0, 0, 1)
while True:
if pir.value() == 1:
print("¡Movimiento detectado!")
set_color(1, 0, 0) # Rojo
time.sleep_ms(200)
set_color(0, 1, 0)
time.sleep_ms(200)
set_color(0, 0, 1)
time.sleep_ms(200)
set_color(1, 1, 0)
time.sleep_ms(200)
set_color(0, 1, 1)
time.sleep_ms(200)
set_color(1, 1, 1)
time.sleep_ms(200)
buzzer.value(1)
time.sleep_ms(2000)
buzzer.value(0)
time.sleep_ms(500)
else:
set_color(0, 0, 0) # Verde
buzzer.value(0)
time.sleep_ms(100)