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)
BOOTSELLED1239USBRaspberryPiPico©2020RP2-8020/21P64M15.00TTT