from machine import Pin, time_pulse_us
from time import sleep
trig = Pin(13, Pin.OUT)
echo = Pin(12, Pin.IN)
red = Pin(26, Pin.OUT)
blue = Pin(21, Pin.OUT)
yellow = Pin(27, Pin.OUT)
orange = Pin(22, Pin.OUT)
def measure_distance():
trig.low()
sleep(0.002)
trig.high()
sleep(0.00001)
trig.low()
duration = time_pulse_us(echo, 1)
distance = (duration * 0.0343) / 2
return distance
while True:
distance_2 = measure_distance()
print("Distance:", distance_2, "cm")
sleep(1)
red.value(0)
blue.value(0)
yellow.value(0)
orange.value(0)
if distance_2 <= 100:
red.value(1)
elif distance_2 <= 200:
blue.value(1)
elif distance_2 <= 300:
yellow.value(1)
elif distance_2 <= 400:
orange.value(1)