Skip to content

Commit 48f5496

Browse files
committed
+ version 0.2.06
+ all size vars not 8 bit + minor refactor + added sample counter to ra_test.ino (easier to compare output)
1 parent 394c4fd commit 48f5496

File tree

3 files changed

+26
-18
lines changed

3 files changed

+26
-18
lines changed

libraries/RunningAverage/RunningAverage.cpp

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
//
22
// FILE: RunningAverage.cpp
33
// AUTHOR: Rob Tillaart
4-
// VERSION: 0.2.05
5-
// DATE: 2014-dec-16
4+
// VERSION: 0.2.06
5+
// DATE: 2015-mar-07
66
// PURPOSE: RunningAverage library for Arduino
77
//
8-
// The library stores the last N individual values in a circular buffer,
8+
// The library stores N individual values in a circular buffer,
99
// to calculate the running average.
1010
//
1111
// HISTORY:
@@ -18,16 +18,17 @@
1818
// 0.2.03 - 2013-11-31 getElement
1919
// 0.2.04 - 2014-07-03 added memory protection
2020
// 0.2.05 - 2014-12-16 changed float -> double
21+
// 0.2.06 - 2015-03-07 all size uint8_t
2122
//
2223
// Released to the public domain
2324
//
2425

2526
#include "RunningAverage.h"
2627
#include <stdlib.h>
2728

28-
RunningAverage::RunningAverage(int n)
29+
RunningAverage::RunningAverage(uint8_t size)
2930
{
30-
_size = n;
31+
_size = size;
3132
_ar = (double*) malloc(_size * sizeof(double));
3233
if (_ar == NULL) _size = 0;
3334
clear();
@@ -44,15 +45,18 @@ void RunningAverage::clear()
4445
_cnt = 0;
4546
_idx = 0;
4647
_sum = 0.0;
47-
for (int i = 0; i< _size; i++) _ar[i] = 0.0; // needed to keep addValue simple
48+
for (uint8_t i = 0; i< _size; i++)
49+
{
50+
_ar[i] = 0.0; // keeps addValue simple
51+
}
4852
}
4953

5054
// adds a new value to the data-set
51-
void RunningAverage::addValue(double f)
55+
void RunningAverage::addValue(double value)
5256
{
5357
if (_ar == NULL) return;
5458
_sum -= _ar[_idx];
55-
_ar[_idx] = f;
59+
_ar[_idx] = value;
5660
_sum += _ar[_idx];
5761
_idx++;
5862
if (_idx == _size) _idx = 0; // faster than %
@@ -76,10 +80,11 @@ double RunningAverage::getElement(uint8_t idx)
7680
// fill the average with a value
7781
// the param number determines how often value is added (weight)
7882
// number should preferably be between 1 and size
79-
void RunningAverage::fillValue(double value, int number)
83+
void RunningAverage::fillValue(double value, uint8_t number)
8084
{
81-
clear();
82-
for (int i = 0; i < number; i++)
85+
clear(); // TODO conditional? if (clr) clear();
86+
87+
for (uint8_t i = 0; i < number; i++)
8388
{
8489
addValue(value);
8590
}

libraries/RunningAverage/RunningAverage.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
//
22
// FILE: RunningAverage.h
33
// AUTHOR: Rob dot Tillaart at gmail dot com
4-
// VERSION: 0.2.05
5-
// DATE: 2014-dec-16
4+
// VERSION: 0.2.06
5+
// DATE: 2015-mar-07
66
// PURPOSE: RunningAverage library for Arduino
77
// URL: http://arduino.cc/playground/Main/RunningAverage
88
// HISTORY: See RunningAverage.cpp
@@ -17,20 +17,20 @@
1717
#ifndef RunningAverage_h
1818
#define RunningAverage_h
1919

20-
#define RUNNINGAVERAGE_LIB_VERSION "0.2.05"
20+
#define RUNNINGAVERAGE_LIB_VERSION "0.2.06"
2121

2222
#include "Arduino.h"
2323

2424
class RunningAverage
2525
{
2626
public:
2727
RunningAverage(void);
28-
RunningAverage(int);
28+
RunningAverage(uint8_t);
2929
~RunningAverage();
3030

3131
void clear();
3232
void addValue(double);
33-
void fillValue(double, int);
33+
void fillValue(double, uint8_t);
3434

3535
double getAverage();
3636

libraries/RunningAverage/examples/ra_test/ra_test.ino

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//
22
// FILE: runningAverageTest.pde
33
// AUTHOR: Rob Tillaart
4+
// VERSION: 0.1.01
45
// DATE: 2012-12-30
56
//
67
// PUPROSE: show working of runningAverage
@@ -25,13 +26,15 @@ void loop(void)
2526
long rn = random(0, 1000);
2627
myRA.addValue(rn * 0.001);
2728
samples++;
28-
Serial.print("Running Average: ");
29+
Serial.print(samples);
30+
Serial.print("\t Running Average: ");
2931
Serial.println(myRA.getAverage(), 3);
3032

3133
if (samples == 300)
3234
{
3335
samples = 0;
3436
myRA.clear();
37+
Serial.println();
3538
}
36-
delay(100);
39+
delay(10);
3740
}

0 commit comments

Comments
 (0)