Go Down

Topic: Volume of cylinder (Read 199 times) previous topic - next topic

sp99

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 radius 4
#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);

jremington

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

michinyon

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

odometer

#3
Jul 14, 2015, 06:39 am Last Edit: Jul 14, 2015, 06:40 am by odometer Reason: clarification
Is the tank standing upright? Or on its side?
(Yes, it makes a difference to the calculations.)
Please see the attached picture.

Nick_Pyner

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.

sp99

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

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 radius 4
#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;
   volume = 3.14 * radius * radius * (1 - percent_empty);
 
   Serial.print(distance);
  Serial.println(" cm");
  Serial.print(volume,5);
  Serial.println(" cm cubed");
  delay(500);
}

AWOL

Code: [Select]
#define h 8
Code: [Select]
long duration, distance, volume, n, duration_empty,percent_empty,sa,h;

'h' ?

Oops.

Please post all your code.
Use code tags.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

UKHeliBob

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 ?
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

AWOL

OK, I'll make it a bit clearer:
This is what you see
Code: [Select]
h = height ;
This is what the compiler sees
Code: [Select]
8 = height ;
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Robin2

The tank is upright.

Then calculate the cross-section area with your calculator and put it in your Arduino program as a constant.
Code: [Select]
volume = height * crossSectionArea;


...R

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.

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.
GoForSmoke: "What GShield? You never mentioned a shield."

Robin2

#11
Jul 14, 2015, 12:39 pm Last Edit: Jul 14, 2015, 12:40 pm by Robin2
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

UKHeliBob

The original requirement was stated as
Quote
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,
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Go Up