from machine import Pin
import utime
led_list = [28,27,26,22,21,20,19,18,17,16]
n=0
trigger = Pin(15, Pin.OUT)
echo = Pin(2, Pin.IN)
for pin_num in led_list:
led_list[n] = Pin(pin_num, Pin.OUT)
n+=1
def todos(array):
for x in range(10):
led_list[x].value(array[x])
def liga(quantos):
print (quantos)
for ligado in range(quantos):
led_list[ligado].value(1)
if quantos==1:
todos([1,0,0,0,0,0,0,0,0,0])
if quantos==2:
todos([1,1,0,0,0,0,0,0,0,0])
if quantos==3:
todos([1,1,1,0,0,0,0,0,0,0])
if quantos==4:
todos([1,1,1,1,0,0,0,0,0,0])
if quantos==5:
todos([1,1,1,1,1,0,0,0,0,0])
if quantos==6:
todos([1,1,1,1,1,1,0,0,0,0])
if quantos==7:
todos([1,1,1,1,1,1,1,0,0,0])
while True:
trigger.low()
utime.sleep_us(2)
trigger.high()
utime.sleep_us(5)
trigger.low()
while echo.value() == 0:
signaloff = utime.ticks_us()
while echo.value() == 1:
signalon = utime.ticks_us()
timepassed = signalon - signaloff
distance = (timepassed * 0.0343) / 2
#distance = "{:.1f}".format(distance)
result = ((distance/100) % 10) * 2.5
liga(int(result))
utime.sleep(0.5)