Reputation: 11
I have a small project, in need to detect the movement of the device Ex: i have my mobile held on a table if i move the device left "like moving a PC mouse", i need to know if i move it right, i need to know if i move it forward, and backward i need to know
I searched and used many tutorials, but all the tutorials gave me numbers that changes a lot even when the device is not moved!! i tried this
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER){
mGravity = event.values.clone();
// Shake detection
float x = mGravity[0];
float y = mGravity[1];
float z = mGravity[2];
mAccelLast = mAccelCurrent;
mAccelCurrent = (float)Math.sqrt(x * x + y * y + z * z);
float delta = mAccelCurrent - mAccelLast;
mAccel = mAccel * 0.9f + delta;
// Make this higher or lower according to how much
// motion you want to detect
if(mAccel > 0.0005){
((TextView)findViewById(R.id.XField)).setText("X:" + String.valueOf((x)));
((TextView)findViewById(R.id.YField)).setText("Y:" + String.valueOf((y)));
((TextView)findViewById(R.id.ZField)).setText("Z:" + String.valueOf(z));
}
}
thanks
Upvotes: 0
Views: 685
Reputation: 3804
From Android Documentation:
Measures the acceleration force in m/s2 that is applied to a device on all three physical axes (x, y, and z), including the force of gravity.
i.e.accelerometer detects acceleration, not movement, not speed and not direction. In a nutshell: if your movement will be at constant speed the accelerometer will show 0. You need to write an algorithm that will parse speed and/or direction from the acceleration. It involves a lot of study about physics and mathematics. In a nutshell: integral of acceleration is speed, second integral of acceleration is direction. (Of course it is much more complex, but just to give you direction of where to start exploring).
Upvotes: 1