from machine import Pin
from gpio_lcd import GpioLcd
import time
import utime
tank_height = 150 #give the height in centimeter that the ultrasonic detects the height of tank in empty stage
trigger = Pin(15,Pin.OUT)#Give the Pin number that which pin You used
echo = Pin(14,Pin.IN)#Give the Pin number that which pin You used
buzzer = Pin(2,Pin.OUT)
relay = Pin(7,Pin.OUT)
def lcd():
display = GpioLcd(rs_pin = Pin(8),
enable_pin = Pin(9),
d4_pin = Pin(10),
d5_pin = Pin(11),
d6_pin = Pin(12),
d7_pin = Pin(13))
distance_in_percentage = (Distance() * (100/tank_height))
display.move_to(0,0)
display.putstr("TANK WATER LVL")
display.move_to(4,1)
display.putstr(str(int((100+10)-distance_in_percentage))+" %")
print(str(int((100+10)-distance_in_percentage))+" %")
def Distance():
trigger.low()
utime.sleep_us(2)
trigger.high()
utime.sleep_us(5)
trigger.low()
while echo.value()==0:
signal_transmitted_time = utime.ticks_us()
while echo.value()==1:
signal_received_time = utime.ticks_us()
overall_time = signal_received_time - signal_transmitted_time
distance = (0.0344 * overall_time) / 2
return distance
while True:
if Distance() <= 15:
relay.off()
lcd()
if Distance() <=10:
for i in range(20):
lcd()
buzzer.on()
utime.sleep(0.5)
buzzer.off()
utime.sleep(0.5)
if Distance() > 10:
break
else:
lcd()
buzzer.off()
elif Distance() >= (tank_height-10):
lcd()
relay.on()
if Distance() >= (tank_height-5):
for i in range(10):
lcd()
buzzer.on()
utime.sleep(0.5)
buzzer.off()
utime.sleep(0.5)
if Distance() < (tank_height-5):
break
else:
lcd()
buzzer.off()
else:
lcd()
utime.sleep(0.5)
continue