from machine import Pin, time_pulse_us
from time import sleep

TRIG = Pin(2, Pin.OUT)
ECHO = Pin(4, Pin.IN)

display1 = [Pin(0, Pin.OUT), Pin(1, Pin.OUT), Pin(3, Pin.OUT), Pin(5, Pin.OUT), Pin(6, Pin.OUT), Pin(7, Pin.OUT), Pin(8, Pin.OUT)]
display2 = [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)]
display3 = [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)]

numeros = [
    [0, 0, 0, 0, 0, 0, 1],  # 0
    [1, 0, 0, 1, 1, 1, 1],  # 1
    [0, 0, 1, 0, 0, 1, 0],  # 2
    [0, 0, 0, 0, 1, 1, 0],  # 3
    [1, 0, 0, 1, 1, 0, 0],  # 4
    [0, 1, 0, 0, 1, 0, 0],  # 5
    [0, 1, 0, 0, 0, 0, 0],  # 6
    [0, 0, 0, 1, 1, 1, 1],  # 7
    [0, 0, 0, 0, 0, 0, 0],  # 8
    [0, 0, 0, 0, 1, 0, 0],  # 9
]

def exibir_numero(display, numero):
    for i in range(7):
        display[i].value(numeros[numero][i])

def medir_distancia():
    TRIG.value(0)
    sleep(0.000002)  
    TRIG.value(1)
    sleep(0.00001) 
    TRIG.value(0)

    duracao = time_pulse_us(ECHO, 1)
    
    distancia = (duracao / 2) / 29.1
    return distancia

def main():
    while True:
        distancia = int(medir_distancia())
       
        centenas = (distancia // 100) % 10
        dezenas = (distancia // 10) % 10
        unidades = distancia % 10
        
        exibir_numero(display1, centenas)
        exibir_numero(display2, dezenas)
        exibir_numero(display3, unidades)
        
        sleep(0.5)

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