Getting to Know Accelerometer (Silverlight For Windows Phone)

Accelerometer is a component to measure acceleration in such a way that it can detect changes in the device’s position and the magnitude of the change. Microsoft requires every Windows Phone manufacturer to put this sensor in every device that supports Windows Phone. This way, a change in the device’s physical position can be used as an alternative for users to interact with a Windows Phone application.

This component gives as a new user experience in interacting with the device’s movement. This can be used not only for standard applications but especially for games also.

To use accelerometer, you need to import Microsoft.Devices.Sensors dll.

It’s pretty simple to use; there are two basic functions, Start and Stop, and an event to handle, which is ReadingChanged. Accelerometer detects a change in the x, y, and z dimensions. Using the value it detects, we can write certain code that reacts depending on the change.

tmp1C-149_thumb

The device’s axis does not change on orientation change. The Y axis is always from the top to the bottom of the device, perpendicular to the three hardware buttons, the X axis is always from one side of the device to another, parallel to the three hardware button, while the Z axis is a virtual axis that goes through the device, assuming we are holding the device and looking at it. The value we can get ranges from -1 to 1.


tmp1C-150_thumb

For reading schema, take a look at these illustrations of result values from the accelerometer:

tmp1C-151_thumb

 

 

 

tmp1C-152

Now we will learn how to retrieve data from the sensor.

1. If you continue from the previously made project, then add a page to learn about Accelerometer. Otherwise, create a new project for this purpose. Having been doing exercises up to this page, it should be fairly easy to do. The following example uses a previously existing project. Right click on the project, Add New Item, select Windows Phone Portrait Page and rename the file, in this example Accelerometer.xaml then select Add.

tmp1C-153

2. Add a button and a TextBlock so that it looks like this:

tmp1C-154

3. Add a reference to Microsoft.Devices.Sensors assembly. On Solution Explorer window, right click on Reference and select Add New Reference

tmp1C-155

4. Add the following code:

tmp1C-156_thumb

Don’t forget to add assembly usage reference. On the next part the Accelerometer object id defined for us to use later.

5. Add an event handler on the button using this code:

tmp1C-157_thumb

 

tmp1C-158_thumb

After instantiating accelerometer, add a handler to handle data change from the sensor.

6. Because data changes are sent constantly and this may happen during thread runtime, in fetching data we need to use Dispatcher to invoke data change event handler.

tmp1C-159_thumb

7. Insert a function to handle the data change.

tmp1C-160_thumb

Fill this part with the logic for the application you’re developing. 8. Press F5 and press button, see what happens.

tmp1C-161

Accelerometer shows the values 0 0 -1, which means the emulator is in lying flat position. Since the emulator doesn’t have built in support for accelerometer, we cannot test this function using emulator.

There are several things to remember. Getting the exact value of 1.0 is however very unlikely to ever happen because gravity does not work that way. It may astound you how standing on top of a mountain or shaking your hand too much can add pressure on the device.

Accelerometer has its own fault tolerance, so maybe you would want to experiment on the total value of the fault.

Imagine that you will receive a lot of new data changes, precisely 50 times per second. This means that the amount of data we will receive is humongous. Retrieved data is very likely to be unstable due to the nature of accelerometer sensor itself. Even when the device is lying on a table, any variation may occur. This means that any application that uses accelerometer needs a reading mechanism, whether it’s calibration or smoothing for any data received from the component.

Further information regarding this topic can be obtained here.

Next post:

Previous post: