from machine import Pin, PWM
import time
calibrationTime = 5
lowIn = 0
pause = 5000
lockLow = True
takeLowTime = False
pirPin = Pin(2, Pin.IN)
ledPin = Pin(4, Pin.OUT)
buzzer = PWM(Pin(5))
buzzer.freq(1000)
def setup():
print("calibrating sensor ", end="")
for i in range(calibrationTime):
print(".", end="")
time.sleep(1)
print(" done")
print("SENSOR ACTIVE")
time.sleep(0.05)
# Initialize the global variables here
setup()
while True:
if pirPin.value() == 1:
ledPin.on()
buzzer.duty(512)
if lockLow:
lockLow = False
print("---")
print("motion detected at", time.ticks_ms() // 1000, "sec")
time.sleep(0.05)
takeLowTime = True
if pirPin.value() == 0:
ledPin.off()
buzzer.duty(0)
if takeLowTime:
lowIn = time.ticks_ms()
takeLowTime = False
if not lockLow and time.ticks_ms() - lowIn > pause:
lockLow = True
print("motion ended at", (time.ticks_ms() - pause) // 1000, "sec")
time.sleep(0.05)