import machine
import utime
from machine import I2C, Pin
from pico_i2c_lcd import I2cLcd # Ensure this file is on your Pico
# 1. Setup I2C and LCD
# Adjust Pins if necessary (SDA=GP0, SCL=GP1)
i2c = I2C(0, sda=Pin(0), scl=Pin(1), freq=400000)
# Auto-detect I2C Address
devices = i2c.scan()
if not devices:
print("No I2C LCD found!")
else:
I2C_ADDR = devices[0]
lcd = I2cLcd(i2c, I2C_ADDR, 2, 16)
# 2. Setup PIR Sensors
pir1 = Pin(16, Pin.IN)
pir2 = Pin(17, Pin.IN)
pir3 = Pin(18, Pin.IN)
# Initialize previous states to 0
p1_prev, p2_prev, p3_prev = 0, 0, 0
print("System Armed. Watching for intruders...")
lcd.clear()
lcd.move_to(0, 0)
lcd.putstr("SYSTEM ARMED")
def trigger_alert(top_text):
"""Helper to clear screen and display alert"""
lcd.clear()
lcd.move_to(0, 0)
lcd.putstr(top_text)
lcd.move_to(0, 1)
lcd.putstr(" ALERT ALERT ")
print(f"Log: {top_text}")
while True:
# Read current PIR values
v1 = pir1.value()
v2 = pir2.value()
v3 = pir3.value()
# Check for "Rising Edge" (Transition from 0 to 1)
if v3 == 1 and p3_prev == 0:
trigger_alert("ADULT DETECTED")
elif v2 == 1 and p2_prev == 0:
trigger_alert("SISTER DETECTED")
elif v1 == 1 and p1_prev == 0:
trigger_alert("CAT DETECTED")
# Update previous states for next loop
p1_prev, p2_prev, p3_prev = v1, v2, v3
# Small delay for stability
utime.sleep(0.1)