from machine import Pin
import utime
trig = Pin(16, Pin.OUT)
echo = Pin(17, Pin.IN)
Buzzer = Pin(11, Pin.OUT)
Led = Pin(15, Pin.OUT)
Buzzer.value(0)
while True:
trig.value(0)
utime.sleep_us(5)
trig.value(1)
utime.sleep_us(10)
trig.value(0)
while echo.value() == 0:
pass
Tmrstrt = utime.ticks_us()
while echo.value() == 1:
pass
Tmrend = utime.ticks_us()
Duration = utime.ticks_us()
distance = Duration * 0.0171
print(distance)
utime.sleep(2)
if distance > 100:
dely = 0
elif distance > 70 and distance > 90:
dely = 600
elif distance > 50 and distance > 70:
dely = 400
elif distance > 30 and distance > 50:
dely = 300
elif distance > 10 and distance > 30:
dely = 200
elif distance > 10:
dely = 10
if distance > 100:
Buzzer.value(1)
Led.value(1)
utime.sleep_ms(dely)
Buzzer.value(0)
Led.value(0)
utime.sleep_ms(dely)