0

In my app I want to count for how long the accelerometer is over a certain threshold, but i'm unsure of how to do this.
The sensitivity of my accelerometer is set to 'SensorManager.SENSOR_DELAY_GAME' so the onSensorChanged() method is called approximately 20 times a second.

I have a flag called movementFlag that I'm setting to true whenever the accelerometer speed goes over a threshold, and setting back to false when it goes under the threshold.

So say over the space of an hour, I want to count up the total amount of time that the movementFlag is set to true, incrementing this time value each time it's set, and the same for when it's set to false.
I thought maybe I could have 2 values - moveStart and moveFinish - that I could set to System.currentTimeMillis() upon the transition of movementFlag from true to false and vice versa. Subtracting these values from each other would give me the amount of time the flag is set each time.
But I don't know how to do this just on the transition of a flag and not the whole time while a flag is set.
Can anyone tell me how to do this or another method to do what I want to do here? Thanks

Adinia
  • 3,722
  • 5
  • 40
  • 58
bobby123
  • 1,006
  • 4
  • 14
  • 24
  • Depending on what you'd like to do with this: Keep in mind that some phones will switch *off* the accelerometer when the screen is off (e.g. Motorola Milestone) so measuring movements may not work over a long period of time (only if the screen is on). – Select0r Mar 01 '11 at 16:20
  • @Select0r - Yea I know, a partial wakelock should work to get around that issue, but doesn't as I've tried and this Google error page shows [link]( http://code.google.com/p/android/issues/detail?id=3708) so at the moment I'm settling with a dimmed screen lock, chews through battery but can't do much else. – bobby123 Mar 01 '11 at 16:30

1 Answers1

1

You need to store two values to know when transition occurs, one which stores last value of your movementFlag (let's call it lastV), and one which stores value before that one (prevV). Then:

  1. On each sensor change event do: prevV = lastV; lastV = movementFlag
  2. If prevV!=lastV, then we have a change!!
  3. If LastV==true, it means start of the movement - remember start time (moveStart)
  4. If it is false, it is the end of the movement - remember end time (moveFinish) and subtract

This way, you may accumulate time on changes only.

dstefanox
  • 2,222
  • 3
  • 19
  • 21
  • I think I see where you're going with this using extra flags, but the method above wouldn't seem to work as I'd be setting prevV = lastV, and then in step 2, checking if prevV != lastV. Do you mean to check if prevV != movementFlag? – bobby123 Mar 01 '11 at 16:23
  • You missed part lastV = movementFlag after prevV = lastV. This way I manage to save previous value before comparing it to current. This should work for sure. – dstefanox Mar 01 '11 at 17:53
  • Woops i almost forgot about this, your method worked a treat. Cheers for that!! – bobby123 Mar 09 '11 at 04:02