import time

from machine import Pin,PWM,ADC
# time.sleep(0.1) # Wait for USB to become ready
#1
# servo_load = PWM(Pin(28))
# servo_load.freq(50)
# while True:
#     for i in range (1000,9000,50):
#         servo_load.duty_u16(i)
#         time.sleep(0.2)

# xAxis = ADC(Pin(28))
# yAxis = ADC(Pin(27))
# SW = Pin(1,Pin.IN, Pin.PULL_UP)
# readDelay = 0.5

#2
# while True:
#     xRef = xAxis.read_u16()
#     yRef = yAxis.read_u16()
#     SWPushed = SW.value()
#     cur_dir=""
#     if yRef < 32759:
#         cur_dir+="bottom "
#     elif yRef >32759:
#         cur_dir+="top "
#     if xRef < 32759:
#         cur_dir+="right "
#     elif xRef >32759:
#         cur_dir+="left "
    
#     if SWPushed==0:
#         cur_dir+="Is Pressed"
    
#     print(cur_dir)
#     time.sleep(readDelay)
#3&4
from picozero import Servo
import time
from machine import time_pulse_us
# import time



servo = Servo(28)
servo.min()
time.sleep(4)

trig_pin = Pin(0, Pin.OUT)
echo = Pin(2, Pin.IN) 

while True:
    trig_pin.value(0)
    time.sleep_us(2)
    trig_pin.value(1)
    time.sleep_us(10)
    trig_pin.value(0)

    while echo.value() == 0:
       signaloff = time.ticks_us()
    while echo.value() == 1:
       signalon = time.ticks_us()
    timepassed = signalon - signaloff
    distance = (timepassed * 0.0343) / 2
    if distance>100:
        servo.mid()
    else:
        servo.max()
    print("The distance from object is ",distance,"cm")
    time.sleep(0.5)

BOOTSELLED1239USBRaspberryPiPico©2020RP2-8020/21P64M15.00TTT