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)
led1 = Pin(4, Pin.OUT)
led2 = Pin(5, Pin.OUT)
AdressOfLcd = 0x27
i2c = I2C(0, scl=Pin(19), sda=Pin(18), freq=400000)
lcd = I2cLcd(i2c, AdressOfLcd, 2, 16)
buzzer = PWM(Pin(2, 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 <= 150:
led1.on()
led2.off()
buzzer.duty(0)
elif distance <= 250:
led1.off()
led2.on()
buzzer.duty(0)
elif distance > 250:
led1.on()
led2.on()
buzzer.duty(512)
else:
led1.off()
led2.off()
sleep(5)