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);
}
}