Move buffer down one place
//Move the buffer down 1
for (Count = 0; Count < (MY_BUFFER_SIZE - 1); Count++)
{
MyBuffer[Count] = MyBuffer[(Count + 1)];
}
MyBuffer[(MY_BUFFER_SIZE - 1)] = SomeNewValueBeingAdded;
Move buffer up one place
//Move the buffer up 1
for (Count = (MY_BUFFER_SIZE - 1); Count > 0; Count--)
{
MyBuffer[Count] = MyBuffer[(Count - 1)];
}
MyBuffer[0] = SomeNewValueBeingAdded;
Rolling sample buffer
const int MY_BUFFER_SIZE = 2048;
static int MyBufferNextValueIndex = 0;
static uint16_t MyBuffer[MY_BUFFER_SIZE];
static bool MyBufferIsFull = 0;
int Count;
int MaxIndex;
uint32_t Value;
//Add the new value to the buffer
MyBuffer[MyBufferNextValueIndex] = analogRead(A0); //<<<< Add your value to buffer
MyBufferNextValueIndex++;
if (MyBufferNextValueIndex >= MY_BUFFER_SIZE)
{
MyBufferNextValueIndex = 0;
MyBufferIsFull = 1;
}
//Get the current average value
MaxIndex = MY_BUFFER_SIZE;
if (!MyBufferIsFull)
MaxIndex = MyBufferNextValueIndex;
Value = 0;
for (Count = 0; Count < MaxIndex; Count++)
Value += MyBuffer[Count];
Value /= MaxIndex;
//Do something with it...
USEFUL?
We benefit hugely from resources on the web so we decided we should try and give back some of our knowledge and resources to the community by opening up many of our company’s internal notes and libraries through resources like this. We hope you find it helpful.
Please feel free to comment if you can add help to this page or point out issues and solutions you have found, but please note that we do not provide support here. If you need help with a problem please use one of the many online forums.