I read an analog temperature into an array.
The measurement is a infinite loop.
For the average I need the last 50 values.
First I fill up the array until all elements are written. Then it kicks out the first element to make space for a new one. Then the average is taken. Unfortunately it let's my program freeze for a second. Seems like that my way of doing it takes lots of resources.
Is there a more neat way?
UInt64 count_avg = 0; //global variable
//Main
public MainPage()
{
this.InitializeComponent();
while(true)
{
double analog_temperature = getData();
Average(analog_temperature);
}
} //Main
private string Average(double analog_temperature)
{
double temp_avg = 0;
double[] array = new double[50];
string avg = "";
count_avg += 1;
if (count_avg <= 50)
{
array[count_avg] = analog_temperature;
temp_avg = analog_temperature;
}
else
{
double sum = 0;
List<double> array_liste = array.OfType<double>().ToList(); //convert array to list
array_liste.RemoveAt(0); //delete first list item
array = array_liste.ToArray(); //convert list ot array
array[array.Length - 1] = analog_temperature; //write current value in last array element
for (int i = 0; i < array.Length; i++)
{
sum = array[i];
}
temp_avg = summe / array.Length;
}
temp_avg = Math.Round(temp_avg , 1);
return avg = Convert.ToString(temp_avg );
}