Analog count

Hi, I’m currently working on a project which requires me to detect and count the number of droplet flow within a channel. I’m using IR sensor to detect the presence and the output of the sensor is connected to analog pin. So far, I only managed to detect the presence of the droplet (which alternately water and air) but the counting is not working.

This is my coding,

int analog=A5;
int sensorValue;
int count=0;

#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup(){

pinMode (analog, INPUT);
Serial.begin(9600);

// set up the LCD’s number of columns and rows:
lcd.begin(16, 2);
lcd.setCursor(0,0);
lcd.print(“Droplet counting”);

}

void loop(){

sensorValue = analogRead(analog);

Serial.println(sensorValue);

delay(1000);

if(sensorValue >= 50)
{
lcd.setCursor(0, 1);
//lcd.print(millis()/1000);
//lcd.noDisplay();

count = count++;

lcd.print(count);

}

else if (sensorValue < 50)
{
lcd.clear();

//value = val;
}

}

Any idea? Thanks.

ummisz: the counting is not working.

What does that actually mean?

If the drops are less than a second apart, it won't be seeing them during the delay(1000).

If the time it takes for a drop to pass the sensor is slower than the speed of loop() then you'll get multiple drops counted per drop, since the pin will still be >50 next time through although it's the same drop.

In that case, have a look at Button State Change, where it records the fact that the button became pressed as distinct from is pressed: that's what you need for your drop- you need to know the drop just arrived, and isn't the old drop from last time through loop().

  delay(1000);

Get rid of this crap. You need to detect a change in state of the analog pin - when the value changes significantly. You can't do that with your thumb up your ass in a delay().

   count = count++;

This is more crap. The behavior of this statement is undefined.

   count++;

is what you want!

JimboZA: What does that actually mean?

The counter is supposed to start counting when a droplet is detected by the IR sensor and yet nothing happen.

And yes, I have applied the button state change concept to count the droplet - I set the analog A5 as the input. but it doesn't work they way I hope it does. Can I do it without using push button?

PaulS:   count = count++;

This is more crap. The behavior of this statement is undefined.

   count++;

is what you want!

I did this before but the counter will keep counting everytime the value is >50, even if there is no droplet passing the IR.

Do you think I can apply state change detection here? Using analog instead of digital?

You want either

count=count+1 ;

or

count++ ;

Either of these is fine, what you had is wrong.

I did this before but the counter will keep counting everytime the value is >50, even if there is no droplet passing the IR.

Because that is what you wrote. You need to detect when the value goes from below 50 to above 50. You count ONCE when that happens. Then, you ignore the sensor until the value goes below 50 again.

Do you think I can apply state change detection here? Using analog instead of digital?

Obviously.