Read this guide to learn everything you need to know about RPA, and how it can help you manage and automate your processes.

This article is Day #25 in a series called 31 Days of Windows 8. Each of the articles in this series will be published for both HTML5/JS and XAML/C#. You can find additional resources, downloads, and source code on our website.

Today, we are taking a look at another sensor that we might find in a Windows 8 device: the accelerometer. With the accelerometer, we can measure the movement of the user’s machine. If you’re looking for more information about what exactly an accelerometer is check out this. With an accelerometer we can do things like detect the orientation of the device and even sense if a device is falling.

The X axis runs horizontally across the device. The Y axis runs vertically across the device. The Z axis runs immediately through the device, from front to back. Here’s an image to help illustrate, using a Windows Phone as the example (courtesy of http://www.andybeaulieu.com/):

In simpler terms, we’re measuring the g-forces applied to those three axes. Because of this, when we lay a Windows 8 tablet flat on a table, we get a Z axis value of –1, because there is one “g” (one unit of gravity) exerting itself on the negative size of the Z axis. In the same way, if we propped our device up (similar to the phone in the photo) so that the bottom of the device is sitting on the table, we would get a Y axis value of –1.

Working with the accelerometer is very similar to working with the other sensors. Just a few simple steps and we’re getting data.

Initialize the Sensor.

If it’s available,

create a readingchanged event handler.

create a shaken event handler

In the event handlers, grab the data from the sensor and write it to the screen.

Here’s my the entirety of my code and it looks scary similar to that of the other sensors.

Again, no real surprises here. We get to our data in the same basic was as we did with the other sensors. Now the accelerometer sensor did introduce a new event called shaken. This event fires when a user shakes their machine In writing this article I was using the original build tablet for testing. I shook the machine but sadly couldn’t seem to get the event to fire. Just when I started to think I didn’t have a sensor which supported that event, I’d tossed down the machine down onto the couch and all of the sudden it fired. Turns out, I just wasn’t holding it right.

Summary

Today we briefly looked at getting data from our accelerometer. Working with each of the different sensors is all very similar. Ultimately, how you use this data is where your real creativity comes in and I’m looking forward to hearing how you’ll use it in your app.

If you’d like to download my working sample that uses the code from this article, click the icon below: