from time import sleep
from machine import I2C, Pin, ADC, PWM
from i2c_lcd import I2cLcd
from hcsr04 import HCSR04
ultrasonic = HCSR04(trigger_pin=13, echo_pin=12, echo_timeout_us=1000000)
AdressOfLcd = 0x27
i2c = I2C(0, scl=Pin(19), sda=Pin(18), freq=400000)
lcd = I2cLcd(i2c, AdressOfLcd, 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:',distance, "cm", '|', distance/2.54, 'inch')
lcd.clear()
lcd.putstr('Distance: {:.2f} cm'.format(distance))
if distance <= 200:
led1.on()
led2.off()
led3.off()
buzzer.duty(0)
elif distance <= 350:
led1.off()
led2.on()
led3.off()
buzzer.duty(0)
elif distance >= 350:
led1.off()
led2.off()
led3.on()
buzzer.duty(512)
else:
led1.off()
led2.off()
led3.off()
buzzer.duty(0)
sleep(5)