Go Down

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

abdkdrsnl

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?

thanks for your help in advance

wildbill

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

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

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 readings
int index;                           // the index of current reading
int sensorMin=1023;                  //min sensor value
int sensorMax=0;                     //max sensor value
int deltaMax;                        // Max_value- Average
int deltaMin;                        // Min_value- Average
int total;                           //the running total
int average;                         // The average

void 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 value
if (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

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

Hi,
Thanks for your reply..

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 value
if (readings[index]<sensorMin){
sensorMin=readings[index];
}
if (readings[index]>sensorMax){
sensorMax=readings[index];
}
}

robtillaart


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

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

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 value
int sensorMax=0;                     //max sensor value
void 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 value
if (readings[index]>sensorMax){
sensorMax=readings[index];
}
if (readings[index]<sensorMin){
sensorMin=readings[index];
}
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  


any idea??

robtillaart

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

if (readings[index]<sensorMin){
sensorMin=readings[index];
}

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 pm Last 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 sensor
readings[index]=analogRead(A0);       //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;


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

robtillaart

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
sensorMax -= readings[index];

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 pm Last 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
thanks for your help in advance

robtillaart


sensorMin+=readings[index];  //substract the last reading

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

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