print("Alarm Motion Detector system with PIR sensor & buzzer,display in OLED")
import ssd1306 
from machine import Pin
from utime import sleep
from machine import  SoftI2C, Pin, PWM
motion = False
i2c_oled = SoftI2C(scl=Pin(15), sda=Pin(4))
oled_width = 128
oled_height = 64 
oled = ssd1306.SSD1306_I2C(oled_width, oled_height, i2c_oled)
def handle_interrupt(pin):
    global motion
    motion = True
    global interrupt_pin
    interrupt_pin = pin
led_detect = Pin(25, Pin.OUT)
pir = Pin(32, Pin.IN)
pir.irq(trigger=Pin.IRQ_RISING, handler=handle_interrupt)
buzzer = PWM(Pin(5),Pin.OUT)
while True:
    
    if motion == True:
        print("Alert!!")
        print("Something moving!")
        print("Carefull!!")
        oled.fill(0)
        oled.text("Alert!!", 0, 5, 1)
        oled.text("Something moving!!", 0, 25, 1)
        oled.text("Carefull!!", 0, 45, 1)
        oled.show()
        
        
        for i in range(20):
            buzzer.init(freq=911, duty=512)
            led_detect.on()
            sleep(0.3)
            led_detect.off()
            buzzer.init(freq=2, duty=0)
            sleep(0.3)
    
        
        motion = False