import time
import board
import digitalio
import pwmio
# ==========================================
# 1. INITIALIZE INPUTS AND OUTPUTS
# ==========================================
# -- Outputs --
led_green = digitalio.DigitalInOut(board.GP10)
led_green.direction = digitalio.Direction.OUTPUT
led_red = digitalio.DigitalInOut(board.GP11)
led_red.direction = digitalio.Direction.OUTPUT
led_yellow = digitalio.DigitalInOut(board.GP12)
led_yellow.direction = digitalio.Direction.OUTPUT
motor = digitalio.DigitalInOut(board.GP15)
motor.direction = digitalio.Direction.OUTPUT
buzzer = pwmio.PWMOut(board.GP14, duty_cycle=0, frequency=1000)
# -- Inputs --
btn_1 = digitalio.DigitalInOut(board.GP18)
btn_1.direction = digitalio.Direction.INPUT
btn_1.pull = digitalio.Pull.DOWN
btn_2 = digitalio.DigitalInOut(board.GP19)
btn_2.direction = digitalio.Direction.INPUT
btn_2.pull = digitalio.Pull.DOWN
mr_sensor = digitalio.DigitalInOut(board.GP20)
mr_sensor.direction = digitalio.Direction.INPUT
mr_sensor.pull = digitalio.Pull.DOWN # Assuming active high
vib_sensor = digitalio.DigitalInOut(board.GP21)
vib_sensor.direction = digitalio.Direction.INPUT
vib_sensor.pull = digitalio.Pull.DOWN # Assuming active high
# Helper function for buzzer
def sound_buzzer(freq, duration, continuous=False):
buzzer.frequency = freq
buzzer.duty_cycle = 32768 # 50% volume
if not continuous:
time.sleep(duration)
buzzer.duty_cycle = 0
# ==========================================
# PART 1: ARMING THE SYSTEM
# ==========================================
# Loop until Push Button 1 is pressed
while True:
if btn_1.value: # Yes: Push Button 1 Pressed?
# Activate Alarm System by sounding Buzzer (single chirp)
sound_buzzer(1000, 0.2)
time.sleep(0.1) # Debounce
break # Exit the setup loop to move to armed phase
# No: It loops back to check automatically
# Turn ON Green LED (System is Armed)
led_green.value = True
# ==========================================
# MAIN MONITORING LOOP (PARTS 2 & 3)
# ==========================================
while True:
# Check the Microwave Radar (MR) Sensor
if mr_sensor.value:
# --- PART 2: INTRUSION LOGIC ---
# Sound the buzzer and Turn ON Red LED
buzzer.frequency = 1500
buzzer.duty_cycle = 32768
led_red.value = True
# Wait until Push Button 2 is pressed to stop
while True:
if btn_2.value: # Push Button 2 Pressed?
# Stop the Buzzer and Turn OFF Red LED
buzzer.duty_cycle = 0
led_red.value = False
time.sleep(0.3) # Debounce
# Flowchart shows an arrow going back to "Turn ON Green LED"
# The loop will naturally restart, keeping the system armed.
break
else:
# Check the Vibration Sensor
if vib_sensor.value:
# --- PART 3: VIBRATION LOGIC ---
# Close the Window using DC Motor
motor.value = True
# Turn ON (or flash) Yellow LED
led_yellow.value = True
# Sound the high-frequency Buzzer siren
# Note: Flowchart shows an infinite loop stuck on "Sound Buzzer"
while True:
sound_buzzer(3000, 0.5, continuous=True)
# The system requires a physical reset/power cycle here based on flowchart diagram
time.sleep(0.1) # Small delay to stabilize the main loop