Simple Accelerometer App For Windows 10 Universal App

In Windows 10 the accelerometer is used to detect the user movement or record the movement in a particular interval. Look at the below image to get a clear idea about accelerometer value change and how it works to identify user movements.

Accelerometer

Let’s see the steps

  1. Create a new Windows 10 universal app.

  2. Design the UI according to your wish; here I have created one button to start the accelerometer reading.

  3. Now go to the coding part and write the following code. Firstly, add the following namespace to access the accelerometer sensor API.

    using Windows.Devices.Sensors;


  4. Create one object for the accelerometer,

    Accelerometer myAccelerometer;

  5. Now get the default properties and reading by using GetDefault method.

    myAccelerometer = Accelerometer.GetDefault();


    Then set interval to read the value from certain interval,

    myAccelerometer.ReportInterval = 16;

    Now create one event accelerometer reading changes to handle the readings and assign it to your accelerometer.

    myAccelerometer.ReadingChanged += MyAccelerometer_ReadingChanged;

    Now start the application and get the values from the accelerometer.
Full code looks like the following code.
  1. public async void startReading()  
  2. {  
  3.     myAccelerometer = Accelerometer.GetDefault();  
  4.     if (myAccelerometer != null)  
  5.     {  
  6.   
  7.         uintminReportInterval = myAccelerometer.MinimumReportInterval;  
  8.         uintreportInterval = minReportInterval > 16 ? minReportInterval : 16;  
  9.         myAccelerometer.ReportInterval = reportInterval;  
  10.         myAccelerometer.ReadingChanged += MyAccelerometer_ReadingChanged;  
  11.     }  
  12. }  
  13.   
  14. private void MyAccelerometer_ReadingChanged(Accelerometer sender, AccelerometerReadingChangedEventArgsargs)  
  15. {  
  16.   
  17.     AccelerometerReading reading = args.Reading;  
  18.     string xAxis = String.Format("{0,5:0.00}", reading.AccelerationX);  
  19.     string yAxis = String.Format("{0,5:0.00}", reading.AccelerationY);  
  20.     string zAxis = String.Format("{0,5:0.00}", reading.AccelerationZ);  
  21.   
  22. }  
To read the accelerometer value use AccelerometerReading class.
 
Read more articles on Windows 10:

Up Next
    Ebook Download
    View all
    Learn
    View all