from machine import Pin
import utime
# Sensor ultrasónico
trig = Pin(2, Pin.OUT)
echo = Pin(3, Pin.IN)
# Segmentos del display (a,b,c,d,e,f,g)
segmentos = [
Pin(6, Pin.OUT),
Pin(7, Pin.OUT),
Pin(8, Pin.OUT),
Pin(9, Pin.OUT),
Pin(10, Pin.OUT),
Pin(11, Pin.OUT),
Pin(12, Pin.OUT)
]
# Números para display de cátodo común
numeros = {
0: [1,1,1,1,1,1,0],
1: [0,1,1,0,0,0,0],
2: [1,1,0,1,1,0,1],
3: [1,1,1,1,0,0,1]
}
def mostrar(numero):
patron = numeros[numero]
for i in range(7):
segmentos[i].value(patron[i])
def distancia():
trig.low()
utime.sleep_us(2)
trig.high()
utime.sleep_us(10)
trig.low()
while echo.value() == 0:
inicio = utime.ticks_us()
while echo.value() == 1:
fin = utime.ticks_us()
tiempo = utime.ticks_diff(fin, inicio)
distancia = (tiempo * 0.0343) / 2
return distancia
while True:
d = distancia()
if d <= 10:
nivel = 1
elif d <= 20:
nivel = 2
elif d <= 30:
nivel = 3
else:
nivel = 0
mostrar(nivel)
print("Distancia: {:.2f} cm".format(d))
print("Nivel:", nivel)
print("-----------------------")
utime.sleep(1)
print ("Sensor De Distancia")
print ("Movimiento Detectado")