Search code examples
androidperformancemathaccelerometerrate

How can I find out how fast my Android device is moving using an Accelerometer?


I'm new to programming with accelerometers on Android.

I want to know how fast my device is moving in any direction using the accelerometer on my Android. I have googled and found code on doing different things based on the direction in which the device is moving, but there's nothing that talking about doing different things based on the rate at which my device moves.

Can somebody help with this?


Solution

  • An accelerometer only measures the rate of change of velocity. (This is the definition of acceleration.) So the only way to use it to tell velocity is to know actual velocity V_0 at some time t_0 and then record dV_i = A_i*(t_i - t_(i-1)) for successive times t_1,t_2,... where A_i is the acceleration at t_i. Then velocity at t_n is V_0 + sum_(i=1,n)(dV_i).

    This is called integration and it's what made Isaac Newton (among others) famous.

    This calculation is very vulnerable to error. I'd say it's not even worth trying for fun.

    A more useful way to measure velocity is change in position (from the location sensor) divided by time.