from machine import Pin,PWM,Timer
from time import sleep_ms
from HCSR04 import HCSR04
S1=PWM(Pin(16),freq=50,duty=128)
trig=Pin(14,Pin.OUT)
echo=Pin(12,Pin.IN)
HC=HCSR04(trig,echo)
def rotate(servo,degree):
servo.duty(int(degree*0.54+25.6))
def fun(tim):
distance=HC.getDistance()
print(distance)
if distance>2 and distance<=180:
rotate(S1,distance)
else:
rotate(S1,180)
tim=Timer(1)
tim.init(period=500,mode=Timer.PERIODIC,callback=fun)