RunningAverage Library- how to access Library array

Hi Folks, I'm very much a Noob here, so hopefully I'm asking in the right place.

The RunningAverage Library ( http://playground.arduino.cc/Main/RunningAverage ) uses a circular array to produce an average of the total array
Is there any way to access an individual array element from the library and use it in a user program?
e.g. array (3,n)
and use the returned value for further processing ?

still learning the Arduino language, hopefully I'm making sense.
thanks, Pencoys

In the library as is there is no method to do that.

The simplest way to patch the lib is to move the internal array from the protected to the public section of the class.
That allows RW access including the option to corrupt the internal admin.

A better way might be to add an access function:

add this line to the .h file in the public section

	float getElement(uint8_t idx);

and this in the cpp

// returns the value of an element if exist, 0 otherwise
float RunningAverage::getElement(uint8_t idx)
{
	if (idx >=_cnt ) return 0; // or NaN ? 
	return _ar[idx];;
}

Can you tell why you want access to the underlying elements?
(if the argument is generic I might add the accessor to the lib permanently)

Hi Rob, I did'nt expect an answer from the author.

Ok, I'm sampling a pressure sensor and want to average its output over a few samples and then display a scrolling graph (against time ) of the averaged results.
I'm trying to avoid writing a circular array for myself as , at present, its beyond my abilities.

So far I'm displaying the averaged result , in text, on a graphic LCD ( PCD8544 ) 84 wide, 48 high, origin top left.

If you just want the running average, there is no reason at all to even save the array.

//For 10 averages
avg = (9.0 * avg)/10.0 + new_val/10.0

If you want good data from the get go, you need to make a special case of the first 10 numbers.

@keithRB
There is not a big difference in many cases.
The array version takes only the last 10 readings into account with equal weight. Older readings are ignored.
The formula does take in account older readings (until eternity) with a decreasing weight.
The formula version assumes that the element "removed" equals the average.

To see the differences calculate them both in a sketch, and feed them different input signals

  • triangle (e.g. nr's 1..100..1
  • wave (100 + sin(i*10)
  • square ( 1 1 1 1 1 1 1 1 1 9 9 9 9 9 9 9 9 9 9 1 1 1 1 1
  • noise
  • your signal of choice

Some untested code to get started

#include "RunningAverage.h"

RunningAverage myRA(10);
int samples = 0;

float avg = 0;

void setup(void) 
{
  Serial.begin(115200);
  Serial.println("Demo RunningAverage lib");
  Serial.print("Version: ");
  Serial.println(RUNNINGAVERAGE_LIB_VERSION);
  myRA.clear(); // explicitly start clean
}

void loop(void) 
{
  long rn = random(0, 1000);
  myRA.addValue(rn * 0.001);
  avg = (9.0 * avg)/10.0 + (rn * 0.001)/10.0;

  Serial.print(myRA.getAverage(), 3);
  Serial.print("\t");
  Serial.print(avg, 3);
  Serial.print("\t");
  Serial.println(myRA.getAverage() - avg, 3);

  delay(100);
}