from machine import Pin
import utime
trig=Pin(0,Pin.OUT, value=0)
echo =Pin(1,Pin.IN)
RelayIn=Pin(2,Pin.OUT,value=0)
RelayOut=Pin(3,Pin.OUT,value=0)
button =Pin(4,Pin.IN,Pin.PULL_UP)
D=320
h=0
def ultrason ():
t=Toff=Ton=distance=0
trig.on()
utime.sleep_us(10)
trig.off()
while echo.value()==0:
Toff=utime.ticks_us()
while echo.value()==1:
Ton=utime.ticks_us()
t=(Ton-Toff)/2
distance=t*0.034
h=320-distance
Hpercent=int (h*(100/300))
print(button .value())
if 210<h<=270:
RelayIn.on()
RelayOut.off()
elif h <=210:
RelayOut.off()
RelayIn.on()
else:
RelayIn.off()
RelayOut.on()
# void loop
while 1:
while button.value()==0:
ultrason()
utime.sleep_ms(50)
else:
pass