Finding Average of a changing variable

I'm trying to create a Step Tracker for Arduino Using a force sensitive resistor. So far I have my code set up so that I can calculate the time between steps, but what I would like to do is take about 30 of those values and calculate the average of them to get a better estimate of how many steps per minute are being taken over a period of time. The problem is that the time between steps (or pace) is constantly being changed and re-written to the same variable every time the user takes a step. How could I best do this? I tried using an array but I think my implementation is wrong because my findAvg() method just returns infinite every time. Any advice/answers would be appreciated.

int fsr = 0;

int fsrReading;

int count = 0;

unsigned long startTime;

unsigned long endTime;

double duration;

byte timerRunning;

double pace;

double avg[30];

int count1 = 0;

double avgPace;



void setup() {
 // put your setup code here, to run once:


 Serial.begin(9600);
 

}

void loop() {
 
 // put your main code here, to run repeatedly:

fsrReading = analogRead(fsr);


if(fsrReading > 600) {
 if(count1 > 30) {
   count1 = 0;
 }
 else {
   count1++;
 }
 
 count= count + 2;
 pace = 120/duration;

 
 Serial.println(pace);
 Serial.println(count);
 
 
 
avg[count1] = pace;

 
 delay(500);
}



stepsPerMinute(); //calculating steps per minute


 
 }


void findAvg() {

 double sum;

 int elements = sizeof(avg) / sizeof(avg[0]);
 
 if(elements < 25) {
   return;
 }
 else {
   for(int i = 0; i < 30; i++) {
     sum = sum + avg[i];
   }
   avgPace = sum;

 Serial.println("Average pace = "); Serial.print(avgPace);
 }


 }


 



 void stepsPerMinute() {

if (timerRunning == 0 && fsrReading < 500){  
 startTime = millis();
 timerRunning = 1;
 }
 if (timerRunning == 1 && fsrReading > 600){ 
 endTime = millis();
 timerRunning = 0;
 duration = endTime - startTime;
 Serial.println();
 //Serial.print ("Time between a step for one foot: ");
 duration = duration / 1000;
 //Serial.println (duration);
 

 } 

}

The forum mangled your code post due to its not being enclosed in code tags. Repost thusly:

Simplest will probably be to just count the steps during one minute; below the basics.

uint32_t startTime;

void setup()
{
  startTime = millis();
}

void loop()
{
  uint32_t currentTime = millis();

  if (currentTime - startTime < 60000UL)
  {
    // count steps
    ...
    ...
  }
  else
  {
    // reset start time for next minute interval
    startTime = currentTime;
    // display steps per minute
    ...
    ...
  }
}

Please read How to use this forum - please read., specifically point #7 about posting code. It will display code as above and it will prevent that e.g. code is printed in italics, contains smileys etc.

How about every time you register a step, yes you put the value in an array and add it to the total value. Once steps reach 30 let the pointer to the next storage spot roll back like durationPointer=durationPointer%30; (actually this will prevent you from writing beyond the size as well) read the duration stored there, substract this from the total, then add the new value and store it. and divide by 30.
all in all it should look something like this

uint16_t avgStepLength(uint16_t thisStep, uint16_t totalSteps) {
  static uint16_t stepDurations[30];
  static uint32_t totalDuration=0;
  uint8_t durationPointer=(totalSteps-1)%30; // after 1 step we want to point to 0
  uint8_t divider=totalSteps;
  if (totalSteps>30) {
    divider=30;
    totalDuration=totalDuration-stepDurations[durationPointer];
  }
  totalDuration=totalDuration+thisStep;
  stepDurations[durationPointer]=thisStep;
  return totalDuration/divider;
}

I haven't tested this but it does compile.