Pages: [1] 2 3   Go Down
 Author Topic: Finding the max and min value of sensor reading  (Read 3021 times) 0 Members and 1 Guest are viewing this topic.
Offline
Newbie
Karma: 0
Posts: 24
 « on: July 15, 2013, 03:15:59 pm » Bigger Smaller Reset

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:
`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?

 Logged

New Jersey
Offline
Karma: 65
Posts: 3638
 « Reply #1 on: July 15, 2013, 03:36:13 pm » Bigger Smaller Reset

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.
 Logged

Montreal
Offline
Karma: 27
Posts: 2557
 « Reply #2 on: July 15, 2013, 04:58:05 pm » Bigger Smaller Reset

Code:
`  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
 Logged

Offline
Newbie
Karma: 0
Posts: 24
 « Reply #3 on: July 16, 2013, 09:09:46 am » Bigger Smaller Reset

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:
` */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);}`
 Logged

Offline
Newbie
Karma: 0
Posts: 7
 « Reply #4 on: July 16, 2013, 11:34:54 am » Bigger Smaller Reset

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.

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

Offline
Newbie
Karma: 0
Posts: 24
 « Reply #5 on: July 17, 2013, 05:19:59 am » Bigger Smaller Reset

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:
`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:
`//record the min ,max sensor valueif (readings[index]<sensorMin){sensorMin=readings[index]; }if (readings[index]>sensorMax){sensorMax=readings[index]; }}`
 Logged

Global Moderator
Netherlands
Offline
Shannon Member
Karma: 211
Posts: 13471
In theory there is no difference between theory and practice, however in practice there are many...
 « Reply #6 on: July 17, 2013, 02:56:14 pm » Bigger Smaller Reset

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
 Logged

Rob Tillaart

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

Offline
Newbie
Karma: 0
Posts: 7
 « Reply #7 on: July 17, 2013, 03:13:55 pm » Bigger Smaller Reset

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.
 Logged

Offline
Newbie
Karma: 0
Posts: 24
 « Reply #8 on: July 18, 2013, 07:43:19 am » Bigger Smaller Reset

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:
`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??
 Logged

Global Moderator
Netherlands
Offline
Shannon Member
Karma: 211
Posts: 13471
In theory there is no difference between theory and practice, however in practice there are many...
 « Reply #9 on: July 18, 2013, 10:50:19 am » Bigger Smaller Reset

Code:
`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.
 Logged

Rob Tillaart

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

Offline
Newbie
Karma: 0
Posts: 24
 « Reply #10 on: July 19, 2013, 10:57:34 am » Bigger Smaller Reset

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:
`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;`

 « Last Edit: July 19, 2013, 02:32:09 pm by abdkdrsnl » Logged

Global Moderator
Netherlands
Offline
Shannon Member
Karma: 211
Posts: 13471
In theory there is no difference between theory and practice, however in practice there are many...
 « Reply #11 on: July 19, 2013, 11:09:28 am » Bigger Smaller Reset

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:
`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
can be shortenend to

works also for += *= /= and some more
 Logged

Rob Tillaart

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

Offline
Newbie
Karma: 0
Posts: 24
 « Reply #12 on: July 19, 2013, 02:28:06 pm » Bigger Smaller Reset

@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:
`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
 « Last Edit: July 19, 2013, 02:34:04 pm by abdkdrsnl » Logged

Global Moderator
Netherlands
Offline
Shannon Member
Karma: 211
Posts: 13471
In theory there is no difference between theory and practice, however in practice there are many...
 « Reply #13 on: July 20, 2013, 01:09:50 am » Bigger Smaller Reset

code comment conflict

Simplified your code, may contain typo or so, not tested, but it should work with minor mods
Code:
`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}  `
 Logged

Rob Tillaart

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

Global Moderator
Netherlands
Offline
Shannon Member
Karma: 211
Posts: 13471
In theory there is no difference between theory and practice, however in practice there are many...
 « Reply #14 on: July 21, 2013, 09:03:31 am » Bigger Smaller Reset

there was one typo in my code as I didn't check, now it should work
Code:
`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
...
 Logged

Rob Tillaart

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

 Pages: [1] 2 3   Go Up