measuring ultrasonic sensor until percentage is met

for my project i am making a tank system that has controllable level in each of the two tanks using ultrasonic sensors, but i am getting stumped on the filling and emptying section (using relays to go and power 12v pumps). i have tried Do, While commands, If, For commands and a couple of others.
i was trying to set the marks at 20% intervals so i have full control over how full or empty it is.

i also have two screens one 20x4 I2C lcd which shows sounding level and another 16x2 serial lcd which shows a percentage bar of how full each tank is.

Anyone have anyone have and ideas or things i'm missing greatly appreciated.

#include <LiquidCrystal.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal lcd(2, 3, 4, 5, 6, 7);
LiquidCrystal_I2C lcd2(0x27, 20, 4);
#define length 16.0
double percent = 100.0;
unsigned char b;
unsigned char b2;
unsigned int peace;
unsigned int peace2;
const int in = 8, out = 9, x = 10, inn = 11, out2 = 12;
int count1 = 0;
int buttonState1 = 0;
int buttonState2 = 0;
int buttonState3 = 0;
const int but1 = 22, but2 = 23, but3 = 24, r2 = 30, r4 = 32, r6 = 34, LED = 38, LED2 = 40;
unsigned char i;



byte p20[8] = {
  B10000,
  B10000,
  B10000,
  B10000,
  B10000,
  B10000,
  B10000,
  B10000,
};
byte p40[8] = {
  B11000,
  B11000,
  B11000,
  B11000,
  B11000,
  B11000,
  B11000,
  B11000,
};
byte p60[8] = {
  B11100,
  B11100,
  B11100,
  B11100,
  B11100,
  B11100,
  B11100,
  B11100,
};
byte p80[8] = {
  B11110,
  B11110,
  B11110,
  B11110,
  B11110,
  B11110,
  B11110,
  B11110,
};
byte p100[8] = {
  B11111,
  B11111,
  B11111,
  B11111,
  B11111,
  B11111,
  B11111,
  B11111,
};

void setup() {

  pinMode (in, INPUT);
  pinMode (out, OUTPUT);
  pinMode (inn, INPUT);
  pinMode (out2, OUTPUT);
  lcd.begin (16, 2);
  lcd.setCursor (0, 0);
  lcd.createChar (0, p20);
  lcd.createChar (1, p40);
  lcd.createChar (2, p60);
  lcd.createChar (3, p80);
  lcd.createChar (4, p100);
  Serial.begin (9600);
  lcd2.init();
  lcd2.backlight();
  pinMode (but1, INPUT);
  pinMode (but2, INPUT);
  pinMode (but3, INPUT);
  pinMode (r2, OUTPUT);
  pinMode (r4, OUTPUT);
  pinMode (r6, OUTPUT);
  pinMode (LED, OUTPUT);
  pinMode (LED2, OUTPUT);
  lcd2.setCursor(0, 0);
  lcd2.print("Sound T1:");
  lcd2.setCursor(10, 0);
  lcd2.print("Sound T2:");

}
void loop() {
  lcd.setCursor (0, 0);
  count1 ++;
  buttonState1 = digitalRead(but1);
  buttonState2 = digitalRead(but2);
  buttonState3 = digitalRead(but3);

  digitalWrite (LED, LOW);
  digitalWrite (LED2, LOW);
  digitalWrite (r2, HIGH);
  digitalWrite (r4, HIGH);
  digitalWrite (r6, HIGH);

  //sensor Tx, Rx and calc:
  digitalWrite(out, LOW);
  delayMicroseconds(2);

  digitalWrite(out, HIGH);
  delayMicroseconds(10);
  digitalWrite(out, LOW);
  float dur = pulseIn(in, HIGH);
  float dur_sec = dur / 1000000;
  float dis_time = dur_sec / 2;
  float ullage = dis_time * 34300;
  delay (10);

  //No.2 sensor Tx, Rx and calc:
  digitalWrite(out2, LOW);
  delayMicroseconds(2);

  digitalWrite(out2, HIGH);
  delayMicroseconds(10);
  digitalWrite(out2, LOW);
  float dur2 = pulseIn(inn, HIGH);
  float dur_sec2 = dur2 / 1000000;
  float dis_time2 = dur_sec2 / 2;
  float ullage2 = dis_time2 * 34300;
  delay (10);


  //percentage calc:
  long Rullage = ullage - 2;
  long per = x * Rullage;
  long finalper = per / 100;
  long sounding = x * ullage;
  long Finalsound = 125 - sounding;


  //No.2 percentage calc:
  long Rullage2 = ullage2 - 2;
  long per2 = x * Rullage2;
  long finalper2 = per2 / 100;
  long sounding2 = x * ullage2;
  long Finalsound2 = 125 - sounding2;


  //tank one and two write:
  //lcd2.setCursor(0, 0);
  //lcd2.print("Sound T1:");
  //lcd2.setCursor(10, 0);
  //lcd2.print("Sound T2:");
  lcd2.setCursor(0, 1);
  lcd2.print(String(Finalsound));
  lcd2.print("  %  ");
  lcd2.setCursor(10, 1);
  lcd2.print(String(Finalsound2));
  lcd2.print("  %  ");
  delay (50);


  //write info:
  //lcd.print(ullage);
  //lcd.print(" - ");
  //lcd.print(per);
  //lcd.print(" %  ");
  //lcd.setCursor(0, 1);

  //testing part and print bar
  lcd.setCursor (0, 0);
  double a = length / 100 * Finalsound;
  if (a >= 1) {
    for (int i = 1; i < a; i++) {
      lcd.write(4);
      b = i;
    }
    a = a - b;
  }
  peace = a * 5;



  switch (peace) {
    case 0:
      break;
    case 1:
      lcd.print((char)0);
      break;
    case 2:
      lcd.write(1);
      break;
    case 3:
      lcd.write(2);
      break;
    case 4:
      lcd.write(3);
      break;
  }


  for (int i = 0; i < (length - b); i++) {
    lcd.print (" ");
    delay (50);

  }

  //No.2 testing part and print bar
  lcd.setCursor (0, 1);
  double a2 = length / 100 * Finalsound2;
  if (a2 >= 1) {
    for (int i2 = 1; i2 < a2; i2++) {
      lcd.write(4);
      b2 = i2;
    }
    a2 = a2 - b2;
  }
  peace2 = a2 * 5;



  switch (peace2) {
    case 0:
      break;
    case 1:
      lcd.print((char)0);
      break;
    case 2:
      lcd.write(1);
      break;
    case 3:
      lcd.write(2);
      break;
    case 4:
      lcd.write(3);
      break;
  }


  for (int i2 = 0; i2 < (length - b2); i2++) {
    lcd.print (" ");
    delay (50);

  };

  //100% port tank

  if (buttonState1 == HIGH) {
    digitalWrite (LED, HIGH);
    digitalWrite (r2, LOW);
    digitalWrite (LED, HIGH);
    delay (1000);
  
  while (finalper == 95) {
    //sensor Tx, Rx and calc:
    digitalWrite(out, LOW);
    delayMicroseconds(2);
    digitalWrite(out, HIGH);
    delayMicroseconds(10);
    digitalWrite(out, LOW);
    float dur = pulseIn(in, HIGH);
    float dur_sec = dur / 1000000;
    float dis_time = dur_sec / 2;
    float ullage = dis_time * 34300;
    delay (10);
    //percentage calc:
    long Rullage = ullage - 2;
    long per = x * Rullage;
    long finalper = per / 100;
    long sounding = x * ullage;
    long Finalsound = 125 - sounding;
    digitalWrite (r2, HIGH);
    digitalWrite (r4, HIGH);
  }
  }


  //      digitalWrite (LED, HIGH);
  //      delay (50);
  //      //sensor Tx, Rx and calc:
  //      digitalWrite(out, LOW);
  //      delayMicroseconds(2);
  //      digitalWrite(out, HIGH);
  //      delayMicroseconds(10);
  //      digitalWrite(out, LOW);
  //      float dur = pulseIn(in, HIGH);
  //      float dur_sec = dur / 1000000;
  //      float dis_time = dur_sec / 2;
  //      float ullage = dis_time * 34300;
  //      delay (10);
  //      //percentage calc:
  //      long Rullage = ullage - 2;
  //      long per = x * Rullage;
  //      long finalper = per / 100;
  //      long sounding = x * ullage;
  //      long Finalsound = 125 - sounding;
  //      digitalWrite (r2, HIGH);
  //      digitalWrite (r4, HIGH);

}

Final_testing_08.06.20.ino (6.17 KB)

Read the how to use this forum-please read sticky to see how to properly post code and some advice on how to ask an effective question. Remove useless white space and format the code with the IDE autoformat tool (crtl-t or Tools, Auto Format) before posting code.

You seem to have a wild mix of 'long' and 'float' variables in your calculations.

Are the two tanks basically the same? If so, you could write a single function that does all of the percent calculations and returns a value from 0 to 100.

Your sketch would be easier to understand if your buttons, relays, and LEDs had meaningful names instead of abstract numbers.

yes the tanks are pretty much identical. i think its a bit mixed with names because i did parts separately then collaborated them together in one code. with the mix of long and float when i was writing that section that one would give a figure to more decimal places for accuracy. also added the code more ease of use.

#include <LiquidCrystal.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal lcd(2, 3, 4, 5, 6, 7);
LiquidCrystal_I2C lcd2(0x27, 20, 4);
#define length 16.0
double percent = 100.0;
unsigned char b;
unsigned char b2;
unsigned int peace;
unsigned int peace2;
const int in = 8, out = 9, x = 10, inn = 11, out2 = 12;
int count1 = 0;
int buttonState1 = 0;
int buttonState2 = 0;
int buttonState3 = 0;
const int but1 = 22, but2 = 23, but3 = 24, r2 = 30, r4 = 32, r6 = 34, LED = 38, LED2 = 40;
unsigned char i;



byte p20[8] = {
  B10000,
  B10000,
  B10000,
  B10000,
  B10000,
  B10000,
  B10000,
  B10000,
};
byte p40[8] = {
  B11000,
  B11000,
  B11000,
  B11000,
  B11000,
  B11000,
  B11000,
  B11000,
};
byte p60[8] = {
  B11100,
  B11100,
  B11100,
  B11100,
  B11100,
  B11100,
  B11100,
  B11100,
};
byte p80[8] = {
  B11110,
  B11110,
  B11110,
  B11110,
  B11110,
  B11110,
  B11110,
  B11110,
};
byte p100[8] = {
  B11111,
  B11111,
  B11111,
  B11111,
  B11111,
  B11111,
  B11111,
  B11111,
};

void setup() {

  pinMode (in, INPUT);
  pinMode (out, OUTPUT);
  pinMode (inn, INPUT);
  pinMode (out2, OUTPUT);
  lcd.begin (16, 2);
  lcd.setCursor (0, 0);
  lcd.createChar (0, p20);
  lcd.createChar (1, p40);
  lcd.createChar (2, p60);
  lcd.createChar (3, p80);
  lcd.createChar (4, p100);
  Serial.begin (9600);
  lcd2.init();
  lcd2.backlight();
  pinMode (but1, INPUT);
  pinMode (but2, INPUT);
  pinMode (but3, INPUT);
  pinMode (r2, OUTPUT);
  pinMode (r4, OUTPUT);
  pinMode (r6, OUTPUT);
  pinMode (LED, OUTPUT);
  pinMode (LED2, OUTPUT);
  lcd2.setCursor(0, 0);
  lcd2.print("Sound T1:");
  lcd2.setCursor(10, 0);
  lcd2.print("Sound T2:");

}
void loop() {
  lcd.setCursor (0, 0);
  count1 ++;
  buttonState1 = digitalRead(but1);
  buttonState2 = digitalRead(but2);
  buttonState3 = digitalRead(but3);

  digitalWrite (LED, LOW);
  digitalWrite (LED2, LOW);
  digitalWrite (r2, HIGH);
  digitalWrite (r4, HIGH);
  digitalWrite (r6, HIGH);

  //sensor Tx, Rx and calc:
  digitalWrite(out, LOW);
  delayMicroseconds(2);

  digitalWrite(out, HIGH);
  delayMicroseconds(10);
  digitalWrite(out, LOW);
  float dur = pulseIn(in, HIGH);
  float dur_sec = dur / 1000000;
  float dis_time = dur_sec / 2;
  float ullage = dis_time * 34300;
  delay (10);

  //No.2 sensor Tx, Rx and calc:
  digitalWrite(out2, LOW);
  delayMicroseconds(2);

  digitalWrite(out2, HIGH);
  delayMicroseconds(10);
  digitalWrite(out2, LOW);
  float dur2 = pulseIn(inn, HIGH);
  float dur_sec2 = dur2 / 1000000;
  float dis_time2 = dur_sec2 / 2;
  float ullage2 = dis_time2 * 34300;
  delay (10);


  //percentage calc:
  long Rullage = ullage - 2;
  long per = x * Rullage;
  long finalper = per / 100;
  long sounding = x * ullage;
  long Finalsound = 125 - sounding;


  //No.2 percentage calc:
  long Rullage2 = ullage2 - 2;
  long per2 = x * Rullage2;
  long finalper2 = per2 / 100;
  long sounding2 = x * ullage2;
  long Finalsound2 = 125 - sounding2;


  //tank one and two write:
  //lcd2.setCursor(0, 0);
  //lcd2.print("Sound T1:");
  //lcd2.setCursor(10, 0);
  //lcd2.print("Sound T2:");
  lcd2.setCursor(0, 1);
  lcd2.print(String(Finalsound));
  lcd2.print("  %  ");
  lcd2.setCursor(10, 1);
  lcd2.print(String(Finalsound2));
  lcd2.print("  %  ");
  delay (50);


  //write info:
  //lcd.print(ullage);
  //lcd.print(" - ");
  //lcd.print(per);
  //lcd.print(" %  ");
  //lcd.setCursor(0, 1);

  //testing part and print bar
  lcd.setCursor (0, 0);
  double a = length / 100 * Finalsound;
  if (a >= 1) {
    for (int i = 1; i < a; i++) {
      lcd.write(4);
      b = i;
    }
    a = a - b;
  }
  peace = a * 5;



  switch (peace) {
    case 0:
      break;
    case 1:
      lcd.print((char)0);
      break;
    case 2:
      lcd.write(1);
      break;
    case 3:
      lcd.write(2);
      break;
    case 4:
      lcd.write(3);
      break;
  }


  for (int i = 0; i < (length - b); i++) {
    lcd.print (" ");
    delay (50);

  }

  //No.2 testing part and print bar
  lcd.setCursor (0, 1);
  double a2 = length / 100 * Finalsound2;
  if (a2 >= 1) {
    for (int i2 = 1; i2 < a2; i2++) {
      lcd.write(4);
      b2 = i2;
    }
    a2 = a2 - b2;
  }
  peace2 = a2 * 5;



  switch (peace2) {
    case 0:
      break;
    case 1:
      lcd.print((char)0);
      break;
    case 2:
      lcd.write(1);
      break;
    case 3:
      lcd.write(2);
      break;
    case 4:
      lcd.write(3);
      break;
  }


  for (int i2 = 0; i2 < (length - b2); i2++) {
    lcd.print (" ");
    delay (50);

  };

  //100% port tank

  if (buttonState1 == HIGH) {
    digitalWrite (LED, HIGH);
    digitalWrite (r2, LOW);
    digitalWrite (LED, HIGH);
    delay (1000);
  
  while (finalper == 95) {
    //sensor Tx, Rx and calc:
    digitalWrite(out, LOW);
    delayMicroseconds(2);
    digitalWrite(out, HIGH);
    delayMicroseconds(10);
    digitalWrite(out, LOW);
    float dur = pulseIn(in, HIGH);
    float dur_sec = dur / 1000000;
    float dis_time = dur_sec / 2;
    float ullage = dis_time * 34300;
    delay (10);
    //percentage calc:
    long Rullage = ullage - 2;
    long per = x * Rullage;
    long finalper = per / 100;
    long sounding = x * ullage;
    long Finalsound = 125 - sounding;
    digitalWrite (r2, HIGH);
    digitalWrite (r4, HIGH);
  }
  }


}

This bit is strange and broken:

   while (finalper == 95)
    {
      //sensor Tx, Rx and calc:
      digitalWrite(out, LOW);
      delayMicroseconds(2);
      digitalWrite(out, HIGH);
      delayMicroseconds(10);
      digitalWrite(out, LOW);
      float dur = pulseIn(in, HIGH);
      float dur_sec = dur / 1000000;
      float dis_time = dur_sec / 2;
      float ullage = dis_time * 34300;
      delay (10);
      //percentage calc:
      long Rullage = ullage - 2;
      long per = x * Rullage;
      long finalper = per / 100;
      long sounding = x * ullage;
      long Finalsound = 125 - sounding;
      digitalWrite (r2, HIGH);
      digitalWrite (r4, HIGH);
    }

I'm not sure what it's supposed to do, fill the port tank to 100% perhaps (from the comment). But it loops while finalper is 95. I'd expect it to loop until finalper becomes 95. Trouble is, you calculate finalper inside the loop but you declare a new copy of the variable to work with so it will never affect the while loop anyway.

You really need some decent names and to refactor some of the code into some functions again with decent names and maybe people can get a better idea what you're doing. Right now, it's too much like hard work.

Yes that part was supposed to fill up to around 100% i moved the percentage down because 95% was much more reliable to reach for the sensors, and the equal to was a mistake i have change to ">". when talking about the finalper inside the loop i thought i would have to have sensor code and calculations inside the loop so it could measure and calculate it to see if it had reached the value or not, although obviously i may be wrong.
thank you for your help
ive also renamed parts of the code so its more easy to read and understand whats going on a little bit more.

#include <LiquidCrystal.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal lcd(2, 3, 4, 5, 6, 7);
LiquidCrystal_I2C lcd2(0x27, 20, 4);
#define length 16.0
double percent = 100.0;
unsigned char b, b2, peace, peace2, i;
const int in = 8, out = 9, x = 10, inn = 11, out2 = 12;
int count1 = 0;
int buttonState1 = 0, buttonState2 = 0, buttonState3 = 0;
const int port100 = 22, port80 = 23, port60 = 24, pumpin = 30, pumpoutport = 32, pumpoutstbd = 34, testled1 = 38, testled2 = 40, solenoidport = 42, solenoidstbd = 44;


// custum characters for percentage bar
byte p20[8] = {
  B10000,
  B10000,
  B10000,
  B10000,
  B10000,
  B10000,
  B10000,
  B10000,
};
byte p40[8] = {
  B11000,
  B11000,
  B11000,
  B11000,
  B11000,
  B11000,
  B11000,
  B11000,
};
byte p60[8] = {
  B11100,
  B11100,
  B11100,
  B11100,
  B11100,
  B11100,
  B11100,
  B11100,
};
byte p80[8] = {
  B11110,
  B11110,
  B11110,
  B11110,
  B11110,
  B11110,
  B11110,
  B11110,
};
byte p100[8] = {
  B11111,
  B11111,
  B11111,
  B11111,
  B11111,
  B11111,
  B11111,
  B11111,
};

void setup() {
  //pinmode for ultrasonic sensor
  pinMode (in, INPUT);
  pinMode (out, OUTPUT);
  pinMode (inn, INPUT);
  pinMode (out2, OUTPUT);
  //starting lcd and creating custum characters
  lcd.begin (16, 2);
  lcd.setCursor (0, 0);
  lcd.createChar (0, p20);
  lcd.createChar (1, p40);
  lcd.createChar (2, p60);
  lcd.createChar (3, p80);
  lcd.createChar (4, p100);
  Serial.begin (9600);
  lcd2.init();
  lcd2.backlight();
  pinMode (port100, INPUT);
  pinMode (port80, INPUT);
  pinMode (port60, INPUT);
  pinMode (pumpin, OUTPUT);
  pinMode (pumpoutport, OUTPUT);
  pinMode (pumpoutstbd, OUTPUT);
  pinMode (testled1, OUTPUT);
  pinMode (testled2, OUTPUT);
  lcd2.setCursor(0, 0);
  lcd2.print("Sound T1:");
  lcd2.setCursor(10, 0);
  lcd2.print("Sound T2:");

}
void loop() {
  //set up for the main loop
  lcd.setCursor (0, 0);
  count1 ++;
  buttonState1 = digitalRead(port100);
  buttonState2 = digitalRead(port80);
  buttonState3 = digitalRead(port60);

  //reseting everything making sure everything is back to nuetral setup

  digitalWrite (testled1, LOW);
  digitalWrite (testled2, LOW);
  digitalWrite (pumpin, HIGH);
  digitalWrite (pumpoutport, HIGH);
  digitalWrite (pumpoutport, HIGH);

  //No.1 sensor Tx, Rx and calculation:
  digitalWrite(out, LOW);
  delayMicroseconds(2);

  digitalWrite(out, HIGH);
  delayMicroseconds(10);
  digitalWrite(out, LOW);
  float dur = pulseIn(in, HIGH);
  float dur_sec = dur / 1000000;
  float dis_time = dur_sec / 2;
  float ullage = dis_time * 34300;
  delay (10);
  
  //ullage = distance between bottom of the tank and top of the liquid,
  //sounding = distance between top of liquid and top of the tank,
  //No.1 percentage calculation:
  long Rullage = ullage - 2;
  long per = x * Rullage;
  long finalper = per / 100;
  long sounding = x * ullage;
  long Finalsound = 125 - sounding;

  //No.2 sensor Tx, Rx and calculation:
  digitalWrite(out2, LOW);
  delayMicroseconds(2);

  digitalWrite(out2, HIGH);
  delayMicroseconds(10);
  digitalWrite(out2, LOW);
  float dur2 = pulseIn(inn, HIGH);
  float dur_sec2 = dur2 / 1000000;
  float dis_time2 = dur_sec2 / 2;
  float ullage2 = dis_time2 * 34300;
  delay (10);

  //No.2 percentage calculationc:
  long Rullage2 = ullage2 - 2;
  long per2 = x * Rullage2;
  long finalper2 = per2 / 100;
  long sounding2 = x * ullage2;
  long Finalsound2 = 125 - sounding2;

  //writing sounding variable into the correct position on lcd screen

  lcd2.setCursor(0, 1);
  lcd2.print(String(Finalsound));
  lcd2.print("  %  ");
  lcd2.setCursor(10, 1);
  lcd2.print(String(Finalsound2));
  lcd2.print("  %  ");
  delay (50);

  //code for the percentage bars for visula representation of percentage filled
  //Tank No.1 Percentage bar counting and printing to lcd screen

  lcd.setCursor (0, 0);
  double a = length / 100 * Finalsound;
  if (a >= 1) {
    for (int i = 1; i < a; i++) {
      lcd.write(4);
      b = i;
    }
    a = a - b;
  }
  peace = a * 5;
  switch (peace) {
    case 0:
      break;
    case 1:
      lcd.print((char)0);
      break;
    case 2:
      lcd.write(1);
      break;
    case 3:
      lcd.write(2);
      break;
    case 4:
      lcd.write(3);
      break;
  }
  for (int i = 0; i < (length - b); i++) {
    lcd.print (" ");
    delay (50);
  }

  //Tank No.2 Percentage bar counting and printing to lcd screen

  lcd.setCursor (0, 1);
  double a2 = length / 100 * Finalsound2;
  if (a2 >= 1) {
    for (int i2 = 1; i2 < a2; i2++) {
      lcd.write(4);
      b2 = i2;
    }
    a2 = a2 - b2;
  }
  peace2 = a2 * 5;
  switch (peace2) {
    case 0:
      break;
    case 1:
      lcd.print((char)0);
      break;
    case 2:
      lcd.write(1);
      break;
    case 3:
      lcd.write(2);
      break;
    case 4:
      lcd.write(3);
      break;
  }
  for (int i2 = 0; i2 < (length - b2); i2++) {
    lcd.print (" ");
    delay (50);
  };

  // code to fill and empty tanks at intervals of 20%, so 100%, 80%, 60%, 40%, 20%, 0%
  //100% port tank

  if (buttonState1 == HIGH) {
    digitalWrite (testled1, HIGH);
    digitalWrite (pumpin, LOW);
    digitalWrite (testled1, HIGH);
    delay (1000);

    while (finalsound >= 95) {
      //sensor Tx, Rx and calc:
      digitalWrite(out, LOW);
      delayMicroseconds(2);
      digitalWrite(out, HIGH);
      delayMicroseconds(10);
      digitalWrite(out, LOW);
      float dur = pulseIn(in, HIGH);
      float dur_sec = dur / 1000000;
      float dis_time = dur_sec / 2;
      float ullage = dis_time * 34300;
      delay (10);
      //percentage calc:
      long Rullage = ullage - 2;
      long per = x * Rullage;
      long finalper = per / 100;
      long sounding = x * ullage;
      long Finalsound = 125 - sounding;
      digitalWrite (pumpin, HIGH);
      digitalWrite (solenoidstbd, HIGH);
    }
  }
}

The names are a help indeed. Although there is still some work to do there cough a, a2, b, b2 cough.

This is still broken:

  if (buttonState1 == HIGH)
  {
    digitalWrite (testled1, HIGH);
    digitalWrite (pumpin, LOW);
    digitalWrite (testled1, HIGH);
    delay (1000);
    while (finalsound >= 95)
    {
      //sensor Tx, Rx and calc:
      digitalWrite(out, LOW);
      delayMicroseconds(2);
      digitalWrite(out, HIGH);
      delayMicroseconds(10);
      digitalWrite(out, LOW);
      float dur = pulseIn(in, HIGH);
      float dur_sec = dur / 1000000;
      float dis_time = dur_sec / 2;
      float ullage = dis_time * 34300;
      delay (10);
      //percentage calc:
      long Rullage = ullage - 2;
      long per = x * Rullage;
      long finalper = per / 100;
      long sounding = x * ullage;
      long Finalsound = 125 - sounding;
      digitalWrite (pumpin, HIGH);
      digitalWrite (solenoidstbd, HIGH);
    }
  }

That loop doesn't change finalsound. It does declare a different variable called Finalsound - case matters. But even if the name was the same, this:

      long Finalsound = 125 - sounding;

declares a new variable. Fix the case and get rid of the long there.

Also, if the aim is to fill the tank, shouldn't the loop be:

    while (finalsound <= 95)

I don't get what you're doing with the pump either. If the 100% button is pressed for port it looks like the pumpin is turned on but in the loop you turn it off. There's something happening with the starboard solenoid too, don't understand why.

It might help if you could describe how all the tanks and pumps and valves and buttons are connected and what they do.