This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Saving Milliseconds to array

I am running an infinte while loop. Inside the while loop i give a FOR loop which runs 3 times. each time i get a specific value of second from the real time clock. I need to store these 3 seconds value in an array and calculate the average at the end of 3 rd loop.

I tried like this:


int time_diff[3];// Initialization of array for 3 values
double avg;

SCU_APBPeriphClockConfig(__RTC,ENABLE); /*enable RTC clock*/
SCU_APBPeriphReset(__RTC,DISABLE);      /*RTC out of RESET state*/

time.milliseconds = 0; // Initialization time sturcture

while(1)
{
 for(k=0;k<=2;k++)
  {
    if ( a condition)
    {
     RTC_GetTime(BINARY,&BEGIN_TIME);
    }
    if ( another conditon)
    {
    RTC_GetTime(BINARY,&END_TIME);
    time_diff[k] = %02dms; // THIS LINEDIDNT WORK. DONT KNOW how to save that current value to time_diff[k] array
    }
    if (k == 3)
    {
    avg = (time_diff[0]+ time_diff[1]+time_diff[2])/3;
    sprintf(text, "Average =  %d",avg);
    lcd_print (text);
    }
 }
}


How is it possible to save the millisecond value as an array element? This code below displays the millisecond. But what i need is to save this current value to an array and then do averaging.

sprintf(text, "TIME in milli: %02dms ",END_TIME.milliseconds);
lcd_print (text);

Parents
  • Note that you don't even need to store the individual samples to compute an average.

    It's enough to sum the values and to sum the number of samples added to the sum.

    for (;;) {
        ...
        sum += delta_t;
        count++;
    }
    avg = sum/count;
    


    Without an array, you can sum many values - the limit is just when sum or count overflows.

Reply
  • Note that you don't even need to store the individual samples to compute an average.

    It's enough to sum the values and to sum the number of samples added to the sum.

    for (;;) {
        ...
        sum += delta_t;
        count++;
    }
    avg = sum/count;
    


    Without an array, you can sum many values - the limit is just when sum or count overflows.

Children