Go Down

### Topic: Finding the max and min value of sensor reading (Read 20186 times)previous topic - next topic

#### abdkdrsnl

##### Jul 15, 2013, 10:15 pm
Hello everybody,

I have been trying nearly all the ways and i opened this topic into discussion but got not proper answer..

I have a sensor and i would like to find the max and min value after 10 readings. any idea?  basically put one for loop then save them into readings[index]
Code: [Select]
`int min=0,max=0;int readings[10];for(int index=0; index<10; index++){  readings[index] = analogRead(A0);`

but how can i find and store max and min value among 10 values?

#### wildbill

#1
##### Jul 15, 2013, 10:36 pm
Set min to 1024, max to -1.
Within your loop, use an if to test whether the latest reading is less than min and if so, set min to the new value. Similar thing for max.

#### Magician

#2
##### Jul 15, 2013, 11:58 pm
Code: [Select]
`  minim = 1023;  maxim = 0;  for ( int i = 0; i < INBUF; i++){                                    if ( x[chan][i] > maxim ) maxim = x[chan][i];    if ( x[chan][i] < minim ) minim = x[chan][i];  }`
INFUF - is a size (10 in your case), x[] an array of samples

#### abdkdrsnl

#3
##### Jul 16, 2013, 04:09 pm
Hi,

Thanks for your answer. Here i have done something .I can see sensor reading and running average but can not see max and min value shows me always zero! Any idea?
Code: [Select]
` */const int numReadings=5;int readings[numReadings];            //Number of readingsint index;                           // the index of current readingint sensorMin=1023;                  //min sensor valueint sensorMax=0;                     //max sensor valueint deltaMax;                        // Max_value- Averageint deltaMin;                        // Min_value- Averageint total;                           //the running totalint average;                         // The averagevoid setup() {  Serial.begin(9600);pinMode(10, OUTPUT); pinMode(11, OUTPUT);readings[index]=analogRead(A0);for (int i=0 ;i<numReadings;i++)readings[i]=0; //record the min ,max sensor valueif (readings[index]<sensorMin){sensorMin=readings[index]; }if (readings[index]>sensorMax){sensorMax=readings[index]; }}void loop() {    total=total-readings[index];        //substract the last reading  readings[index]=analogRead(A0);  //read from the sensor  total=total+readings[index];         //add the reading in the total  index=index+1;  if (index >= numReadings)                  index = 0;                             average=total /numReadings;          //running average   deltaMax=sensorMax-average;       deltaMin=average-sensorMin;  if(deltaMax >1||deltaMin >1){digitalWrite(11,LOW);digitalWrite(10,LOW);index=0 ;                                       //turn the beginning read the sensor again}if(deltaMax>average){digitalWrite(11,LOW);digitalWrite(10,readings[index]);}if(deltaMin<average){ digitalWrite(10,LOW);digitalWrite(11,readings[index]);}Serial.println (average);delay(100);}`

#### Vinylon

#4
##### Jul 16, 2013, 06:34 pm
You manually set the whole array to 0 before checking it against the max/min. 0 will always be >= 0, and 0 will always be < 1023. Set the array to the input data instead of initializing it to 0 directly before checking for min/max.

So your problem is,

Code: [Select]
`for (int i=0 ;i<numReadings;i++)readings[i]=0;`

#### abdkdrsnl

#5
##### Jul 17, 2013, 12:19 pm
Hi,

I did not get exacyly what you mean.with this code readings=0; i initialitze the array and fill the array with sensor readings.when i delete this sensor shows 0 for the average values then i say
Code: [Select]
`if (readings[index]<sensorMin){sensorMin=readings[index]; }if (readings[index]>sensorMax){sensorMax=readings[index]; }` and my codes shows me the sensor readings, average but my codes does not show the min and max values among sensor readings :S
Code: [Select]
`//record the min ,max sensor valueif (readings[index]<sensorMin){sensorMin=readings[index]; }if (readings[index]>sensorMax){sensorMax=readings[index]; }}`

#### robtillaart

#6
##### Jul 17, 2013, 09:56 pm

You can check this library too - http://playground.arduino.cc/Main/Statistics -

It will give you min, max and the average and optional std_dev
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

#### Vinylon

#7
##### Jul 17, 2013, 10:13 pm
You're calculating the average in your loop, but only calculating the min/max once in the setup function. Further, you calculate the min/max directly after initializing the entire array to 0. Of course they're going to read 0.

#### abdkdrsnl

#8
##### Jul 18, 2013, 02:43 pm
Hello again,

Yes now i am able to see the running average value of 5 readings and max value also.but i cant see min value..it always shows me ''0''

Code: [Select]
`int sensorMin=1023;                  //min sensor valueint sensorMax=0;                     //max sensor valuevoid setup() {Serial.begin(9600);pinMode(10, OUTPUT); pinMode(11, OUTPUT);readings[index]=analogRead(A0);for (int i=0 ;i<numReadings;i++)readings[i]=0;}void loop() {//record the min ,max sensor valueif (readings[index]>sensorMax){sensorMax=readings[index]; }if (readings[index]<sensorMin){sensorMin=readings[index]; }total=total-readings[index];         //substract the last readingreadings[index]=analogRead(A0);  //read from the sensortotal=total+readings[index];         //add the reading in the totalindex=index+1;if (index >= numReadings)                index = 0;  average=total /numReadings;          //running average  `

any idea??

#### robtillaart

#9
##### Jul 18, 2013, 05:50 pm
Code: [Select]
`readings[index]=analogRead(A0);for (int i=0 ;i<numReadings;i++)readings[i]=0;`
in this for loop you make all your readings 0 so the analogRead() you did is gone...

then in loop() you do

}

which forces sensorMin to 0.

follow your code with paper and pencil and be the processor. Draw boxes for the variables and you will see.
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

#### abdkdrsnl

#10
##### Jul 19, 2013, 05:57 pmLast Edit: Jul 19, 2013, 09:32 pm by abdkdrsnl Reason: 1
Hi again,

Here i have changed the codes a little bit ..i see the max value and mix value perfectly! but average value shows always 0!

Code: [Select]
`void setup() {Serial.begin(9600);pinMode(10, OUTPUT); pinMode(11, OUTPUT);}void loop() {for (int i=0 ;i<numReadings;i++)//read from the sensorreadings[index]=analogRead(A0);       //read from the sensor//record the min ,max sensor valuesensorMax=sensorMax-readings[index];  //substract the last readingif (readings[index]>sensorMax){sensorMax=analogRead(A0); }sensorMin=sensorMin+readings[index];  if (readings[index]<sensorMin){sensorMin=readings[index]; }total=total-readings[index];          //substract the last readingreadings[index]=analogRead(A0);total=total+readings[index];         //add the reading in the totalindex=index+1;if (index >= numReadings)                index = 0;  average=total /numReadings;          //running average deltaMax=sensorMax-average;     deltaMin=average-sensorMin;`

I really need your idea and help..thanks a lot in advance

#### robtillaart

#11
##### Jul 19, 2013, 06:09 pm
What you still need to learn is improve your writing style/layout.
Press CTRL-T in the IDE  autoformats the code and that improves readability a lot.
Furthermore some spaces and empty lines

Code: [Select]
`void setup() {  Serial.begin(9600);  pinMode(10, OUTPUT);   pinMode(11, OUTPUT);}void loop() {  for (int i=0 ;i<numReadings;i++)    readings[index]=readings[index];           //read from the sensor  //record the min ,max sensor value  sensorMax = sensorMax - readings[index];  //substract the last reading  if (readings[index] > sensorMax){    sensorMax = analogRead(A0);   }  sensorMin = sensorMin + readings[index];    if (readings[index] < sensorMin){  sensorMin = readings[index];   }  total = total - readings[index];          //substract the last reading  readings[index] = analogRead(A0);  total=total + readings[index];         //add the reading in the total  index=index + 1;  if (index >= numReadings)                  index = 0;    average = total /numReadings;          //running average   deltaMax = sensorMax - average;       deltaMin = average - sensorMin;`

finally
sensorMax = sensorMax - readings[index];
can be shortenend to

works also for += *= /= and some more
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

#### abdkdrsnl

#12
##### Jul 19, 2013, 09:28 pmLast Edit: Jul 19, 2013, 09:34 pm by abdkdrsnl Reason: 1
@robtillart

Thanks for the informative answer and tipps but i still did not solve the problem of calculation average value..
as i told in previous post that now i am able to read the sensor and find the max and min value ..however average value still shows ''0'' .

Code: [Select]
`void setup() {  Serial.begin(9600);  pinMode(10, OUTPUT);   pinMode(11, OUTPUT);}void loop() {  for (int i=0 ;i<numReadings;i++)    readings[index]=analogRead(A0);   //read from the sensor  //record the min ,max sensor value  sensorMax-=readings[index];  //substract the last reading  if (readings[index]>sensorMax){    sensorMax=readings[index];   }  sensorMin+=readings[index];  //substract the last reading  if (readings[index]<sensorMin){    sensorMin=readings[index];   }  total-=readings[index];        //substract the last reading  readings[index]=analogRead(A0);  total+=readings[index];         //add the reading in the total  index=index+1;  if (index >= numReadings)                    index = 0;    average=total /numReadings;          //running average `

is it maybe i did not initialize the array? but when i make this then i cant get min and max values..:S

#### robtillaart

#13
##### Jul 20, 2013, 08:09 am

code comment conflict

Simplified your code, may contain typo or so, not tested, but it should work with minor mods
Code: [Select]
`int numReadings = 10;int readings[10];int index = 0;int sensorMax = 0;int sensorMin = 0;void setup() {  Serial.begin(9600);  pinMode(10, OUTPUT);   pinMode(11, OUTPUT);  sensorMax = analogRead(A0);  sensorMin = sensorMax;}void loop(){  // MEASUREMENTS  // every iteration of loop makes one additional measurement,  // so the first 10 readings will display too low average  int value = analogRead(A0);  readings[index] = value;  index++;  if (index >= numReadings) index = 0;    // DO SOME MATH  if (value > sensorMax) sensorMax = value;   if (value < sensorMin) sensorMin = value;   //running average  float total = 0;  for (int i = 0; i< numReadings; i++) total += readings[index];  float average = total /numReadings;            // OUTPUT TO SERIAL  Serial.print("\tMIN:\t");  Serial.print(sensorMin);  Serial.print("\tMAX:\t");  Serial.print(sensorMax);  Serial.print("\tAVG:\t");  Serial.println(average, 1);  // 1 decimal}  `
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

#### robtillaart

#14
##### Jul 21, 2013, 04:03 pm
there was one typo in my code as I didn't check, now it should work
Code: [Select]
`int numReadings = 10;int readings[10];int index = 0;int sensorMax = 0;int sensorMin = 0;void setup() {  Serial.begin(9600);  pinMode(10, OUTPUT);   pinMode(11, OUTPUT);  sensorMax = analogRead(A0);  sensorMin = sensorMax;}void loop(){  // MEASUREMENTS  // every iteration of loop makes one additional measurement,  // so the first 10 readings will display too low average  int value = analogRead(A0);  readings[index] = value;  index++;  if (index >= numReadings) index = 0;    // DO SOME MATH  if (value > sensorMax) sensorMax = value;   if (value < sensorMin) sensorMin = value;   //running average  float total = 0;  for (int i = 0; i< numReadings; i++) total += readings[i];  float average = total /numReadings;            // OUTPUT TO SERIAL  Serial.print("\tMIN:\t");  Serial.print(sensorMin);  Serial.print("\tMAX:\t");  Serial.print(sensorMax);  Serial.print("\tAVG:\t");  Serial.println(average, 1);  // 1 decimal} `

output, as predicted it takes 10 readings to stabilize ..
MIN:   680   MAX:   682   AVG:   68.2
MIN:   680   MAX:   694   AVG:   137.6
MIN:   680   MAX:   707   AVG:   208.3
MIN:   680   MAX:   730   AVG:   281.3
MIN:   680   MAX:   730   AVG:   349.5
MIN:   657   MAX:   730   AVG:   415.2
MIN:   657   MAX:   738   AVG:   489.0
MIN:   637   MAX:   738   AVG:   552.7
MIN:   637   MAX:   738   AVG:   622.3
MIN:   637   MAX:   738   AVG:   691.0
MIN:   633   MAX:   738   AVG:   686.1
MIN:   633   MAX:   738   AVG:   689.6
MIN:   633   MAX:   738   AVG:   682.2
MIN:   633   MAX:   738   AVG:   674.7
MIN:   633   MAX:   738   AVG:   676.7
MIN:   615   MAX:   738   AVG:   672.5
MIN:   615   MAX:   738   AVG:   668.2
MIN:   615   MAX:   738   AVG:   668.8
MIN:   615   MAX:   738   AVG:   661.6
MIN:   615   MAX:   738   AVG:   663.5
MIN:   604   MAX:   738   AVG:   660.6
MIN:   604   MAX:   738   AVG:   654.2
MIN:   604   MAX:   738   AVG:   656.4
...
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Go Up