Finding min and max values from 5 readings of a sensor

Hello everybody ,

I am pretty new in Arduino and i hope i can get some help and ideas from you…

I have a sensor and i would like to read the the sensor value (eg 5 times) and find the min and max value out of 5 readings.It will be simply

readings[index]=analogRead(A0); //index will go till 5 and values will be stored in readings.but how can i find the min and max value

I have tried this one ; int minValue(min(readings[index])); but it doesnt work it gives me this error ‘min’ was not declared in this scope…

Thanks for your help in advance…

int min=0,max=0;
int readings[5];
for(int index=0; index<5; index++)
{
  readings[index] = analogRead(A0);
  if(readings[index]<min)
    min = readings[index];
  else if(readings[index]>max)
    max = readings[index];
}

You now have your maximum and minimum values in “max” and “min” variables.

Hi thanks for your answer but its not working this way.

the sensor somehow does not find the max and min value of 5 readings because in further programm i take the average value of readings and make as following;

delta_1=Max_Value-average;
delta_2=average-Min_Value;

if i dont do any process in sensor the readings must be same and therefore delta_1 and delta_2 must be zero but it doesnt show me so.i am able to read the values and storing the readings
''readings[index]=analogRead(A0); ‘’ but iu stucked here…here is a part of code
int Min_Value=0;
int Max_Value=0;
if (readings[index]<Min_Value){
Min_Value=readings[index];
}
else if (readings[index]>Max_Value){
Max_Value=readings[index];
}
delta_1=Max_Value-average;
delta_2=Min_Value-average;

any idea?

I'm sorry but I don't understand you very well :~

Could you post an actual sample of your code, and explain what you want it to do exactly please ?

hi again,

i have a strain sensor and the output value chanced depending on the strain position. (in case of UP the sensor output value decrease and in case of DOWN output value increases and LEDs becomes on or off).
So what i want to do is make automatic calibration. when u dont touch the sensor the sensor output will not chance (maybe a little)…

int readings[5];
int index;
int Min_Value=0;
int Max_Value=0;
int delta_1;
int delta_2;
int total;
int average;
int sensorValue =0;// variable to store the value coming from the sensor
void setup() {
Serial.begin(9600);
pinMode(10, OUTPUT);   //led for UP
pinMode(11, OUTPUT);   //led for DOWN
for (int i=0 ;i<5;i++)
readings[i]=0; 
 }
void loop() {
  total=total-readings[index];                   //here i make smoothing process to take the average value
  readings[index]=analogRead(A0);
  total=total+readings[index];
  index=index+1;
  if(index>=5)
  index=0;
  average=total/5;                                    //here i take average value

                  
  if (readings[index]<Min_Value){                           //from here i applied your code
  Min_Value=readings[index];
  }
  else if (readings[index]>Max_Value){
  Max_Value=readings[index];
  }
  delta_1=Max_Value-average;
  delta_2=Min_Value-average;
  
if(delta_1&&delta_2==0){                         //i say if delta_1 and delta_2 is zero (means no strain) turn the LEDs off
digitalWrite(11,LOW);
digitalWrite(10,LOW);
}
else if(delta_1>1){                                      //if delta_1 is bigger than 1,turn on the led for the UP position
digitalWrite(11,LOW);
digitalWrite(10,readings[index]);
}
else if(delta_2>1){                                    //if delta_1 is bigger than 1,turn on the led for the DOWN position
digitalWrite(10,LOW);
digitalWrite(11,readings[index]);
}
}

thanks a lot in advance

Oh OK, I think I guess what you want to do, but your code is full of nonsense :p

Please give me a few minutes, I'll give you a proper program ;)

Here it is, I hope this is what you wanted.

int cal_zero_min=1023, cal_zero_max=0;

void setup() {
  Serial.begin(9600);
  pinMode(10, OUTPUT);   //led for UP
  pinMode(11, OUTPUT);   //led for DOWN
  calibrate();
}

void calibrate() {
  for(int i=0; i<20; i++)
  {
    int value = analogRead(A0);
    if(value < cal_zero_min)
      cal_zero_min = value;
    if(value > cal_zero_max)
      cal_zero_max = value;
    delay(50);
  }
}

void loop()
{
  int value;
  for(int i=0; i<5; i++)
    value += analogRead(A0);
  value /= 5;
  if(value >= cal_zero_min && value <= cal_zero_max)
  {
    digitalWrite(10, LOW);
    digitalWrite(11, LOW);
  }
  else if( value < cal_zero_min)
  {
    digitalWrite(10, LOW);
    digitalWrite(11, HIGH);
  }
  else
  {
    digitalWrite(10, HIGH);
    digitalWrite(11, LOW);
  }
}

edit: maybe you want to add a dead zone around the neutral position of the sensor, in this case, just add this to the end of the calibrate function:

int threshold = 20; // adjust this value as you like
if(cal_zero_min - threshold >= 0)
  cal_zero_min = cal_zero_min - threshold;
else
  cal_zero_min = 0;
if(cal_zero_max + threshold <=1023)
  cal_zero_max = cal_zero_max + threshold;
else
  cal_zero_max = 1023;