import machine
from machine import Pin, PWM
import time, math
from time import sleep
PWM_servo = PWM(Pin(2),freq=50,duty=0)
echo=Pin(14,Pin.IN)
trig=Pin(12,Pin.OUT)
def Servo(servo, angle):
PWM_servo.duty(int(((angle)/180*2+0.5)/20*1023))
while True:
trig.value(0)
time.sleep(0.5)
trig.value(1)
time.sleep(0.5)
trig.value(0)
x=machine.time_pulse_us(echo,1)
distance = (0.034*x)/2
tran_dis = '%.2f' %(distance)
print(f"Distance : {tran_dis} cm")
if distance<200:
Servo(2,90)
sleep(1)
else:
Servo(2,0)
sleep(1)