# Rolling Array

**URL:** <https://forum.arduino.cc/t/rolling-array/364063>\
**Category:** Programming\
**Created:** [February 8, 2016, 11:59am UTC](https://forum.arduino.cc/t/rolling-array/364063 "2016-02-08T11:59:27Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chuoby](https://avatars.discourse-cdn.com/v4/letter/c/7cd45c/32.png) [@Chuoby](https://forum.arduino.cc/u/Chuoby)\
**Post date:** [February 8, 2016, 11:59am UTC](https://forum.arduino.cc/t/rolling-array/364063/1 "2016-02-08T11:59:27Z")

</div>

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.

```cpp
{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.

```cpp
#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 :

```cpp
 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 !

---

<div class="post-metadata">

**Author:** ![BulldogLowell](https://avatars.discourse-cdn.com/v4/letter/b/22d042/32.png) [@BulldogLowell](https://forum.arduino.cc/u/BulldogLowell)\
**Post date:** [February 8, 2016, 12:31pm UTC](https://forum.arduino.cc/t/rolling-array/364063/2 "2016-02-08T12:31:13Z")

</div>

from [Rob's class](http://playground.arduino.cc/Main/RunningAverage):

Are you sure there is a value in element 80?

```cpp
// 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:

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

```

which element is the last?

---

<div class="post-metadata">

**Author:** ![Chuoby](https://avatars.discourse-cdn.com/v4/letter/c/7cd45c/32.png) [@Chuoby](https://forum.arduino.cc/u/Chuoby)\
**Post date:** [February 8, 2016, 12:43pm UTC](https://forum.arduino.cc/t/rolling-array/364063/3 "2016-02-08T12:43:31Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![BulldogLowell](https://avatars.discourse-cdn.com/v4/letter/b/22d042/32.png) [@BulldogLowell](https://forum.arduino.cc/u/BulldogLowell)\
**Post date:** [February 8, 2016, 12:57pm UTC](https://forum.arduino.cc/t/rolling-array/364063/4 "2016-02-08T12:57:03Z")

</div>

> 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:

```arduino
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

---

<div class="post-metadata">

**Author:** ![Chuoby](https://avatars.discourse-cdn.com/v4/letter/c/7cd45c/32.png) [@Chuoby](https://forum.arduino.cc/u/Chuoby)\
**Post date:** [February 8, 2016, 5:49pm UTC](https://forum.arduino.cc/t/rolling-array/364063/5 "2016-02-08T17:49:11Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Whandall](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/whandall/32/222457_2.png) [@Whandall](https://forum.arduino.cc/u/Whandall)\
**Post date:** [February 8, 2016, 7:28pm UTC](https://forum.arduino.cc/t/rolling-array/364063/6 "2016-02-08T19:28:09Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [February 8, 2016, 7:36pm UTC](https://forum.arduino.cc/t/rolling-array/364063/7 "2016-02-08T19:36:02Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![BulldogLowell](https://avatars.discourse-cdn.com/v4/letter/b/22d042/32.png) [@BulldogLowell](https://forum.arduino.cc/u/BulldogLowell)\
**Post date:** [February 8, 2016, 10:14pm UTC](https://forum.arduino.cc/t/rolling-array/364063/8 "2016-02-08T22:14:59Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 5, 2021, 9:43pm UTC](https://forum.arduino.cc/t/rolling-array/364063/9 "2021-05-05T21:43:10Z")

</div>

Closed on May 5, 2021, 9:43pm UTC


