Averaging or smoothing

Hi all,
I am very new at the arduino world but enjoying it very much.

I am looking at the best way to program this probably simple project to start.

I want to calculate the time between two presses of a button. - this I have done.

I want to the take the timings and work out the average over a period of time.. Say 1 minute for example.. Could be 2 minutes or 5mins.

Then use that average in my next part of the program.

Hope that makes sense.

Many thanks in advance.

I simply create an array to store items I want to average.

Karma: 0 [add]
Logged

Averaging or smoothing
Today at 15:20:07
Hi all,
I am very new at the arduino world but enjoying it very much.

I am looking at the best way to program this probably simple project to start.

I want to calculate the time between two presses of a button. - this I have done.

I want to the take the timings and work out the average over a period of time… Say 1 minute for example… Could be 2 minutes or 5mins.

So you either need to save each value, maybe in an array, EEPROM or SD card and do the calculation at the end of the period, or average it as you go along when a new value becomes available. Use millis() to time the overall period as well as the times between button presses.

Are you timing between when the button becomes pressed or are you detecting when it is pressed ?

Count the number of presses in a minute or whatever. At the end of 1 minute the average time between presses in seconds is 60/count.

A rolling average is also possible, if you need to have it update every 10 seconds instead of waiting a whole minute each time. Then you need an array to store the count in each 10-second block and add up the last 6 array entries to get the total count in the last 60 seconds.

I am grabbing the timing between presses, not the duration of the press.

I have been using the smoothing option.
But not sure if this is the best option. The problem is, I don't know how many presses is going to be done within the minute or two etc..

Using an array, I would have to specify the number of entries that's going to be put into it and I don't know how many that is going to be within the minute etc.

Could be 1 it could be 200. That's why I was looking at smoothing?

It's the storing all the timings between presses somewhere and working out the average time at the end of the specified time period.

Thanks all for your help so far.

No, you only need to count.

I suggested an array with 6 entries will update every 10 seconds. If you want updates every second then 60 integers is not large. The Arduino has a lot more memory than that.

Remember seconds per event is just the inverse of events per second.

I am grabbing the timing between presses, not the duration of the press.

That was not what I was asking.

Please post your code

Actually none...

it is the principle of the routine that will capture the data and get the average after a given time with unpredictable occurrences.

OK, so if I held the button down for 10 seconds at the start of one minute and then didn't touch it for the rest of the minute, the average time between presses would be 50 seconds?

If I pressed it for 10 seconds, waited 20, pressed it for 10, waited 20 then the average is 20?

The "counting" average for those two examples would be 60 seconds and 30 seconds.

ashtonm:
I want to the take the timings and work out the average over a period of time… Say 1 minute for example… Could be 2 minutes or 5mins.

So, count the number of button presses and divide by the number of minutes to get button-presses per minute.

The only the thing is I want to know the time between presses.

In my end code it will be pressed or not. 1 or 0 HIGH or LOW.

So this is what I am trying to do.
example...

do for 5 minutes....
time between each press of a button (not time button is pressed for)
record each time between each press i.e pressed at 0000 millisecond then pressed at 3000
millisecond i.e. 3000, 3500, 2600, 4000 etc...

when time is up get average of all timings

use the average time to do something
e.g. average at this point is 3275

then each time the button is pressed from now on it will adjust the average with new timing.
i.e. next press 3900 + 3275 new average 3587.50
repeat use the average time to do something

Hope this helps

No it doesn't help.

When you say "time between presses" do you mean the time between the button going up and the button going down? That would be 5 minutes minus the time that it was held down.

Or do you mean the time between events when it went from up to down? Then you just need to count.

Pressing the button down = stop timer.
it will be from the instant the button is pressed down.
Not timing the length of the depression. it would be an instant press.
1
2
3
4
5
press
timing = 5
1
2
3
4
press timing = 4
etc..

average = (5+4)/2 in this example
average = 4.5

2 events in 9 seconds is 4.5 sec per event.

You just need to count the number of transitions from high to low. Either count for a whole minute or drop them into bins of the desired duration for your updates. 6 or 60 bins should be enough.

0.5-second bins will look like it updates instantly after each press, to a human observer at least.

Dude, are you trying to tell us that the pushbutton is meant to act like a toggle switch? That you want the sum of the times between every second press?