from machine import Pin, PWM
import time
pir = Pin(0, Pin.IN)
button = Pin(2, Pin.IN, Pin.PULL_DOWN)
buzzer = PWM(Pin(1))
system_active = False
last_button = 0
alarm_end_time = 0
def play_alarm():
global alarm_end_time
alarm_end_time = time.ticks_add(time.ticks_ms(), 5000)
while time.ticks_diff(alarm_end_time, time.ticks_ms()) > 0:
#1 KHZ
buzzer.freq(1000)
buzzer.duty_u16(30000)
time.sleep(0.5)
#2.5 KHZ
buzzer.freq(2500)
buzzer.duty_u16(30000)
time.sleep(0.5)
buzzer.duty_u16(0) # stop sound
while True:
current = button.value()
#ON OFF BUTTON
if current == 1 and last_button == 0:
system_active = not system_active
print("System Activation Status:", system_active)
time.sleep(0.3)
last_button = current
#MOTION DETECTED
if system_active and pir.value() == 1:
print("Motion Detected")
play_alarm()
time.sleep(0.1)