from machine import Pin, time_pulse_us
import time
trig = Pin(3, Pin.OUT)
echo = Pin(2, Pin.IN)
led = Pin(14, Pin.OUT)
b = Pin(15, Pin.OUT)
while True:
trig.low()
time.sleep_us(2)
trig.high()
time.sleep_us(10)
trig.low()
t = time_pulse_us(echo, 1)
d = t / 58
print("Distance:", d)
if d < 50:
led.on()
b.on()
else:
led.off()
b.off()
time.sleep(1)