 # Problem with Average Library

Hi friends,
I have to calculate average values of data stored in arrays. Here I am taking 50 readings each time and then storing the maximum and minimum of the readings in arrays named as maxi and mini. The arrays size is 500. The problem which I am facing is that whenever use the library function “mean” to calculate the mean values of the arrays it works fine only when I am calculating mean of any one array. When I try calculating mean of both the arrays , the code doesn’t print anything on the serial monitor. The code is as follows:

``````#include <Average.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
float current1;
int maxi;
int mini;
void setup(){
Serial.begin(9600);
}
void loop(){
int i,j,mini,maxi;
int c,a=0,b=1023;
for(i=0;i<500;i++){
for(j=0;j<50;j++){
if(c>a)
a=c;
else if(c<b)
b=c;
}
maxi[i]=a;
mini[i]=b;
Serial.println(a);
Serial.println(b);

}
float avgmax=mean(maxi,500);

float avgmin=mean(mini,500);
Serial.println("MEan");
Serial.println(avgmax);
Serial.println("Avg-min");
Serial.print(avgmin);
delay(1000*60);

}
``````

Thank you

Layout your code! (look in the IDE) and NEVER CROSS POST!.

Mark

``````int maxi;
int mini;
``````

That's 2000 bytes of SRAM of the 2048 available on the 328-based Arduinos. Therefore, you must be using a Mega or a Due, right?

``````void loop(){
int i,j,mini,maxi;
``````

These 'mini' & 'maxi' are not the same variables declared in the global scope, they are created and destroyed every loop. Your loop method will not touch the global arrays.

Also as PaulS pointed out the memory usage, you can now effectively double this requirement. Although the globals are most probably removed from the final compiled output.

int maxi; int mini; these two takes 2K of RAM, that's all an UNO has .... Are you using a MEGA?