Calculating Liquid Volume from Number of pulses

Hello everyone,

I am trying to calculate the liquid volume by counting the number of drops passing through a sensor. In a research paper, I read that one drop averages around 0.05ml. So, In the code below i try to count the number of drops and then measuring the total volume of liquid. But, when i trun the code and drops are passing, there is always 0 value displayed for liquid. Can anyone figure out what could be the problem? Code is below:

/*
  AnalogReadSerial
  Reads an analog input on pin 0, prints the result to the serial monitor.
  Graphical representation is available using serial plotter (Tools > Serial Plotter menu)
  Attach the center pin of a potentiometer to pin A0, and the outside pins to +5V and ground.

  This example code is in the public domain.
*/
int count=0;
int liquid=0;

// the setup routine runs once when you press reset:
void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
}

// the loop routine runs over and over again forever:
void loop() {
  // read the input on analog pin 0:
  int sensorValue = analogRead(A0);
  if(sensorValue>850)
  {
    count++;
    }
  //print out the value you read:
  liquid=count*0.05;
  Serial.println(liquid);
  delay(1500);        // delay in between reads for stability
}

I look forward to hearing from u all soon.

Can anyone figure out what could be the problem?

Two possible problems. The first, is that the sensor reading may never exceed 850.

The second one is that you are counting fewer that 20 drops every 1.5 seconds. If you count less than 20, even 19 * 0.05 is only 0.95, which, when truncated, becomes 0.

Thank you for your reply. I think the problem would be the 2nd one. I think increasing the delay might help or will i lose some data if i do that?

I think increasing the delay might help or will i lose some data if i do that?

Do you really need to volume in milliliters? If so, keep the type as integer. If not, changing the type to float or double would make the code work as written.

Yes thats what i was thinking. I have one more question related to this. If i say i want count to be 0 if the reading do not change at all for 30 seconds. Then how can i apply this condition?

Why are you using analogread if you are counting pulses ?

The sensor is analog, what i am doing is that if there is a fluctutation in the analog reading, the an object has passed through the sensor. So, one fluctuation will be added 1 to the counter. Now, the problem is, i want to add 0 to the counter if the analog value does not change for 30 seconds or it remains within the +-10 range. how can i do that?