Determining the reading fluctuations in an analog sensor

Hello Everyone,

I am using a photo interrupter sensor which is analog based for detection of liquid drops. The reading before obstruction changes everytime i connect my sensor. Sometimes it starts with range of 400 and sometimes 700. But when the slot is obstructed, the analog value is fluctuated by 50-100. So, I want to record the fluctutation and everytime it happens i want to increament in the counter.

Note that fluctutations might be increamental or decreamental. I am usign the simple code below:

int sensorPin = A0; // select the input pin for LDR
int sensorValue = 0; // variable to store the value coming from the sensor
void setup() {
Serial.begin(9600); //sets serial port for communication
digitalWrite(sensorPin, LOW);
}
void loop() {
int sensorValue = analogRead(sensorPin); // read the value from the sensor
Serial.println(sensorValue); //prints the values coming from the sensor on the screen
delay(300);
}

How can i record the fluctutations?

I look forward to the insightful answers.

Best Regards
Rushan

It would certainly be helpful if your code used some white space between functions.

It would certainly be helpful if your code was properly indented.

How can i record the fluctutations?

Where do you want to record them? Should the Arduino be controlling a servo to which a chisel is attached, so that it can record the values by chiseling away stone?

I am sorry for the indention problem. The updated code is below:

int sensorPin = A0; // select the input pin for LDR

int sensorValue = 0; // variable to store the value coming from the sensor

void setup() {
    Serial.begin(9600); //sets serial port for communication
    
    digitalWrite(sensorPin, LOW);
}

void loop() {
    int sensorValue = analogRead(sensorPin); // read the value from the sensor
    
    Serial.println(sensorValue); //prints the values coming from the sensor on the screen
    
    delay(300);
}

I want to store the fluctuations in a counter variable. For expample if there are fluctuations whether increamentatl or decreamental, the counter variable should be increased by 1.

Can you guide me how to do that?

I want to store the fluctuations in a counter variable.

Sorry, that doesn't make sense. Are you trying to determine the magnitude of the fluctuations? Or are you trying to count the number of times the value fluctuates?

What, exactly, does fluctuate mean to you? How much variation in value, over what period of time, constitutes a "fluctuation"?

By fluctutation i mean a value difference of 50-100 in analog values. I want to measure the number of times the fluctutations occur. When an object passes through the sensor, the value changes. I want to count the number of times the value changes.

When an object passes through the sensor, the value changes.

I suspect that you have fluctuations in the value read from the sensor when there is no object present. I suspect that you have fluctuations in the value read from the sensor when there is an object present. I further suspect that there is a significant change in readings between no object present and an object present.

If that is the case, then the "fluctuation" you want to count is when the current reading is significantly larger (or smaller, depending on whether the presence of the object causes an increase or decrease) than the previous reading.

So, you need to keep track of the previous reading, and compare that to the current reading.

int prevReading;
int currReading;

int objects = 0;

void setup()
{
   prevReading = analogRead(somePin);
}

void loop()
{
   currReading = analogRead(somePin);
   if(abs(currReading - prevReading) > 50) // Is the change significant?
   {
      objects++;
   }
   prevReading = currReading;
}

One thing to note is that this doesn't actually count objects passing. There are two transitions - when an object first blocks the sensor and when the object last blocks the sensor.

You could count all the transitions, as this code does, and divide by two to get the number of objects, or you could, inside the if body, determine whether the delta is positive or negative, and only count the positive changes or the negative changes.

Thank you very much for the reply. I just have one small query. How can i track the current reading as the reading from the analog pin goes to the previous reading?

How can i track the current reading as the reading from the analog pin goes to the previous reading?

The analog pin reading goes to prevReading only in setup(). The analog pin reading goes to currReading in loop().