Click here to Skip to main content
15,886,137 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have created an application to read the accelerometer data . It shows different values for x-axis, y-axis as well as z-axis. I want to save this data into log files so that I can retrieve them and analyze the data. Also the values which I am getting are changing very fast. Please help me to keep these values in an SD-card or log file, also how to slow down the changing values??
Java
public class MainActivity extends Activity implements SensorEventListener {
private float mLastX, mLastY, mLastZ;
private boolean mInitialized;
private SensorManager mSensorManager;
private Sensor mAccelerometer;
private float NOISE;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mInitialized = false;
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);
}
protected void onResume() {
super.onResume();
mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);
}
protected void onPause() {
super.onPause();
mSensorManager.unregisterListener(this);
}



@Override
public void onSensorChanged(SensorEvent event) {
    // TODO Auto-generated method stub
    TextView tvX= (TextView)findViewById(R.id.x_axis);
    TextView tvY= (TextView)findViewById(R.id.y_axis);
    TextView tvZ= (TextView)findViewById(R.id.z_axis);
    ImageView iv = (ImageView)findViewById(R.id.image);
    float x = event.values[0];
    float y = event.values[1];
    float z = event.values[2];
    if (!mInitialized) {
    mLastX = x;
    mLastY = y;
    mLastZ = z;
    tvX.setText("0.0");
    tvY.setText("0.0");
    tvZ.setText("0.0");
    mInitialized = true;
    } else {
    float deltaX = Math.abs(mLastX - x);
    float deltaY = Math.abs(mLastY - y);
    float deltaZ = Math.abs(mLastZ - z);
    if (deltaX < NOISE) deltaX = (float)0.0;
    if (deltaY < NOISE) deltaY = (float)0.0;
    if (deltaZ < NOISE) deltaZ = (float)0.0;
    mLastX = x;
    mLastY = y;
    mLastZ = z;
    tvX.setText(Float.toString(deltaX));
    tvY.setText(Float.toString(deltaY));
    tvZ.setText(Float.toString(deltaZ));
    iv.setVisibility(View.VISIBLE);
    if (deltaX > deltaY) {
    iv.setImageResource(R.drawable.horizontal);
    } else if (deltaY > deltaX) {
    iv.setImageResource(R.drawable.vertical);
    } else {
    iv.setVisibility(View.INVISIBLE);
    }
    }

}

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
    // TODO Auto-generated method stub

}

}
Posted

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



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