import machine
import utime, math
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)
AdressOfLcd = 0x27
i2c =I2C(0, scl=Pin(19), sda=Pin(18), freq=400000)
lcd = I2cLcd(i2c, AdressOfLcd, 2, 16)
led1 = Pin(2, Pin.OUT)
pwm = PWM(Pin(4), freq=50, duty=0)
def Servo(servo, angle):
pwm.duty(int(((angle)/180*2+0.5)/20*1023))
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 > 350:
led1.on()
Servo(4, 180)
sleep(1)
else:
led1.off()
Servo(4, 0)
sleep(1)
sleep(5)