from machine import Pin
import time
segments = [
Pin(2, Pin.OUT), #a
Pin(4, Pin.OUT), #b
Pin(16,Pin.OUT), #c
Pin(17, Pin.OUT),#d
Pin(5, Pin.OUT), #e
Pin(18, Pin.OUT),#f
Pin(19,Pin.OUT) #g
]
numbers = [
#a b. c d e f g
[1, 1, 1, 1, 1, 1, 0], # 0
[0, 1, 1, 0, 0, 0, 0], # 1
[1, 1, 0, 1, 1, 0, 1], # 2
[1, 1, 1, 1, 0, 0, 1], # 3
[0, 1, 1, 0, 0, 1, 1], # 4 -
[1, 0, 1, 1, 0, 1, 1], # 5
[1, 0, 1, 1, 1, 1, 1], # 6
[1, 1, 1, 0, 0, 0, 0], # 7
[1, 1, 1, 1, 1, 1, 1], # 8
[1, 1, 1, 1, 0, 1, 1] # 9
]
def display(number):
digit = numbers[number]
for i in range(7):
segments[i].value(digit[i])
trig = Pin(12 , Pin.OUT)
echo = Pin(14 , Pin.IN)
ton = toff = d = t = 0
while 1 :
trig.on()
time.sleep_us(10)
trig.off()
time.sleep_us(2)
while echo.value() == 0 :
toff = time.ticks_us()
while echo.value() == 1:
ton = time.ticks_us()
t = ( ton - toff ) / 2
d = t * 0.034
d = int(d *(100 / 400) / 10)
print(d)
time.sleep_ms(30)
display(d)