# walking pace (code)

hi guys,

for school I want to make a little thing that measures your walking pace (like the frequency). we have an FSR which you stick under your foot so it measures if you take a step. we tried this and it works, by tweaking the threshold. so if the output value of the FSR exceeds 400, it means you are standing on the FSR (e.g. your taking a step). now our only problem is that we don't know how (and if) we are able to let the arduino measure the time between steps, and compute that into a frequency.

can anyone help us with the code??

So you could basically detect when the sensor value goes over a certain threshold and when it goes under the treshold, and store those times. You could then take the average of these and which would be the time of the footstep. Now wait until the next step and you got the delta time between steps. The Frequency is then just 1 over the delta time.

Sample code:

``````bool entered = false;
long step_start;
long last_step;

long dt;
float frequency;

void loop() {
int sensor_data = get_sensor_data();
if(!entered && sensor_data > THRESHOLD) {
entered = true;
step_start = millis();
} else if (entered && sensor_data < THRESHOLD) {
entered = false;
long step = (millis() + step_start) / 2;
dt = step - last_step;
frequency = 1.0 / dt;
}
}
``````

Thanks for the fast response! It's helpful, though I don't understand what the value of last_step is?

I suspect the value you're really looking for is the period or interval between steps (or indeed the average), because the frequency will be very low. Both point in the same direction of the time between events (steps), but it may help you understand the programming if you look for the most relevant term / metric to be measured.

This might be a little easier to read:

``````bool FootDown = false;
unsigned long PreviousFootDown = 0;
const int FootFSRPin = A0;

void setup() {
Serial.begin(19200);
}

void loop() {
if (!FootDown && sensor_data > THRESHOLD) {
FootDown = true;
unsigned long footDownTime = millis();
if (PreviousFootDown != 0) {
// Calculate how many milliseconds between footfalls
unsigned long millisPerPace = footDownTime - PreviousFootDown;
float pacesPerMinute = 60000.0 / millisPerPace;
// But that's for one foot.  Double for steps.
float stepsPerMinute = pacesPerMinute * 2;
Serial.print("Steps per minute: ");
Serial.println(stepsPerMinute);
}
PreviousFootDown = footDownTime;
} else
FootDown = false;
}
``````