Objects/time. average display. How to?

Hi all

What I have: - an ir light barrier. - a program which counts objects passing the barrier.

What I would like to have in addition: - a program which also displays the number of objects which passed in the last minute. - I would like the number to refresh about every second.

If anyone has an idea how to realize the project please answer.

What I have: - an ir light barrier. - a program which counts objects passing the barrier.

OK.

What I would like to have in addition: - a program which also displays the number of objects which passed in the last minute.

Fortunately, the Arduino has a millis() function. It's worth researching what it does.

  • I would like the number to refresh about every second.

Write the appropriate code, and that will happen. Nothing is automatic, though.

If anyone has an idea how to realize the project please answer.

I do. I did.

DiMedici: - a program which also displays the number of objects which passed in the last minute. - I would like the number to refresh about every second.

You could achieve that with a circular buffer of samples acting as a FIFO, each sample covering one second, sixty samples in the buffer making up the total for the past minute.

Accumulate the number of objects in a counter. Once per second, push the counter value into the FIFO and then reset the counter. Pushing the new value in will push the oldest value out of the FIFO. The sum of the entries in the FIFO gives you the total for the past minute.

To avoid having to walk the contents of the FIFO adding them up every second, you can keep a running total which is incremented with the value pushed into the FIFO and decremented with the value pushed out.

If the counters are going to be large and an exact total over the minute isn't important you can get very similar behaviour much more simply just using a single accumulator. Each time you get a new sample you calculate a weighted average of the accumulator and the new sample, and store that in the accumulator. In effect you get a decaying average of the recent samples, and your weighting determines how quickly it decays.