How to create an average that changes every ten seconds

I am trying to operate a servo in such a way that serial input from another source can be used to tell the Arduino how to move the servo. The issue is that the serial input comes so quickly that the servo cannot keep up with the input. When I try to add a delay into the code, the data eventually backs up to the point that no new data comes in. So I am taking a slightly different approach.

I discovered that the Arduino is able to smooth out data. Is there a way to modify the smoothing code found at so that it collects stores ten readings, averages them, stores another ten readings, re-averages them, and so on? This way, I could just use the average to drive the servo. For example: 1,2,3,4,5,6,7,8,9,10 AVG: 5.5; 2,3,4,5,6,7,8,9,10,11 AVG:  6.5

This is the code I am currently using:

#include <Servo.h>

Servo servo1;
String inData;
int commaPosition;
String mes1;
String mes2;
int servoPin = 9;
char mesX[16];
char mesY[16];
float xPos;
float yPos;

void setup()
  Serial.begin(115200); //originally 9600
  Serial.write('Setup complete');

void loop()
  if(Serial.available() > 0)
      char received =;
      inData += received;
      commaPosition = inData.indexOf(',');
      if (received == '\n')
          if(commaPosition != -1)
           mes1 = inData.substring(0, commaPosition);
           mes2 = inData.substring(commaPosition + 1, inData.length()); 
      Serial.print("mes1: ");
      Serial.print("mes2: ");
      mes1.toCharArray(mesX, sizeof(mesX));
      float xPos = atof(mesX);
      Serial.print("xPos = ");
          inData = ""; //clear buffer              
      //add code here maybe                               
     if(xPos <= 0.56)
     else if(xPos > 0.56)


Thanks in advance.

something like

void loop()
  int sum = 0;
  for (int i=0; i< 10; i++) sum += analogRead(A0);
  sum = sum/10;

I would question a design that requires the servo to move faster than it is physically capable of moving. Having said that, you could consider sub-sampling rather than averaging. Only process every other serial input, or every third or fourth until you can keep up.

Or a simple low pass digital filter

float filtered = 0.0 ;

void process_sample (int value)
  filtered += 0.01 * (value - filtered) ;  // LPF with time constant of about 100 samples.

Then use filtered to set the servo drive at sensible intervals.