from machine import Pin
from time import sleep
pir_pin = Pin(7, Pin.IN)
buzz_pin = Pin(12, Pin.OUT)
motion_detected = False
motion_stopped_printed = False
def pir_interrupt(pin):
global motion_detected
if pin.value() == 1:
motion_detected = True
buzz_pin.value(1)
else:
motion_detected = False
buzz_pin.value(0)
pir_pin.irq(trigger=(Pin.IRQ_RISING | Pin.IRQ_FALLING), handler=pir_interrupt)
try:
while True:
if motion_detected and not motion_stopped_printed:
print("Motion detected!")
motion_stopped_printed = True
elif not motion_detected and motion_stopped_printed:
print("Motion stopped.")
motion_stopped_printed = False
sleep(0.1)
except KeyboardInterrupt:
print("Keyboard interrupt.")
pir_pin.irq(trigger=0)
buzz_pin.value(0)