Rolling Array

Hi everybody, i've got some issues concerning running average library

I'm reading values from a sensor.
I would like to have only the last 100 values of the sensor.

{value (t-100*delay) , value(t-99*delay),  ....  ,value(t-delay)}
// with value(t) = analogRead(SENSEPIN)

I'm using RunningAverage library to shift the values in the array.
The fact is that when i declare my Running Average array, i can't access to an element in particular, i can just access to the average and add a new value.

#include "RunningAverage.h"

RunningAverage myRA(100); //créé un array de 100 valeurs
int samples = 0;
int SENSEPIN = 7;

void setup(void) 
{
  Serial.begin(9600);
  myRA.clear();
}

void loop(void) 
{
  int rn = analogRead(SENSEPIN); //reading sensor value
  myRA.addValue(rn);  //adding it to myRA (which places the value to the last position and shift the rest of   the values
  samples++; 

  Serial.println("Running Average: ");
  Serial.println(myRA.getAverage(), 2);

  Serial.println("The Last value of the array is : ");
  Serial.println(myRA.getElement(80)); //here i can't get the last element, i've got nan and then a fix value like 566...
  delay(100);
  
  if (samples == 300)
  {
    samples = 0;
  }
}

I really need to access the last value in order to apply some kind of a filter on it :

 if ( abs(lastValue - actualValue) >= x )
//remove the value
// wait
// measure again

Not sure the RunningAverage is the best way but that's the most popular way i've found to shift values in an array.

Thanks a lot !

from Rob's class:

Are you sure there is a value in element 80?

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

separate to that:

RunningAverage myRA(100); //créé un array de 100 valeurs

which element is the last?

Ok, indeed, i didn't wait enough time.
The problem is that the last element doesn't change every measure (and it should be because of add.value)

Chuoby:
Ok, indeed, i didn't wait enough time.
The problem is that the last element doesn't change every measure (and it should be because of add.value)

why do you think that the last value should change?

again, from Rob's class:

void RunningAverage::addValue(float f)
{
    if (_ar == NULL) return;
    _sum -= _ar[_idx];
    _ar[_idx] = f;
    _sum += _ar[_idx];
    _idx++;
    if (_idx == _size) _idx = 0;  // faster than %
    if (_cnt < _size) _cnt++;
}

it appears he moves _idx and doesn't cascade the values back in the array

Thank you for your answer ! Yet i didn't understood very well if it's possible to access the last element added to the array.

You will have to add a function to the library to get access to the last element.
That could be a function that returns the index of the last added value, or the value itself.

You could use variable in your sketch that holds the last added value.

Am I missing something?
The newest value in the array is the one you just put there.

AWOL:
Am I missing something?
The newest value in the array is the one you just put there.

The library moves a protected index to the next element of the array to deposit the next value.

There isn't a method to retrieve the last value entered, in the case where you went off the reservation, scope-wise.