from time import sleep
from machine import Pin, ADC, I2C, PWM
from i2c_lcd import I2cLcd
from hcsr04 import HCSR04
ultrasonic = HCSR04(trigger_pin=13, echo_pin=12, echo_timeout_us=1000000)
AddressOfLcd = 0x27
i2c = I2C(0, scl=Pin(19), sda=Pin(18), freq=400000)
lcd = I2cLcd(i2c, AddressOfLcd, 2, 16)
led1 = Pin(2,Pin.OUT)
led2 = Pin(4,Pin.OUT)
led3 = Pin(5,Pin.OUT)
buzzer = PWM(Pin(16, Pin.OUT))
buzzer.freq(4186)
buzzer.duty(0)
while True:
distance = ultrasonic.distance_cm()
print('Distance', 'cm', '|', distance/2.54, 'Inch')
lcd.clear()
lcd.putstr('Distance: {:.2f} cm'.format(distance))
if distance<= 150:
led1.on()
led2.off()
led3.off()
elif distance<= 250:
led1.off()
led2.on()
led3.off()
elif distance<= 350:
led1.off()
led2.off()
led3.on()
buzzer.duty(512)
else:
led1.off
led2.off
led3.off
buzzer.duty(0)
sleep(5)