SparkFun Line Follower Array Hookup Guide

Pages
Contributors: MTaylor
Favorited Favorite 4

Extra Library Function: The Circular Buffer

The arduino library actually contains two classes. The first (discussed above) does all the reading and configuration of the actual sensor. The second is a data structure for creating and holding a buffer of data.

The structure is a circular buffer where, when full, new data overwrites the oldest data and all access to the data is referenced from the newest piece of data.

Object Construction

Construct the buffer objects in the global scope.

Arguments:

Pass the maximum size of the buffer to create. Argument is type unsigned 16 bit integer, but size must be limited to the size of memory available.

getPosition() takes no arguments.

Example:

language:c
#define CBUFFER_SIZE 100

//...

CircularBuffer positionHistory(CBUFFER_SIZE);

getElement()

Read the data in the buffer at some depth from newest.

Arguments:

Pass the element number to get as unsigned 16 bit integer. The newest element is referenced as 0.

Return:

Element value as signed integer.

pushElement()

Add a new piece of data to the buffer.

Arguments:

Pass the value to push into the buffer as signed 16 bit integer.

pushElement returns void.

Example:

language:c
//Read data from the sensor and put it in the buffer.
positionHistory.pushElement( mySensorBar.getPosition());

averageLast()

Average some number of the most recent entries.

Arguments:

Pass number of elements to average as unsigned 16 bit integer.

Return:

The mathematical average of the newest elements as signed 16 bit integer.

Example:

language:c
//Get an average of the last 'n' readings
int16_t avePos = positionHistory.averageLast( 10 );

recordLength()

recordLenght() takes no arguments.

Return:

Number of elements currently in the buffer.

When working with a partially filled buffer, this will report how many entries have been pushed in. When the buffer is full, this reports the total size of the buffer.