I’ve just made a new template library called AverageList.
It will help averaging a set of values of the same datatype.
API:
-
addValue(datatype value)
-
//add a value to the AverageList
-
getAverage() : datatype
-
//return the average of all values that are added with an addValue call
-
getTotalAverage() : datatype
-
//return the average of all values in the AverageList
-
getMin() : datatype
-
//return the low peak of the values in the set
-
getMax() : datatype
-
//return the high peak of the values in the set
-
reset()
-
//reset the AverageList, discard old values
-
operator=
-
//reset and assign
-
operator+=
-
//add value to AverageList
-
operator-=
-
//remove value from AverageList if present
-
operator
-
//subscript operator, read and/or assign
Short Example:
This example will take 20 readings from analog pin 2 and calculate the average, then send it to the serial console.
#include <AverageList.h>
const byte LIST_SIZE = 20;
void setup(){
//enable user feedback
Serial.begin(9600);//this is used for storage, an array of ints
int arr[LIST_SIZE] = {0};//initialize an AverageList object that operates on ints
AverageList testList(arr,LIST_SIZE);//take samples
for(int i=0; i<LIST_SIZE; i++){
testList = analogRead(2);
delay(20);*
}*
Serial.println( testList.getTotalAverage());*
}
void loop(){/nothing to loop/}
[/quote]
Example Sketch:
A more thorough investigation of the API and operator overloading.
```
*#include <AverageList.h> //http://www.arduino.cc/playground/uploads/Code/AverageList.zip
const byte LIST_SIZE = 5;
void setup(){
//enable user feedback
Serial.begin(9600);
//this is used for storage, an array of ints
int arr[LIST_SIZE] = {0};
//initialize an AverageList object that operates on ints
AverageList testList(arr,LIST_SIZE);
//perform operations on the AverageList
testList.addValue(2); //add 2 to the AverageList
testList.addValue(4); //add 4 to the AverageList
testList.addValue(6); //add 6 to the AverageList
//average should be (2+4+6)/3 = 4
Serial.print("Avarage of the values inserted into testList: ");
Serial.println( testList.getAverage() );
//perform operations on the AverageList
testList.reset(); //reset AverageList to 0
testList+=1; //add 1 to the AverageList
testList+=3; //add 3 to the AverageList
//average should be (1+3)/2 = 2
Serial.print("Avarage of the values inserted into testList: ");
Serial.println( testList.getAverage() );
//perform operations on the AverageList
testList=1; //reset AverageList to 1
testList+=3; //add 3 to the AverageList
testList+=5; //add 5 to the AverageList
testList-=4; //try to remove 4 from the AverageList, but no item in the list is equal to 4, therefore take no action
testList+=12; //add 12 to the AverageList
//average should be (1+3+5+12)/4 = 5,25 = (int) 5
Serial.print("Avarage of the values inserted into testList: ");
Serial.println( testList.getAverage() );
//total average should be (1+3+5+12+0)/5 = 4,2 = (int) 4
Serial.print("Avarage of testList: “);
Serial.println( testList.getTotalAverage() );
//perform operations on the AverageList
testList[4] = 12;
//total average should be (1+3+5+12+12)/5 = 6,6 = (int) 6
Serial.print(“Avarage of testList after ‘testList[4] = 12;’: “);
Serial.println( testList.getTotalAverage() );
Serial.print(”\nThe AverageList testList contains:\n\t”);
for(int i=0; i<LIST_SIZE; i++){
Serial.print( testList[i] , DEC );
Serial.print(”, ");
}
}
void loop(){/nothing to loop/}*
* *Example Sketch Serial Output:* *
*Avarage of the values inserted into testList: 4
Avarage of the values inserted into testList: 2
Avarage of the values inserted into testList: 5
Avarage of testList: 4
Avarage of testList after ‘testList[4] = 12;’: 6
The AverageList testList contains:
1, 3, 5, 12, 12*
```
Feedback Welcome!