import utime
import machine
from machine import I2C
from LCD_API import LcdApi
from Pico_I2C_LCD import I2cLcd

I2C_ADDR     = 0x27
I2C_NUM_ROWS = 2
I2C_NUM_COLS = 16

i2c = I2C(0, sda=machine.Pin(4), scl=machine.Pin(5), freq=800000)
lcd = I2cLcd(i2c, I2C_ADDR, I2C_NUM_ROWS, I2C_NUM_COLS)

sensor_pir = machine.Pin(28, machine.Pin.IN)
buzzer = machine.Pin(15, machine.Pin.OUT)

buzz_alarm = False

def pir_handler(pin):
    global buzz_alarm
    if buzz_alarm == False:
        lcd.clear()
        lcd.move_to(5,0)
        lcd.putstr("Detected")
        buzz_alarm = True
        for i in range(50): 
            buzzer.toggle() 
            utime.sleep_ms(100)
        lcd.clear()
        lcd.move_to(5,0)
        lcd.putstr("Detecting")
        buzz_alarm = False

sensor_pir.irq(trigger=machine.Pin.IRQ_RISING, handler=pir_handler)

lcd.clear()
lcd.move_to(5,0)
lcd.putstr("Detecting")

while True:
    utime.sleep_ms(1)
    
    

