Trouble with PING))) and 'if' statements

The Situation: I’m building a device that will use a PING))) sonic rangefinder to measure the level of liquid (wine) in a tank, and then print out the volume of the wine in the tank based on the measurement to the serial monitor. Since the tank has a different shape at different heights (cylindrical at the base and conical at the top) the program needs to use two different equations to determine the volume of wine in the tank depending on the distance from the top of the tank to the surface of the wine. The program uses one equation for when the distance to the wine is greater than 12 inches, and one for when it is less than 12 inches.

The Problem: The PING))) is working just fine, I’m able to get a usable signal from it and the program is successfully converting the microsecond pulses into inches. However, I’m so far unable to get the program to use an ‘if’ statement to pick which equation it needs to use to calculate the volume of wine in the tank. Instead of picking one or the other, it always uses the second ‘if’ statement in the sequence and ignores the first, regardless of whether or not the variable ‘inches’ is greater or less than 12. If you look at the attached code, the program will always use the equation “gallons = 2279 - ((inches - 12) * 24)”, and never the other one, regardless of the input from the PING))). The same goes for the other equation (after the first ‘if’ statement) if you put it second. I did remember to change the <> operators following the ‘if’ statements when I moved the equations around. Maybe the program is running through both equations for the variable ‘gallons’ and then just printing the second, most recent, result. I’ve tried using an ‘if/else’, but that wouldn’t compile, for some reason I got an error message saying the ‘else’ had no previous ‘if’. I’m new at this, so there is probably something simple I’ve misunderstood about 'if’s. Any help is appreciated!

Ping_volume_sketch.ino (706 Bytes)

Sorry, here’s my code.

``````void setup() {
Serial.begin(9600);

}

void loop() {

const int pingPin = 7;
float duration;
float inches;
float gallons;

pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);
pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);
inches = (duration / 74 / 2);

if
(inches < 12);
{
gallons = 2279 + (-.9 * pow(inches, 2)) + (.9 * inches) + 121;
}

if
(inches > 12);
{
gallons = 2279 - ((inches - 12) * 24);
}

Serial.print(inches);
Serial.print("in, ");
Serial.print("gal., ");
Serial.print(gallons);
Serial.println();
delay(1000);

}
``````

if(inches < 12);
AND
if(inches > 12);
Get rid of the ;

inches = (duration / 74 / 2);
USE
inches = (duration / 74.0 / 2.0);

That fixed it, thank you!