In this post we will see how to read different axis values using Accelerometer API.

To work with Accelerometer API first you need to add reference of Microsoft.Devices.Sensors

clip_image001

Then below namespace,

clip_image001[6]

Now to capture X Axis, Y Axis and Z Axis value you need to follow below steps

Step 1

Create task object of Accelerometer chooser

clip_image001[8]

Step 2

Call the callback method when user will complete a task and Implement the completed event to capture user data and status when user complete as tasks.

clip_image001[10]

Step 3

Call the start method to start the accelerometer,

clip_image001[12]

Step 4

We can read value of XAxis, YAxis and ZAxis as below along with timestamp of capturing data as below in the completed event. We are changing double values to string.

clip_image001[14]

You can save these data for further use that when accelerometer value got changed.

Let us run the application in debug mode to see value got captured,

image

When you click on Capture Data button control will go to Accelerometer reading changed event. Then each time you will move the ball Accelerometer reading changed event will get called.

image

For your reference full code is as below,


using System;
using System.Windows;
using Microsoft.Phone.Controls;
using Microsoft.Devices.Sensors;

namespace EncryptingandDecryptinginMango
{
public partial class MainPage : PhoneApplicationPage
{
public MainPage()
{
InitializeComponent();
}
void accelerometerTaskObject_ReadingChanged(object sender, AccelerometerReadingEventArgs e)
{
string xCordinate = e.X.ToString("0.00");
string yCordinate = e.Y.ToString("0.00");
string zCordinate = e.Z.ToString("0.00");
DateTimeOffset timeCaptured = e.Timestamp;
}

private void btnCaptureData_Click(object sender, RoutedEventArgs e)
{

var accelerometerTaskObject = new Accelerometer();
accelerometerTaskObject.ReadingChanged +=
new EventHandler<AccelerometerReadingEventArgs>
(accelerometerTaskObject_ReadingChanged);

try
{
accelerometerTaskObject.Start();
}
catch
{
//Error in starting the Accelerometer
}

}
}
}

In this way you can work with Accelerometer API to capture different axis values. I hope this post was useful. Thanks for reading Smile

If you find my posts useful you may like to follow me on twitter http://twitter.com/debug_mode or may like Facebook page of my blog http://www.facebook.com/DebugMode.Net If you want to see post on a particular topic please do write on FB page or tweet me about that, I would love to help you.

Advertisements

4 thoughts on “Reading coordinate values using Accelerometer API in Windows Phone 7

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s