import machine
import time
pir = machine.Pin(28, machine.Pin.IN)
led = machine.Pin(15, machine.Pin.OUT)
b_led = machine.Pin(1, machine.Pin.OUT)
y_led = machine.Pin(5, machine.Pin.OUT)
button = machine.Pin(17, machine.Pin.IN, machine.Pin.PULL_DOWN) # Added PULL_DOWN for button stability
alarm_active = False # Use a flag to control the alarm state
def button_handler(pin):
global alarm_active
if alarm_active:
print("Alarm disarmed by button.")
b_led.value(0)
y_led.value(0)
alarm_active = False
def pir_handler(pin):
global alarm_active
if not alarm_active: # Only trigger if alarm is not already active
print("Alarm is triggered! Motion detected")
alarm_active = True
for _ in range(50): # Use _ if the loop variable isn't used
if not alarm_active: # Check if button has disarmed the alarm during the loop
break
b_led.toggle()
time.sleep_ms(200)
y_led.toggle()
time.sleep_ms(100)
# Ensure LEDs are off if the loop completes without disarming
if alarm_active:
b_led.value(0)
y_led.value(0)
alarm_active = False # Reset alarm_active after the sequence if not disarmed by button
pir.irq(trigger=machine.Pin.IRQ_RISING, handler=pir_handler)
button.irq(trigger=machine.Pin.IRQ_RISING, handler=button_handler)
while True:
led.toggle()
time.sleep(5)
#IRQ_FALLING - Initial value is high(1)
#IRQ_RISING - Initial value is low(0)