CRTC Electronics

Click here to edit subtitle

Accelerometer

Here is a very clear and short video on how to code the accelerator:

https://www.youtube.com/watch?v=MwH0z1HIxog


MainActivity

add  implements SensorEventListener




make these three veriables


public class MainActivity extends IOIOActivity implements SensorEventListener {




Sensor accelerometer_;

SensorManager sm_;

TextView acceloration_;

onCreate

add this code to onCreate

sm_ = (SensorManager)getSystemService(SENSOR_SERVICE);

accelerometer_ = sm_.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);

sm_.registerListener(this, accelerometer_, SensorManager.SENSOR_DELAY_NORMAL);

acceloration_ = (TextView)findViewById(R.id.tvAccel);

onSensorChanged

add this code to onSensorChanged

double sum_ = event.values[0] +event.values[1] + event.values[2];

acceloration_.setText("X: "+ event.values[0] +

"\nY: "+ event.values[1] +

"\nZ: "+ event.values[2] +

"\nTotal: " + sum_

);