import time
from machine import I2C, Pin, time_pulse_us
from i2c_lcd import I2cLcd

SOUND_SPEED=340
TRIG_PULSE_DURATION_US=1
trig_pin = Pin(0, Pin.OUT) #  초음파 쏘는애
echo_pin = Pin(1, Pin.IN)  #  초음파 수신부
pin = Pin(5, Pin.IN, Pin.PULL_DOWN)
DEFAULT_I2C_ADDR = 0x27
isMotionDetected = False

buzzer = Pin(15, Pin.OUT)

def detect_motion(pin):
    global isMotionDetected
    isMotionDetected=True


def setup():
    global lcd
    i2c = I2C(0,sda=Pin(20),scl=Pin(21),freq=400000) # SDA는 0번핀, SCL은 1번 핀
    lcd = I2cLcd(i2c, DEFAULT_I2C_ADDR, 2, 16)

def loop():
    global isMotionDetected
    while True:
        trig_pin.value(0)
        time.sleep_us(5)
        trig_pin.value(1)
        time.sleep_us(TRIG_PULSE_DURATION_US)
        trig_pin.value(0)
        ultrason_duration = time_pulse_us(echo_pin, 1, 30000)
        distance = SOUND_SPEED * ultrason_duration / 20000


        pin.irq(trigger=Pin.IRQ_RISING, handler=detect_motion)
        
        lcd.move_to(0,0) # 첫번째 줄
        lcd.putstr(f"street : {distance}" ) # 작성할 내용
        if isMotionDetected:
            isMotionDetected=False
            lcd.move_to(0,1)
            lcd.putstr("Motion detected") # 작성할 내용
        

        if distance <= 100 and distance > 50:
            buzzer.on()
            time.sleep(1)
            buzzer.off()
        elif distance <=50 and distance > 10:
            buzzer.on()
            time.sleep(0.25)
            buzzer.off()
        elif distance <= 10:
            buzzer.on()
            time.sleep(0.05)
            buzzer.off()    

    # 동작 시작 부분

setup()
loop()
$abcdeabcde151015202530354045505560fghijfghij
BOOTSELLED1239USBRaspberryPiPico©2020RP2-8020/21P64M15.00TTT