- Home /
Implementing the pedometer to count step with Unity3d
I have tried to implement a pedometer with Unity3d in C# language. The code below is my code which I learn from http://answers.unity3d.com/questions/160106/implementing-a-psuedo-pedometer.html
I test this code with the accelerometer frequency of 30Hz.
I have some problems with the accuracy of this pedometer. When I try to test it in my skinny jean pocket, the step count doesn't work at all. Anyone here please help me to find the solution. Thank you very much :)
private float loLim = 0.005F;
private float hiLim = 0.3F;
private int steps = 0;
private bool stateH = false;
private float fHigh = 8.0F;
private float curAcc= 0F;
private float fLow = 0.2F;
private float avgAcc;
public int stepDetector(){
curAcc = Mathf.Lerp (curAcc, Input.acceleration.magnitude, Time.deltaTime * fHigh);
avgAcc = Mathf.Lerp (avgAcc, Input.acceleration.magnitude, Time.deltaTime * fLow);
float delta = curAcc - avgAcc;
if (!stateH) {
if (delta > hiLim) {
stateH = true;
steps++;
} else if (delta < loLim) {
stateH = false;
}
stateH = false;
}
avgAcc = curAcc;
calDistance (steps);
return steps;
}
What exactly doesnt work?
Does it give you 0 steps?
Are you sure the hiLim and loLim are correct for the data you get?
I tested with the other types of pants which is not too fit and the result closes with the real step. For the data of hiLim and loLim, I change it from the experiment that I tried to do.
Answer by sarebots2 · Jul 04, 2016 at 01:33 PM
You logic withing the if statement is incorrect. It should be something like:
if (!stateH) {
if (delta > hiLim) {
stateH = true;
steps++;
}
} else {
if (delta < loLim) {
stateH = false;
}
}
Also make sure you are calling the stepDetector function at every frame, preferably using FixedUpdate.