# Volume of cylinder

Hello! I am using a HC-204 ultrasound sensor to get distance and with that, I am trying to find the height of water that can be filled in the tank. I am using volume of cylinder to find the volume of water. It however gets stuck at 50cmm cubed. Kindly do let me know what is the error in the code.

# define h 8

percent_empty = distance / h; sa = (1-percent_empty)*2*3.14*radius*height; volume = 3.14 * radius * radius * (1 - percent_empty); Serial.print(distance); Serial.println(" cm"); Serial.print(volume,5); Serial.println(" cm cubed"); delay(500);

Post all the code, using code tags ("</>" button).

Check your variable types. What is "sa" supposed to be ?

Is the tank standing upright? Or on its side?
(Yes, it makes a difference to the calculations.)

odometer: Is the tank standing upright? Or on its side?

More to the point, if the tank is upright, why do you need that stuff? There is only one variable, the height, and the rest are constant.

michinyon:
Check your variable types. What is “sa” supposed to be ?

The tank is upright.

jremington:
Post all the code, using code tags ("</>" button).

sa is for surface area, I haven’t used it anywhere…so can be ignored.
<
#define trigPin 13
#define echoPin 12
#define led 11
#define led2 10
#define h 8
void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(led, OUTPUT);
pinMode(led2, OUTPUT);
}
void loop() {
long duration, distance, volume, n, duration_empty,percent_empty,sa,h;

digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);

delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
h = height ;
percent_empty = distance / h;

Serial.print(distance);
Serial.println(" cm");
Serial.print(volume,5);
Serial.println(" cm cubed");
delay(500);
}

``````#define h 8
``````
``````long duration, distance, volume, n, duration_empty,percent_empty,sa,h;
``````

'h' ?

Oops.

Did you miss the code tags ?

All of the variables in your program are integers so the results of the calculations will be integers. Could this be causing the problem ?

OK, I'll make it a bit clearer: This is what you see `h = height ;` This is what the compiler sees `8 = height ;`

sp99:
The tank is upright.

Then calculate the cross-section area with your calculator and put it in your Arduino program as a constant.

``````volume = height * crossSectionArea;
``````

…R

`3.14 * radius * radius` is a good way of expressing the cross section area constant. It makes it obvious how it's calculated rather than just putting a bare number in.

Speaking of which, what is 29.1? Speed of sound? This really needs a named constant. When you define the constant, also note the units - meters per millisecond?

`h = height;` looks more like a comment than actual code.

Then the code as presented won't work because percent_empty is an integer but it seems like it's being used to hold values in the range zero to one. This will result in integer values of zero OR one. Either make it a floating-point variable or change the range so that it's holding integers zero to one hundred.

MorganS: `3.14 * radius * radius` is a good way of expressing the cross section area constant. It makes it obvious how it's calculated rather than just putting a bare number in.

Fair point. But put it at the top of the code - before setup()

Better still, do the calculation on a calculator for greater precision and add a comment that explains the calculation.

...R

The original requirement was stated as

I am using a HC-204 ultrasound sensor to get distance and with that, I am trying to find the height of water that can be filled in the tank.

If that requirement is really what the OP wants then volume does not come into it. If, however, he wants to know what volume of water can safely be added to the tank given the current water level then it does.

I await clarification as to the exact requirement,