Hello,
My program has one analog input (A0). Its values varying (Small variations) like, Ex:- 221,225,220,226,220,229,227.....I need to select some set of analog reading values in a certain time (Ex:-0.5s) or certain number of values (Ex-10 number of analog reading values) and get the mean value of it. Then i need to check the different between those real time analog input values & the mean value with some Threshold value.
Ex:-
Mean value of analog reading(in 0.5s or 10 number) = M
variable values = X1, X2, X3, X4....... (Those are analog reading values) lets say one value is X,
Threshold value = 5
***** Section 1 *****
So 1st i need to check is,
(|(M-X)|)<5 , (here im really checking ("Modulus" value of the solution) <5
in here (M-X), there will be positive results and negative results, but i need the "magnitude" of the results value.
like "" (abs (M-X))<5 ""
......if ((abs (M-X))<5)......
in the above stage i really wanted to see whether there is s larger variation in the analog input values (check whether if it flickers in larger values).
If its false do nothing,
If its true,
In this case its need to store the above " M " value for further use.
Then i need to re-check above values with another Threshold values.
If we are in this stage its need to "avoid" the new "M" value calculations. because if its calculate again and again the below checking will change and cant get the needed results.But about the "X" values its need to get real time "X" values from the analogRead(A0) continuously for the checking.
****** Section 2 ******
-
if (5<(abs(M-X))<10)...Serial.print("Its low")
-
else if (10<(abs(M-X))<20)...Serial.print("Its Medium")
-
else if (20<(abs(M-X)))...Serial.print("Its High")
in the above 3 stages its need to stay on one stage at least 3 seconds and re check the Three "" if condition "".
*** The main thing is the stability and the smoothness of above 01) , 02), 03) stages.***
When exit from above " Section 2 ", its need to remove that stored " M " value for the operation and Go to " Section 1 " (to beginning) and need start to check if there is large flickers in input values.
My beginning of the code is mentioned below, but its needs to modify or change. Thats what i really want.Thank you. a
Code- 01.
int previousReading = -1;
int currentReading ;
void setup() {
// pinMode(A0,INPUT);
Serial.begin(9600);
}
void loop() {
currentReading = analogRead(A0);
//Serial.println(currentReading);
if (previousReading != -1) {
if ((currentReading > previousReading + 25) ||
(currentReading < previousReading - 25)) {
Serial.println("Its HIGH");
delay(100);
} else if ( (currentReading > previousReading + 20) ||
(currentReading < previousReading - 20)) {
Serial.println("Its Medium");
delay(100);
} else if ( (currentReading > previousReading + 5) ||
(currentReading < previousReading - 5)) {
Serial.println("Its LOW");
delay(100);
}
}
previousReading = currentReading;
}
Code 02-
int Readings = 10; // number of samples
int readingNumber; // counter for the sample array
int analogValues[10];
int averageArray();
void setup() {
Serial.begin(9600);
}
void loop() {
for (readingNumber = 0; readingNumber < Readings; readingNumber++) {
//get the reading:
analogValues[readingNumber] = analogRead(A0);
// increment the counter:
readingNumber++;
}
//Serial.println(abs (AV));
//Serial.print(averageArray(), DEC);
// average the array:
int AV=((analogRead(A0)-averageArray()));
Serial.print(abs(AV));
// Serial.print(analogRead(A0));
if(abs(AV)<15){
Serial.println("LOW");
delay(3000);
}else if(15<abs(AV)<20){
Serial.println("MIDDLE");
delay(3000);
}else if(20<abs(AV))
Serial.println("HIGH");
delay(3000);
}
// average the values in the array:
int averageArray() {
int total ;
int average ;
int i;
for (i = 0; i< Readings; i++) {
total = total + analogValues[i];
}
average = total/(Readings +1 );
return average;
}