from hcsr04 import SR04
from machine import Pin
from machine import I2C, Pin
from I2C_LCD import I2cLcd
import time

SR=SR04(14,27)
ledR = Pin(15,Pin.OUT)
ledV = Pin(4,Pin.OUT)
i2c = I2C(scl=Pin(12), sda=Pin(13))
devices = i2c.scan()

for device in devices:
    lcd = I2cLcd(i2c, device, 2, 16)   

try:
    while True:
        distance =SR.distance()
        print(distance)
        time.sleep(2)
        if distance < 200:
            ledR.value(1)
            ledV.value(0)
            try: 
                lcd.clear()
                lcd.move_to(4, 0)
                lcd.putstr(str(distance)+"cm")
                lcd.move_to(3, 1)
                lcd.putstr("Attention!")
            except:
                pass  
    
        elif distance > 200 :
            ledR.value(0)
            ledV.value(1)
            try:  
                lcd.clear()
                lcd.move_to(7, 0)
                lcd.putstr("ok")
                lcd.move_to(3, 1)
                lcd.putstr(str(distance)+" cm")
            except:
                pass  
            
except:
    pass