Skip to content

Instantly share code, notes, and snippets.

@iewnait
Created March 14, 2012 19:04
Show Gist options
  • Save iewnait/2038706 to your computer and use it in GitHub Desktop.
Save iewnait/2038706 to your computer and use it in GitHub Desktop.
WIMM Accelerometer sample code
@Override
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
float[] values = event.values;
// Movement
float xAxisValue = values[0];
float yAxisValue = values[1];
float zAxisValue = values[2];
float resultant = (float) Math.sqrt(Math.pow(xAxisValue, 2)+
Math.pow(yAxisValue, 2));
if(Math.abs(zAxisValue)<8 && !startedActivity){
//jump back to spiritLevelActivity;
startedActivity = true;
startActivity(new Intent(this, TubeSpiritLevelActivity.class));
finish();
}
else{
float angle = (float) Math.asin(yAxisValue/resultant);
sDegreeHistory.addDegree(angle);
sAccelerometerHistory.addDataSet(xAxisValue, yAxisValue, zAxisValue);
if (mCircularSpiritLevelView != null) {
mCircularSpiritLevelView.invalidate();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment