from machine import Pin, PWM, SPI
import time
import ili9341
# 1. CONFIGURACIÓN DE LA PANTALLA (SPI 0)
# Pines: SCK=18, MOSI=19, CS=17, DC=20, RST=21
spi = SPI(0, baudrate=40000000, sck=Pin(18), mosi=Pin(19))
display = ili9341.ILI9341(
spi,
cs=Pin(17),
dc=Pin(20),
rst=Pin(21),
w=320,
h=240,
r=3 # Rotación horizontal
)
# 2. CONFIGURACIÓN RESTO DE COMPONENTES
led_red = Pin(13, Pin.OUT)
led_green = Pin(14, Pin.OUT)
pir = Pin(28, Pin.IN)
servo = PWM(Pin(16))
servo.freq(50)
def mover_servo(angulo):
duty = int((angulo / 180 * 6500) + 1638)
servo.duty_u16(duty)
# 3. INICIO DEL SISTEMA
display.fill(ili9341.color565(0, 0, 255)) # Fondo Azul
display.text('SISTEMA INICIADO', 80, 110, 0xFFFF)
led_red.value(0)
led_green.value(1)
mover_servo(0)
time.sleep(2)
# 4. BUCLE PRINCIPAL
while True:
if pir.value() == 1:
# ALERTA
led_red.value(1)
led_green.value(0)
display.fill(ili9341.color565(255, 0, 0)) # Fondo Rojo
display.text('¡INTRUSO!', 120, 110, 0xFFFF)
mover_servo(90)
time.sleep(2) # Pausa de alerta
else:
# SEGURO
led_red.value(0)
led_green.value(1)
display.fill(ili9341.color565(0, 150, 0)) # Fondo Verde
display.text('TODO SEGURO', 100, 110, 0xFFFF)
mover_servo(0)
time.sleep(0.1)