from machine import Pin, time_pulse_us
from time import sleep

trigger = Pin(0, Pin.OUT)
echo = Pin(1, Pin.IN)

displayUnidade = [
    Pin(2, Pin.OUT),
    Pin(3, Pin.OUT),
    Pin(4, Pin.OUT),
    Pin(5, Pin.OUT),
    Pin(6, Pin.OUT),
    Pin(7, Pin.OUT),
    Pin(8, Pin.OUT)
]

displayDecimal = [
    Pin(9, Pin.OUT),
    Pin(10, Pin.OUT),
    Pin(11, Pin.OUT),
    Pin(12, Pin.OUT),
    Pin(13, Pin.OUT),
    Pin(14, Pin.OUT),
    Pin(15, Pin.OUT)
]

displayCent = [
    Pin(16, Pin.OUT),
    Pin(17, Pin.OUT),
    Pin(18, Pin.OUT),
    Pin(19, Pin.OUT),
    Pin(20, Pin.OUT),
    Pin(21, Pin.OUT),
    Pin(22, Pin.OUT)
]

valores = [
    [0,0,0,0,0,0,1],
    [1,0,0,1,1,1,1],
    [0,0,1,0,0,1,0],
    [0,0,0,0,1,1,0],
    [1,0,0,1,1,0,0],
    [0,1,0,0,1,0,0],
    [0,1,0,0,0,0,0],
    [0,0,0,1,1,1,1],
    [0,0,0,0,0,0,0],
    [0,0,0,0,1,0,0]
]

for pin in displayUnidade:
    pin.value(1)

for pin in displayDecimal:
    pin.value(1)

for pin in displayCent:
    pin.value(1)

def setUnidade(value):
    z = 0
    for i in valores[value]:
        displayUnidade[z].value(i)
        z = z + 1

def setDecimal(value):
    y = 0
    for i in valores[value]:
        displayDecimal[y].value(i)
        y = y + 1

def setCent(value):
    y = 0
    for i in valores[value]:
        displayCent[y].value(i)
        y = y + 1

setUnidade(0)
setDecimal(0)
setCent(0)

while True:
    trigger.value(1)
    sleep(0.001)
    trigger.value(0)

    tempo = time_pulse_us(echo, Pin.high)
    distancia = round(400*tempo/23528)
    print(distancia)
    cent = round(distancia // 100)
    dec = round((distancia - (cent*100)) // 10)
    uni = round(distancia - ((cent*100) + (dec*10)))
    setUnidade(uni)
    setDecimal(dec)
    setCent(cent)
    sleep(0.5)
BOOTSELLED1239USBRaspberryPiPico©2020RP2-8020/21P64M15.00TTT