title: Accelerometer values to degrees
body: <p>I wrote the following code,where the values of the accelerometer are shown in x,y,z during rotation. </p>
  
  <pre><code>public class MainActivity extends AppCompatActivity implements SensorEventListener {
  
  private TextView xText,yText,zText;
  private Sensor mySensor;
  private SensorManager SM;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
      //Creating the Sensor Manager
      SM = (SensorManager)getSystemService(SENSOR_SERVICE);
  
      // Accelerometer Sensor
      mySensor = SM.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
  
      // Register sensor Listener
      SM.registerListener(this, mySensor, SensorManager.SENSOR_DELAY_NORMAL);
  
      // Assign TextView
      xText = (TextView)findViewById(R.id.xText);
      yText = (TextView)findViewById(R.id.yText);
      zText = (TextView)findViewById(R.id.zText);
  
  
  }
  
  @Override
  public void onSensorChanged(SensorEvent sensorEvent) {
  
  
  
      xText.setText("X: " + sensorEvent.values[0]);
      yText.setText("Y: " + sensorEvent.values[1]);
      zText.setText("Z: " + sensorEvent.values[2]);
  }
  
  @Override
  public void onAccuracyChanged(Sensor sensor, int i) {
  
    }
  }
  </code></pre>
  
  <p>Now I want to convert the values I get from the SensorEvents to degrees. I looked at various questions here,but I got confused. </p>
  
  <pre><code>    double x = sensorEvent.values[0];
      double y = sensorEvent.values[1];
      double z = sensorEvent.values[2];
  </code></pre>
  
  <p>There should be a formula that takes the above values and convert them in degrees.</p>
  
  <p>Any ideas?</p>
  
  <p>Thanks,</p>
  
  <p>Theo.</p>
  
  <p><strong>EDIT</strong></p>
  
  <pre><code>@Override
  public void onSensorChanged(SensorEvent sensorEvent) {
      //xText.setText("X: " + sensorEvent.values[0]);
      //yText.setText("Y: " + sensorEvent.values[1]);
      //zText.setText("Z: " + sensorEvent.values[2]);
  
      double x = sensorEvent.values[0];
      double y = sensorEvent.values[1];
      double z = sensorEvent.values[2];
  
      double pitch = Math.atan(x/Math.sqrt(Math.pow(y,2) + Math.pow(z,2)));
      double roll = Math.atan(y/Math.sqrt(Math.pow(x,2) + Math.pow(z,2)));
      //convert radians into degrees
      pitch = pitch * (180.0/3.14);
      roll = roll * (180.0/3.14) ;
  
      yText.setText(String.valueOf(pitch));
      zText.setText(String.valueOf(roll));
  }
  </code></pre>