Click here to Skip to main content
15,070,974 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi All,

I am developing an android app that starts a timer when the user is moving. The timer is starting ass planned, using the accelerometer. The problem, however is to get the timer to stop when the user stops. This should be when the user is at a complete standstill. I am having difficulty with it because if you move too slow the timer stops, or sometimes it stops and I just don't even know why.

Here is my code, which is not working as planned:

Java
@Override
    public void onSensorChanged(SensorEvent event) {
        // get the change of the x,y,z values of the accelerometer
        deltaX = Math.abs(lastX - event.values[0]);
        deltaY = Math.abs(lastY - event.values[1]);
        deltaZ = Math.abs(lastZ - event.values[2]);

        //Zero out gravity on the accelerometer.
        gDeltaX = (float)((0.9 * gDeltaX) + (0.1 * deltaX));
        gDeltaY = (float)((0.9 * gDeltaY) + (0.1 * deltaY));
        gDeltaZ = (float)((0.9 * gDeltaZ) + (0.1 * deltaZ));

        deltaX = deltaX - gDeltaX;
        deltaY = deltaY - gDeltaY;
        deltaZ = deltaZ - gDeltaZ;

        //Make sure process starts only once
        if (flag == 0) {
            if ((deltaX > 10) || (deltaY > 10) || (deltaZ > 10)) {
                //Start the timer
                startTime = SystemClock.uptimeMillis();
                timeHandler.postDelayed(updateTimerMethod, 0);
                flag = 1;
            }
        } else if (flag == 1) {
            if ((deltaX < 1) && (deltaY < 1) && (deltaZ < 1)) {
                //Stop the timer.
                timeSwap += timeInMillies;
                timeHandler.removeCallbacks(updateTimerMethod);
                flag = 2;
            }
        }
    }


If there is any alternate way of stopping the timer, rather than using the accelerometer when it has no movement, I am all open to suggestions. I was thinking the the accelerometer can be the initiallizer, but what to use when stopping??
Posted

1 solution

When you ask about "moving", the question would be: moving relative to what? :-)

Apparently, it is impossible to tell the speed by an accelerometer; it is prohibited by the fundamental physical principles: all phenomena in different reference system moving at uniform speeds go in the same way, so you cannot detect any difference between them from inside the system. In other words, you would detect the same acceleration in systems at different coordinates and speeds, it is principally impossible to tell one from another.

I understand that you are doing a different thing: really detecting acceleration, not speed. This would be enough to detect the moment of time when some motion has started, if the previous state was pretty much complete rest. But what happens when one stops? Generally, it's no different from starting to move: the device just experience different acceleration. This is not enough? Let's consider that your user never change the orientation of the device (a questionable assumption itself), and initial acceleration is positive, then, to come to complete stop, you would need equivalent amount of acceleration with negative sign. But what is "equivalent amount"? Quite apparently, not identical acceleration profile, but some acceleration with the same first integral of motion, speed. Note that you would have to integrate the motion during all the motion, without any stops. It cannot be done precisely, by many reasons. In all cases, integrated values may accumulate the errors.

Instead of trying to find out the speed, you may do something very different. For example, get a statistic of the average acceleration level during some period of time. In other words, detect the vibrations at the frequencies and amplitudes detectable by an accelerometer. Assume some low level of such vibrations as "no motion".

—SA
   
v2
Comments
Christopher Smit 27-Jul-15 16:46pm
   
How do I detect these frequencies and amplitudes? I am very new to android development. This is my very first project and I have only been coding in this language for a month now.
Sergey Alexandrovich Kryukov 27-Jul-15 17:02pm
   
This is not a simple problem at all. You have to experiment with measurements of accelerations at a regular time intervals and different conditions. Look at the data provided in the SensorEvent event object the way you already did, event.values, and so on. Repeat all the recordings for a while, get some statistics, do it at rest, then in motion at different "road conditions", speeds, the ways you hold the device. Draw your conclusions on some typical characteristics, think at them... Try to develop some criteria to tell the motion from the rest.
—SA
Christopher Smit 28-Jul-15 1:02am
   
That seems near impossible... Lol you wouldn't happen to know how the "Nike Running" app is calculating when the user is running? Or maybe, can I use the accelerometer just to start the timer and the track whether the user is stil moving using geolocation? Like I said I am really new to Android so I need any simple alternative..
   
No, just the opposite: it looks very realistic, just the detection of motion I described. But the integration of acceleration to calculate speed and coordinate is not realistic at all, due to principle accuracy problem. I have no idea what this application does.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900