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.
// 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
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
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.