Fish tank "helper" project, water level reading with HC-SR04

Hello,
I’m working for a fish tank “helper”. I’m trying to measure the temperature and display on 16x2 lcd, cool down the tank if the temperature too high and measure the water level of the auto-top-off water container with ultrasonic sensor. Most of the parts of the code are taken from various other projects found online with additions to make it for work for my needs.

What I’m not too sure how to go about is the water level display. Right now I have the distance read by the sensor hard coded to display the “percentage” of water left in the tank.
I was wondering if there is a way to dynamically create the “percentage” based on the distance reading. (ie. 15%, 18%, 60% etc. instead of my 25%, 50%, 75%)
The tank full the distance reading is 10 and with the distance over 30 the tank is “empty”.
Any help would be appreciated for a this newbie.
Thanks.

#include <OneWire.h>
#include <DallasTemperature.h>
#include <LiquidCrystal_I2C.h>

#define ONE_WIRE_BUS 6

LiquidCrystal_I2C lcd(0x27, 16, 2);

int Motor_Pin1 = 4; 
int Motor_Pin2 = 3; 
int Enable = 5;     
int emptyled = 8;
int proxsensor = 9;
const int trig_pin = 13;
const int echo_pin = 12;
long distance, duration;

OneWire oneWire(ONE_WIRE_BUS);

DallasTemperature sensors(&oneWire);

float Fahrenheit = 0;

void setup(void)
{
  pinMode(Motor_Pin1, OUTPUT);
  pinMode(Motor_Pin2, OUTPUT);
  pinMode(Enable, OUTPUT);
  lcd.init();
  lcd.backlight();
  sensors.begin();
  pinMode(emptyled, OUTPUT);
  pinMode(13, OUTPUT); 
  pinMode(12, INPUT); 
  pinMode(proxsensor, INPUT);
  Serial.begin(9600);
}

void loop(void)
{

// backlight
  if ((digitalRead (proxsensor) == LOW)) {
    lcd.backlight();
  }

  if ((digitalRead (proxsensor) == HIGH)) {
    lcd.noBacklight();
  }
  
// temperature
  sensors.requestTemperatures();

  Fahrenheit = sensors.getTempFByIndex(0);

  lcd.setCursor(0, 0);
  lcd.print("Temp: ");
  lcd.println(Fahrenheit);
  lcd.setCursor(11, 0);
  lcd.print("F ");


// tank level  

  lcd.setCursor(0, 1);
  lcd.print("R/O : ");

  digitalWrite(13, HIGH);
  delayMicroseconds(20);
  digitalWrite(13, LOW);
  delayMicroseconds(20);
  duration = pulseIn(echo_pin, HIGH);
  distance = duration * 0.034 / 2;

Serial.println(distance);

if (distance < 10 ) {
   lcd.print("100%    ");
   digitalWrite(emptyled, LOW);
  }

 else if (distance >= 10 && distance <= 17) {
   lcd.print("75%    ");
   digitalWrite(emptyled, LOW);
 }

 else if (distance > 17 && distance <= 24) {
   lcd.print("50%    ");
   digitalWrite(emptyled, LOW);
 }

 else if (distance > 24 && distance <= 30) {
   lcd.print("25%    ");
   digitalWrite(emptyled, HIGH);
 }

  else if (distance >30) {
   lcd.print("EMPTY");
    digitalWrite(emptyled, HIGH);
    lcd.backlight();
  }



// fan control

  if (Fahrenheit < 82 ) {
    analogWrite(Enable, 0);
    lcd.setCursor(13, 0);
    lcd.print("   ");
    delay(100);
  }

  else if (Fahrenheit >= 82) {
    analogWrite(Enable, 255);
    lcd.setCursor(13, 0);
    lcd.print("FAN");
    lcd.backlight();
    delay(100);
  }

  digitalWrite(Motor_Pin1, LOW);
  digitalWrite(Motor_Pin2, HIGH);
  delay(1000); 
}

That is what the map function does best. :grinning:

Thank you very much. That's exactly what I needed.