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 Implementing a Psuedo-Pedometer - Unity Answers

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 :slight_smile:

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

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.

Check this link , i found it on google