from machine import Pin
from neopixel import NeoPixel
from hcsr04 import HCSR04
from time import sleep_ms
ultra = HCSR04(trigger_pin=17, echo_pin=16, echo_timeout_us=400000)
cores = ((255, 0, 0),
(255, 255, 0),
(0, 255, 0),
(0, 255, 255),
(0, 0, 255),
(255, 0, 255),
(0, 128, 255),
(128, 255, 0),
(255, 128, 0),
(255, 255, 255))
pix = NeoPixel (Pin(21, Pin.OUT), 1)
pix[0] = (0, 0, 0)
pix.write()
while True:
try:
distance = ultra.distance_mm()
print(f'Distancia: {distance}mm')
if distance < 130:
for _ in range(3):
for cor in cores:
pix[0] = cor
pix.write()
sleep_ms(350)
else:
pix[0] = (0, 0, 0)
pix.write()
sleep_ms(1000)
except OSError as ex:
print(f'ERROR getting distance: {ex}')
pix[0] = (0, 0, 0)
pix.write()
while True:
try:
distance = ultra.distance_mm()
print(f'Distancia: {distance}mm')
except OSError as ex:
print(f'ERROR getting distance: {ex}')
sleep(2)