How to Detect Shaking Motion on Windows Phone 7

No.of Views1924
Bookmarked1 times
Downloads 
Votes0
By  j2inet   On  10 Nov 2010 02:11:25
Tag : Windows Phone , Miscellaneous
In this codesnippet, i will show you all, How to Detect Shaking Motion on Windows Phone 7
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction

The other day on the MSDN forums some one asked about how to detect a shaking motion on Windows Phone 7. I've been playing with the accelerometer lately so I took great joy in answering this along with providing a working implementation. The question was asking about shaking motion in a left-right direction. I made a class that detects left-right and up-down motion (totally ignoring the Z-axis all together for now). Though extending it to consider the Z-axis wouldn't be hard.

The code for detecting the motion has been abstracted in a class called ShakeDetector. The algorithm used has a few variables/constants defined that can be modified to tune the behaviour of the class. The classes constructor accepts an [optional] parameter of how many times the phone should be shaken before the motion is considered acceptable. can be raised or lowered to control how hard the device needs to be shaken to be considered acceptable. And MinimumShakeTime takes a time span that defines the maximum length of time overwhich a shake sequence must occur to be considered acceptable. One the user moves the phone in a way that meets the requirements for the type of shake we wanted to detect a ShakeDetected event is raise

I've reduced the direction in which the device is moving to one of 8 directions (North, East, South, West, and the directions inbetween those). I could have kept the direction as an angle and just ensured that there was atleast a minimum difference between the angles but I thought using the directions on a map would make it easier for some one else to understand.

Code

void _accelerometer_ReadingChanged(object sender, AccelerometerReadingEventArgs e){//Does the currenet acceleration vector meet the minimum magnitude that we//care about?if ((e.X*e.X + e.Y*e.Y) > MinimumAccelerationMagnitudeSquared){//I prefer to work in radians. For the sake of those reading this code//I will work in degrees. In the following direction will contain the direction// in which the device was accelerating in degrees. double degrees = 180.0*Math.Atan2(e.Y, e.X)/Math.PI;Direction direction = DegreesToDirection(degrees);//If the shake detected is in the same direction as the last one then ignore itif ((direction & _shakeRecordList[_shakeRecordIndex].ShakeDirection) != Direction.None)return;//This is a shake we care about. save in in our listShakeRecord record = new ShakeRecord();record.EventTime = DateTime.Now;record.ShakeDirection = direction;_shakeRecordIndex = (_shakeRecordIndex + 1)%_minimumShakes;_shakeRecordList[_shakeRecordIndex] = record;CheckForShakes();}}void CheckForShakes(){int startIndex = (_shakeRecordIndex - 1);if (startIndex < 0) startIndex = _minimumShakes - 1;int endIndex = _shakeRecordIndex;if ((_shakeRecordList[endIndex].EventTime.Subtract(_shakeRecordList[startIndex].EventTime)) <= MinimumShakeTime){OnShakeEvent();}}

 If you want to see the program in action there is a video on YouTube.

Download Sample Project

Download source files -88 kb

Hopes help.

 
Sign Up to vote for this article
 
About Author
 
j2inet
Occupation-Not Provided
Company-Not Provided
Member Type-Senior
Location-Not Provided
Joined date-12 Jun 2009
Home Page-Not Provided
Blog Page-Not Provided
 
 
Other popularSectionarticles
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top