from machine import Pin, time_pulse_us
from time import sleep_us, sleep_ms
trg = Pin (13, Pin.OUT)
ech = Pin (23, Pin.IN)
def medeDistCm (t, e):
# dispara o pulso
t.value(0)
sleep_us(5)
t.value(1)
sleep_us(10)
t.value(0)
# aguarda o eco
tempo = time_pulse_us (e, 1, 1000000)
print (tempo)
# calcula a distância em cm
#d = tempo/2 / 29.4
d = tempo/58.8
return d
x = [0,0,0,0,0]
while True:
for i in range(len(x)):
x[i] = medeDistCm(trg, ech)
xmed = sum(x) / len(x)
#print (x)
print (xmed)
sleep_ms(2000)