I am reading analog values and storing the fluctuations in the reading to a count variable. For example if there is a fluctuation and reading goes above 450, 1 is added to the count.
Now what i want to do is that if the reading does not change for a period of 30 seconds or it just fluctuates 10-20 from the current reading, 0 should be added to the count. How can I do that?
MY code is below:
int sensorPin = A0; // select the input pin for LDR
int sensorValue = 0;
int prevReading = 0;// variable to store the value coming from the sensor
int currReading = 0;
int count=0;
void setup() {
Serial.begin(9600); //sets serial port for communication
prevReading = analogRead(sensorPin);
}
void loop() {
sensorValue = analogRead(sensorPin);
Serial.println(sensorValue); //prints the values coming from the sensor on the screen
delay(300);
}
Thank You very much for your reply. I do have insert all my code within this while loop right? And actually i want to track the current reading of the sensor and if it remains within the boundary of +-20, 0 should be added to the count. how can i achieve that?
I do have insert all my code within this while loop right?
Probably not. What I'd do is let loop() iterate until a change in reading happens. Then, enter the while loop, to look at that happens over the next 30 seconds. You might want, for instance, to break out of the loop if a drop is counted, starting the clock over again.
Adding 0 to the count is silly. If nothing happens (i.e. the difference between the current reading and the previous reading is less than 20), do nothing.
Thank you very much. I still have one confusion, how can i track that reading has remained same over the period of 30 seconds with a fluctuation of just small numbers like 0-20? like for example, the reading has remained within the range of 400-420 for 30 seconds? usually when an object passes through the sensor, the fluctuation is high, usually more than 50.
I still have one confusion, how can i track that reading has remained same over the period of 30 seconds with a fluctuation of just small numbers like 0-20?
What you do is break out of the while loop is the variation exceeds 20, possibly doing something before breaking (like counting the drop...).
I am trying different things and below is my updated code:
/*
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;
float liquid=0;
unsigned long startTime=millis();
// 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>450)
{
count++;
}
while(millis()-startTime<15000)
{
if((sensorValue >= sensorValue-10) ||(sensorValue>= sensorValue+20))
{
count=count+0;
}
}
//print out the value you read:
liquid=count*0.05;
Serial.println(count);
delay(300); // delay in between reads for stability
}
bool inRange(int val, int minimum, int maximum)
{
return ((minimum <= val) && (val <= maximum));
}
can you tell me with a code snipped what exactly i need to do? It would be really helpful.
sensorValue will always be greater than sensorValue-10. sensorValue will never be greater than, or equal, sensorValue+20.
count=count+0;
How is this useful?
I'm not clear on what the 30 second loop is supposed to do. If you are counting drops for 30 seconds, that's one thing. If you are looking for a 30 second window in which there are no drops, that's another thing.
What i want to achive is that if the sensorValue remains in a 30 values window for 30 seconds (eg. 450-480) then nothing should be added to te count but, otherwise if there are more fluctuations then 1 should be added to count variable.
Can you tell me with a short code snippet how can i achieve that?
I will tell you the scenario, maybe that would be helpful.
Reading starts at 400 and the objects start passing through the sensor and the reading fluctuates and rises to 500 then 600 and when the frequency of object passing decreases the readings comes down to 480, 470 and then the object stops passing and the reading comes to 450 or 420 and stays there for a speicific time say 30 seconds or even more. So, now the reading is 420 now but the objects are not passing so the count should not be increased. and if the object start passing again and reading value increases and count should be increased again.
that last part is what i want to address. Now, can you propose a solution for this?
What are you counting? Using what kind of sensor? Your variations when there is nothing in front of the sensor seem too large. The difference between readings when there is, and is not, something in front of the sensor seem too small.
But, the difference IS large enough to detect. With the code I posted in your other thread, that you butchered and posted at the start of this thread, you could detect something passing in front of the sensor, and that thing passing out from in front of the sensor.
You could record when the difference between the current reading and the previous reading exceeds some threshold that indicates that there the thing has passed the sensor.
You could, periodically (on every pass through loop()), see how long it has been since the last thing passed the sensor.
Do whatever it is you need to do (that is still not clear) when the last thing passed by more than 30 seconds ago.
You use millis() to record when events happen and to determine what "time" it is now.
Here is some code that may or may not do what you want, it's difficult to tell from your description. If you want to use it as a starting point you can follow the comments to see what it's doing, debug it if it does not do what you want and change it as necessary.
int sensorPin = A0; // select the input pin for LDR
int sensorValue = 0;
int prevReading = 0;// variable to store the value coming from the sensor
int currReading = 0;
int count = 0;
int minReading = 450; // minimum reading needed to count
int minFluctuation = 20; // minimum fluctuation to count
void setup() {
Serial.begin(9600); //sets serial port for communication
prevReading = analogRead(sensorPin);
// use this if you want to wait until reading
// reaches a threshold, else delete it
while ( analogRead(sensorPin) < minReading)
{
Serial.println("Waiting ... ");
}
}
void loop() {
currReading = analogRead(sensorPin);
Serial.println(currReading); //prints the values coming from the sensor on the screen
// calculate fluctuation, watch for negative
int fluctuation = (currReading > prevReading) ? currReading - prevReading : prevReading - currReading;
// if reading is over minimum and fluctuation is large enough to count
if ( (currReading > minReading) && (fluctuation > minFluctuation) )
{
// add to count
count++;
Serial.print("Count: ");
Serial.println(count);
} else {
// flucuation too small
// uncomment for debugging
// if( fluctuation <= minFluctuation )
// {
// Serial.print("Fluctation too small: ");
// Serial.println(fluctuation);
// }
// reading too small
// uncomment for debugging
// if( currReading <= minReading )
// {
// Serial.print("Reading too small: ");
// Serial.println(currReading);
// }
} // else
// current reading becomes previous reading
prevReading = currReading;
delay(300);
}