from machine import Pin
from hcsr04 import HCSR04
from time import sleep
from servo import Servo
sensor = HCSR04(trigger_pin = 18, echo_pin = 5, echo_timeout_us=10000)
red = Pin(4,Pin.OUT)
yellow = Pin(2,Pin.OUT)
green = Pin(15,Pin.OUT)
moter = Servo(21)
while True:
distance = sensor.distance_cm()
print('Distance:',distance,'cm')
if sensor.distance_cm() < 50:
red.on()
yellow.off()
green.off()
if 50 < sensor.distance_cm() < 100:
yellow.on()
red.off()
green.off()
if 100 < sensor.distance_cm() < 150:
green.on()
yellow.off()
red.off()
else:
red.off()
yellow.off()
green.off()
sleep(0.5)