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...
Feel free to comment if you can add help to this page or point out issues and solutions you have found. I do not provide support on this site, if you need help with a problem head over to stack overflow.

Comments

Your email address will not be published. Required fields are marked *