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.
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 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;
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.
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,