from machine import Pin
import utime
trigger = Pin(3, Pin.OUT)
echo = Pin(2, Pin.IN)
LED1 = Pin(22, Pin.OUT)
LED2 = Pin(20, Pin.OUT)
LED3 = Pin(21, Pin.OUT)
drukknop_blauw = Pin(27, Pin.IN)
def ultra():
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()
timpassed = signalon - signaloff
distance = (timpassed * 0.0330) / 2
print("afstand is", distance, "cm")
if distance >0 and distance <10:
LED1.on()
LED2.on()
LED3.on()
utime.sleep(1)
LED1.off()
LED2.off()
LED3.off()
if distance >11 and distance <20:
LED1.on()
LED2.on()
utime.sleep(1)
LED1.off()
LED2.off()
if distance >29 and distance <30:
LED1.on()
utime.sleep(1)
LED1.off()
while True:
if drukknop_blauw.value() == 1:
ultra()