from machine import Pin, SoftI2C
import machine
import hcsr04
import time
from lcd_api import LcdApi
from i2c_lcd import I2cLcd
I2C_ADDR = 0x27

ultrasonic = hcsr04.HCSR04(trigger_pin=13, echo_pin=12, echo_timeout_us=1000000)
buzzer = machine.PWM(machine.Pin(32, machine.Pin.OUT))
buzzer.freq(4186)
buzzer.duty(0)
totalRows = 2
totalColumns = 16
i2c = SoftI2C(scl = Pin(22) , sda= Pin(21) , freq = 10000)
lcd = I2cLcd(i2c,I2C_ADDR,totalRows,totalColumns)
lcd.putstr('Hello')
h=-1
while True:
    distance = ultrasonic.distance_cm()
    print('Distance:', distance, 'cm', '|', distance/2.54, 'inch')
    if distance <= 50:
        buzzer.duty(512)
    else:
        buzzer.duty(0)

    h = distance
    lcd.clear()
    lcd.putstr("Distance:   ")
    lcd.putstr(str(h))

    time.sleep_ms(1000)
    lcd.clear()