I have a digital input which get added when a trigger condition is triggered (basically If (thing happens){ value++} would be the psudocode for the situation).
What im curious is can i take the average of this value over a period of time? like if i wanted to find the average value of this counter over a set period of time (like the value would increase and then at the end of a period of time the time average of the value would be taken) what would be the syntax to do that?
I can explain better if nessecary in case im not being clear right now. Even just a point in the right direction would be appreciated.
It's not at all clear what kind of "average" you want. The simplest, and probably least useful, would be to simply record the value at the start of the sample period. At the end of the sample period, subtract the start value from the end value, and divide by two. If you want a time-weighted average(i.e. - weighting by how much time is spent at each value), you need to define the overall sample period, as well as the resolution of the time-weighting.
@Nick_Pyner Thank ya but i need the syntax really to basically time a set amount of time while taking the count and use that count accumulated in that time to get an average.
@RayLivingston Yes Im sorry for not being entirely clear. Yes I would predefine the amount of time i would want to average the sample over, my question is more how to i set up the syntax to effectively set up a counter to take a measurement during a time period. My mind thinks to set up a for loop for that but its a question of how to define that for loop to operate for a period of time rather than a numerical condition.
Again thanks to you both for taking time to help what is probably a stupid beginners question
There are few beginners' questions that are stupid, but some are due to misunderstanding. here, I think you just need the extra variable - the accumulator.
The loop is the same, except for a timer. So, for an average of ten readings taken once per second
setup
{
target=10
}
loop
{
read data
count = count+1
accumulator = accumulator + data
if count = target, av = accumulator/count, accumulator = 0, count = 0
Serial.println(av);
delay(1000):
}
My read of what the OP asked is that the timer is for how long the reading takes place, not how often (although of course "thing happens" could be the passing of some regular interval), and the reading is triggered by some other external thing like a button maybe.
So I'd wrap the reading code in an "if" comparing millis() to the period length in ms. (millis() being the time elapsed since power up or system reset.)
If the time so far (that is, millis()) is less than the desired period, if "thing happens" increment the count and add the data to the accumulator.
Once the elapsed time has exceeded the period, it will drop past that if, then we do the average.
It would be easy enough to add a button to restart the whole process without having to reset the Arduino, if you wanted to get another average later on.