import time
import board
import digitalio
import pwmio
# --- INPUTS ---
btn1_power = digitalio.DigitalInOut(board.GP16)
btn2_reset = digitalio.DigitalInOut(board.GP17)
pir_sensor = digitalio.DigitalInOut(board.GP14)
flame_sensor = digitalio.DigitalInOut(board.GP15)
for pin in (btn1_power, btn2_reset, pir_sensor, flame_sensor):
pin.direction = digitalio.Direction.INPUT
pin.pull = digitalio.Pull.DOWN
# --- OUTPUTS ---
green_led = digitalio.DigitalInOut(board.GP11)
red_led = digitalio.DigitalInOut(board.GP12)
yellow_led = digitalio.DigitalInOut(board.GP13)
for led in (green_led, red_led, yellow_led):
led.direction = digitalio.Direction.OUTPUT
# Buzzer on GP18 (Timer Slice 1)
buzzer = pwmio.PWMOut(board.GP18, duty_cycle=0, frequency=440, variable_frequency=True)
# Servo on GP20 (Timer Slice 2 - No collision!)
# 3276 duty cycle = 1.0ms pulse (0 degrees / Window Closed)
pwm_servo = pwmio.PWMOut(board.GP20, duty_cycle=3276, frequency=50)
# --- SYSTEM STATES ---
system_on = False
pir_alarm_active = False
print("System Ready. Press Button 1 (GP16) to Power ON.")
while True:
b1_pressed = btn1_power.value
b2_pressed = btn2_reset.value
motion_detected = pir_sensor.value
fire_detected = flame_sensor.value
if b1_pressed and not system_on:
system_on = True
print("System Powered ON. Green LED Active.")
time.sleep(0.3)
if system_on:
if motion_detected and not pir_alarm_active:
pir_alarm_active = True
print("Intrusion Detected! Latching Red Alarm.")
if pir_alarm_active and b2_pressed:
pir_alarm_active = False
print("Alarm Reset by Button 2. Back to Standby.")
time.sleep(0.3)
if fire_detected:
yellow_led.value = True
red_led.value = False
green_led.value = False
# 4915 duty cycle = 1.5ms pulse (90 degrees / Window Open)
pwm_servo.duty_cycle = 4915
buzzer.frequency = 2500
buzzer.duty_cycle = 32768
else:
yellow_led.value = False
pwm_servo.duty_cycle = 3276 # Return to 0 degrees
if system_on:
if pir_alarm_active:
red_led.value = True
green_led.value = False
buzzer.frequency = 800
buzzer.duty_cycle = 32768
else:
red_led.value = False
green_led.value = True
buzzer.duty_cycle = 0
else:
green_led.value = False
red_led.value = False
buzzer.duty_cycle = 0
time.sleep(0.05)