Hello, I am trying to find out how to write a program which will take the average over the day. I dont want it to just take the average over the last 24 hours, I know how to do that, rather I want to find the average from 12 midnight to 12 midnight the next day, and to have this average displayed throughout the day and changing as the day progresses. This is to be written in C code
It's hard to see what problem you're having if you know how to average the last 24 hours. If you post your 24 hour code we'll help you to modify it as required.
Stefan, I don't have the code written but for averaging over the last 24 hours i would: static double average = 0; count = 0; average = (average + radiation)/24; count++; if (count > 23) { result = SetCtrlVal (mainpanel, MAINPANEL_WSAV, average); count = 0; wind_av = 0; } This would take the 24 values recieved once the prgram is run and then take the average after all values have been recieved. I need to instead have a running average which displays the average over the day and changes as the day goes on. For example whatever the value is when the system is first truned on, that average would appear in the display until another value was recieved, then it would be modified accordingly. Then average then must return to zero at midnight and the program can start again for the next days average. As I need to know how to take the average from 12 midnight one day to 12 midnight the next day.
Just as you said: At midnight, reset average and count to zero, and display the running average from that time. The last-24-hour running average is reset once when the program starts and you initialize the variables. There's no reason you can't reset the average any time you like, though. You'll have to have some way to set the time of day in your device so that it knows when "midnight" comes around. You could manually set it and use an 8051 timer to track the progress of time. Or perhaps your project includes a real-time clock that can maintain the TOD for you.
Thanks, but Im having problems figuring out how to do the running average itslef. I know its probably simple, but i havent done it before
Im having problems figuring out how to do the running average itself. I.e. it's exactly opposite to what you originally seemed to be saying: you know how to do a fixed-interval average, but not how to compute the average over the last 24 hours (that one is a running average). Well, you need to store all hourly input values from inside the running average's sliding window in an array. Then you do one of these: *) re-compute the sum over that array each time you update the display *) re-compute the sum over the array only when it actually changes *) compute the array a little more cleverly, subtracting the value that fell out the back end of the window, and adding the new reading, each time a new value comes in
Thanks everybody, nobody really knew what I was asking but I figured it out. This is what I did: GetSystemTime (&hour, &minute, &second); if((second == 0 ) & (datecount == 0)) { datecount = 1; sumday = 0; } if (second != 0) { sumday = (sumday + radiation); datecount++; result = SetCtrlVal(mainpanel, MAINPANEL_SUM_DAY, sumday); datecount = 0;
Divide by the number of samples you have. 1 sample divide by 1. 2 samples 2. you could divide by "count in you example. Avoid dividing by zero.
View all questions in Keil forum