import machine
import time
# ==========================================
# 1. SETUP OUTPUTS
# ==========================================
green_led = machine.Pin(14, machine.Pin.OUT)
red_led = machine.Pin(18, machine.Pin.OUT)
yellow_led = machine.Pin(20, machine.Pin.OUT)
motor_sim = machine.Pin(21, machine.Pin.OUT) # Blue LED
buzzer = machine.PWM(machine.Pin(13))
buzzer.duty_u16(0)
# ==========================================
# 2. SETUP INPUTS (Using Internal PULL_DOWN for Wokwi)
# ==========================================
arm_btn = machine.Pin(15, machine.Pin.IN, machine.Pin.PULL_DOWN)
radar_btn = machine.Pin(16, machine.Pin.IN, machine.Pin.PULL_DOWN)
reset_btn = machine.Pin(17, machine.Pin.IN, machine.Pin.PULL_DOWN)
vib_btn = machine.Pin(19, machine.Pin.IN, machine.Pin.PULL_DOWN)
# ==========================================
# 3. MAIN LOGIC (STRICTLY FOLLOWING FLOWCHART)
# ==========================================
# --- PART 1 ---
# Check Push Button 1 -> Loop until pressed
while arm_btn.value() == 0:
pass
# Activate Alarm System by Sound the Buzzer
buzzer.freq(1000)
buzzer.duty_u16(32768)
time.sleep(1)
buzzer.duty_u16(0)
time.sleep(0.5)
# Main System Loop (Arrows return here after reset)
while True:
# Turn ON Green LED
green_led.value(1)
# Check the Microwave Radar (MR) Sensor
if radar_btn.value() == 1:
# --- PART 2 ---
while True:
# Sound the Buzzer
buzzer.freq(1000)
buzzer.duty_u16(32768)
# Turn ON Red LED
red_led.value(1)
# Check Push Button 2
if reset_btn.value() == 1:
# Stop the Buzzer
buzzer.duty_u16(0)
# Turn OFF Red LED
red_led.value(0)
time.sleep(0.5) # Small debounce
break # Arrow goes back up to "Turn ON Green LED"
time.sleep(0.1)
else:
# Check the Vibration Sensor
if vib_btn.value() == 1:
# --- PART 3 ---
# Close the Window using DC Motor
motor_sim.value(1)
# Turn ON Yellow LED
yellow_led.value(1)
# Infinite Loop: Sound the Buzzer
while True:
buzzer.freq(2500)
buzzer.duty_u16(32768)
time.sleep(0.1)
time.sleep(0.1) # Small delay to prevent crashing