How to use if / else / then to choose from list of choices

Hi all, I have this code that measures and reports the level of fuel in a 275 vertical tank with rounded top and bottom. So because the depth readings will not be linear at the top and bottom I have a list of depth to gallon readings. But I don't quite know how to code so it makes a choice from a 44 line long list.
Thanks, Tom

/*
 * fuel_level.ino
 * using  NodeMCU-12E v3 ESP8266 WiFi board and Waterproof HC-SR04 type ultrasonic sounders to measure the volume of fuel in vertical fuel tank.
 * The sonar pings for the top of the fuel, gets the distance in inches, 
 * calculates depth of fuel as in and then calculates volume of fuel as gallons.
 * 
 * 
 *  pin mapping is different on these boards - CAUTION. Pin numbers are generally GPIO numbers
 *  
 */

    #include <ESP8266WiFi.h>

    // Set password to "" for open networks.
 const char * ssid = "ASUS" ;                     //local wifi network SSID
 const char * password = "********" ;            //local network password

 WiFiServer server (80);        
    
    //** CHANGE TO SUIT TANK DIMENSIONS
    const int Depth = 50;                         //total depth of tank in inches, from sensor to base inside 
    const unsigned int Period = 2000;             //period between pings, in milliseconds. i.e  1 munute = 60,000. max 65535. Want longer? use unsigned long
    
    //** SENSOR PINS
    const int trigPin = 5;                           // GPIO5,  D1 
    const int echoPin = 4;                           // GPIO4,  D2
   
    // Global variables
    int Gallons;
    int Distance;
    int FuelDepth;
    int duration;

 void setup () {
   Serial.begin (115200);
  pinMode(trigPin, OUTPUT); // Initializing Trigger Output and Echo Input 
  pinMode(echoPin, INPUT_PULLUP);   
 
 // Connect to the Wi-Fi network
   Serial.println ();
   Serial.println ();
   Serial.print ( "Connecting with" );
   Serial.println (ssid);
 
   WiFi.begin (ssid, password);
 
   while (WiFi.status () != WL_CONNECTED) {
     delay (500);
     Serial.print ( "." );
   }
   Serial.println ( "" );
   Serial.println ( "Connected with WiFi." );
 
   // Start of the Web Server
   server.begin ();
   Serial.println ( "Web server started." );
 
   // This gets the IP address
   Serial.print ( "This is the IP to connect:" );
   Serial.print ( "http: //" );
   Serial.print (WiFi.localIP ());
   Serial.println ( "/" );
 }

void loop() {

  digitalWrite(trigPin, LOW);
 
  delayMicroseconds(5);
 
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
 
  duration = pulseIn(echoPin, HIGH);
 
  Distance = duration / 127.000;

  //***********Reading Fuel Tank
 
    if (Distance >= Depth || Distance == 0 )  Distance = Depth;      //check it does not go negative
   
    FuelDepth = Depth - Distance;

    delay(50);

    //    if (FuelDepth == 44, Gallons = 275);
    //    if (FuelDepth == 43, Gallons = 272);
    //    if (FuelDepth == 42, Gallons = 269);
    //    etc
      
    delay(50);
    
   // Check if a client has been connected
   WiFiClient client = server.available ();
   if (client) {                            // if you get a client,
    Serial.println("new client");           // print a message out the serial port
    String currentLine = "";                // make a String to hold incoming data from the client
    while (client.connected()) {            // loop while the client's connected
      if (client.available()) {             // if there's bytes to read from the client,
        char c = client.read();             // read a byte, then
        Serial.write(c);                    // print it out the serial monitor
        if (c == '\n') {                    // if the byte is a newline character

          // if the current line is blank, you got two newline characters in a row.
          // that's the end of the client HTTP request, so send a response
          
          if (currentLine.length() == 0) {                    // Check to see if the client request was just the IP address if it was just refresh
            
            client.println("HTTP/1.1 200 OK");                // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
            client.println("Content-Type:text/html");         // and a content-type so the client knows what's coming, then a blank line
            client.println("");                               // The HTTP response ends with another blank line:


      // sends to network. works.         
           // print out the fuel depth.
        client.print("fuel");   
        client.println(FuelDepth);                  // send to client
        client.println();                          // print blank line
        delay(1);                                  // delay in between reads for stability
        
            
            break;                                 // break out of the while loop:
          }
          else {      
            currentLine = "";                      // if you got a newline, then clear currentLine:
          }
        }
        else if (c != '\r') {    // if you got anything else but a carriage return character,
          currentLine += c;      // add it to the end of the currentLine
        }
        }

      }
    }

    pinMode(13, OUTPUT);                          // LED GPIO13 D7
   
    digitalWrite(13, HIGH);                                          //flash the LED on D7, just to let us know it's running
    delay(50);
    digitalWrite(13, LOW);

//************************* can be commented out, test use only
    Serial.println();
    Serial.println();
    Serial.println("Tank fuel distance: " + String(Distance));       //print distance
    Serial.println("Tank fuel depth: " + String(FuelDepth));         //print depth
    Serial.println("Tank gallons: " + String(Gallons));              //print gallons
//***********************************************   

    delay(10);
  
//**    can be commented out, test only
    Serial.begin(115200);                                               // Open serial console. 
    Serial.println();
//*******************************    

    delay(2000);


    client.stop();    // close the connection:
    Serial.println("client disconnected");
    }

tom_bauer:
But I don't quite know how to code so it makes a choice from a 44 line long list.

Maybe you could use a look-up table. This very simple example should illustrate the idea

myVolumeList[] = {12, 37, 45, 78};

// assuming the depth readings can be reduced to a series of numbers 0 to 3
// and assume, for example, that the current reading gives a value 2
// then the volume can be obtained like this

currentVolume = myVolumeList[myCurrentReadingNumber];

// the variable currentVolume will have the value 45

...R

What about this concept (pseudo code...)?

if(depth < min_linear_hight){
  switch( depth )
    case 1:
       //one inch volume code;
    break;
    case 2:
       //two inch volume code;
    break;


else if((depth >= min_linear_hight) && depth <= max_linear_hight)
{

 //linear conversion code
}

else{  // near the top....

  switch(depth)

     case 42:

     break;

     case 43:

     break;

     case 44:

     break;
  }
}

Hi, OK well I don't quite grasp those so far!! But here is the list I have which may help understand.
Tom:

    if (FuelDepth = 44, Gallons = 275);
    if (FuelDepth = 43, Gallons = 272);
    if (FuelDepth = 42, Gallons = 269);
    if (FuelDepth = 41, Gallons = 265);
    if (FuelDepth = 40, Gallons = 260);
    if (FuelDepth = 39, Gallons = 254);
    if (FuelDepth = 38, Gallons = 249);
    if (FuelDepth = 37, Gallons = 243);
    if (FuelDepth = 36, Gallons = 236);
    if (FuelDepth = 35, Gallons = 230);
    if (FuelDepth = 34, Gallons = 223);
    if (FuelDepth = 33, Gallons = 216);
    if (FuelDepth = 32, Gallons = 208);
    if (FuelDepth = 31, Gallons = 201);
    if (FuelDepth = 30, Gallons = 194);
    if (FuelDepth = 29, Gallons = 187);
    if (FuelDepth = 28, Gallons = 180);
    if (FuelDepth = 27, Gallons = 173);
    if (FuelDepth = 26, Gallons = 166);
    if (FuelDepth = 25, Gallons = 158);
    if (FuelDepth = 24, Gallons = 151);
    if (FuelDepth = 23, Gallons = 144);
    if (FuelDepth = 22, Gallons = 137);
    if (FuelDepth = 21, Gallons = 130);
    if (FuelDepth = 20, Gallons = 123);
    if (FuelDepth = 19, Gallons = 115);
    if (FuelDepth = 18, Gallons = 108);
    if (FuelDepth = 17, Gallons = 101);
    if (FuelDepth = 16, Gallons = 94);
    if (FuelDepth = 15, Gallons = 87);
    if (FuelDepth = 14, Gallons = 80);
    if (FuelDepth = 13, Gallons = 72);
    if (FuelDepth = 12, Gallons = 65);
    if (FuelDepth = 11, Gallons = 58);
    if (FuelDepth = 10, Gallons = 51);
    if (FuelDepth = 9, Gallons = 44);
    if (FuelDepth = 8, Gallons = 37);
    if (FuelDepth = 7, Gallons = 31);
    if (FuelDepth = 6, Gallons = 25);
    if (FuelDepth = 5, Gallons = 19);
    if (FuelDepth = 4, Gallons = 14);
    if (FuelDepth = 3, Gallons = 9);
    if (FuelDepth = 2, Gallons = 5);
    if (FuelDepth = 1, Gallons = 2);

Not the most elegant engineering solution but You are in control of things. What's left to do to finish the project?

tom_bauer:
Hi, OK well I don't quite grasp those so far!! But here is the list I have which may help understand.
Tom:

    if (FuelDepth = 44, Gallons = 275);

if (FuelDepth = 43, Gallons = 272);
    if (FuelDepth = 42, Gallons = 269);
    if (FuelDepth = 41, Gallons = 265);
    if (FuelDepth = 40, Gallons = 260);
    if (FuelDepth = 39, Gallons = 254);
    if (FuelDepth = 38, Gallons = 249);
    if (FuelDepth = 37, Gallons = 243);
    if (FuelDepth = 36, Gallons = 236);
    if (FuelDepth = 35, Gallons = 230);
    if (FuelDepth = 34, Gallons = 223);
    if (FuelDepth = 33, Gallons = 216);
    if (FuelDepth = 32, Gallons = 208);
    if (FuelDepth = 31, Gallons = 201);
    if (FuelDepth = 30, Gallons = 194);
    if (FuelDepth = 29, Gallons = 187);
    if (FuelDepth = 28, Gallons = 180);
    if (FuelDepth = 27, Gallons = 173);
    if (FuelDepth = 26, Gallons = 166);
    if (FuelDepth = 25, Gallons = 158);
    if (FuelDepth = 24, Gallons = 151);
    if (FuelDepth = 23, Gallons = 144);
    if (FuelDepth = 22, Gallons = 137);
    if (FuelDepth = 21, Gallons = 130);
    if (FuelDepth = 20, Gallons = 123);
    if (FuelDepth = 19, Gallons = 115);
    if (FuelDepth = 18, Gallons = 108);
    if (FuelDepth = 17, Gallons = 101);
    if (FuelDepth = 16, Gallons = 94);
    if (FuelDepth = 15, Gallons = 87);
    if (FuelDepth = 14, Gallons = 80);
    if (FuelDepth = 13, Gallons = 72);
    if (FuelDepth = 12, Gallons = 65);
    if (FuelDepth = 11, Gallons = 58);
    if (FuelDepth = 10, Gallons = 51);
    if (FuelDepth = 9, Gallons = 44);
    if (FuelDepth = 8, Gallons = 37);
    if (FuelDepth = 7, Gallons = 31);
    if (FuelDepth = 6, Gallons = 25);
    if (FuelDepth = 5, Gallons = 19);
    if (FuelDepth = 4, Gallons = 14);
    if (FuelDepth = 3, Gallons = 9);
    if (FuelDepth = 2, Gallons = 5);
    if (FuelDepth = 1, Gallons = 2);

That is a lot of code.

The following has the same effect, but is shorter.

FuelDepth = 1;
Gallons = 2;

Can You explain that in detail?

Railroader:
Can You explain that in detail?

The ifs don't control anything and the tested values are all lists of assignments.

The last win.

Haha, I get it.... OP should use ==, not =. I missed that. I think You twisted things even more, not helping OP exactly.....

const uint16_t transTab[] = {
  275, 272, 269, 265, 260, 254, 249, 243, 236, 230, 223, 216, 208, 201, 194, 187,
  180, 173, 166, 158, 151, 144, 137, 130, 123, 115, 108, 101, 94, 87, 80, 72,
  65, 58, 51, 44, 37, 31, 25, 19, 14, 9, 5, 2,
};

uint16_t depthToGallons(uint8_t depth) {
  return transTab[44 - depth];
}

That's the next level progmem saving version, next to my earlier suggestion.... Joking!

I did not PROGMEM it because

 using  NodeMCU-12E v3 ESP8266 WiFi .....

What ever memory..., it's an elegant solution but maybe a bit more advanced for a new member.
Better a clumsy, easily understandable, working code than a sophisticated not working code.

Whandall:
I did not PROGMEM it because

By the way, being not at all an expert in Arduinos (UNOs)....

I use the construction "#define inputPin n" instead of "int inputPin n". A comment?

The input values are consecutive, so a simple list solution is simple and easy to understand.

I have the table backwards, because I edited it from the 'if' cluster and was too lazy to reverse them.

Railroader:
By the way, being not at all an expert in Arduinos (UNOs)....

I use the construction "#define inputPin n" instead of "int inputPin n". A comment?

The first could compile, the second will not.

For constant pins I would use

const uint8_t InputPin = 10;

I'm beginning to be sorry I asked since no one is really willing to help.

It not even "could compile", it does and it works. A sample used in a working project:

#define leftSensorPin A0
#define rightSensorPin A1
#define speed_pin A2
#define leftStopPin 10//4//12
#define leftReturnSlowPin 11
#define leftReturnFastPin 12//4
#define PWM1_pin 5
#define PWM2_pin 6
#define rightStopPin 3
#define rightReturnPin 2
#define waistPin 9
#define fwdPin 8
#define bwdPin 4
#define clearPin 7

Yes, it needs to be "int InputPin = 10;" I'll oil my "=" key....

Anyway, what's the difference regarding Arduino memory usage? I think: No difference. Defining an int uses memory somewhere.

Sorry OP, sidetracking Your post. Covid effect?

In your special application* 'if' is not the right tool for the job.

*(nonlinear translation of values with consecutive input range)

Make a list of all the numbers and use the input number as an index in the list.

Your depth values run from 1 to 44, the list happens to have the biggest values first,
so the value for 44 has position 0, for 43 position 1, ...., which makes the index (44 - input number).

const uint16_t transTab[] = {
  275, 272, 269, 265, 260, 254, 249, 243, 236, 230, 223, 216, 208, 201, 194, 187,
  180, 173, 166, 158, 151, 144, 137, 130, 123, 115, 108, 101, 94, 87, 80, 72,
  65, 58, 51, 44, 37, 31, 25, 19, 14, 9, 5, 2,
};

uint16_t depthToGallons(uint8_t depth) {
  return transTab[44 - depth];
}

Railroader:
It not even "could compile", it does and it works. A sample used in a working project:

Anyway, what's the difference regarding Arduino memory usage? I think: No difference. Defining an int uses memory somewhere.

I said it could because that depends on the context. The second had no chance in any context.

Defining a const int or whatever can require memory, but usually does not.

It's safer, because it attaches a type and a name to a value, just like a normal variable.

#defines are macros, with all the pros and cons (more cons IMHO).

tom_bauer:
I'm beginning to be sorry I asked since no one is really willing to help.

Well, not really. The very first reply, #1, was a direct solution on a silver platter.