13,899,467 members
Technical Blog
alternative version

#### Stats

41.3K views
16 bookmarked
Posted 2 Aug 2010
Licenced CPOL

# Detect Shaking Motion on Windows Phone 7

, 2 Aug 2010
A demonstration of one way to detect shaking motion on Windows Phone 7

The other day on the MSDN forums, someone 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. `<codeminimumaccelerationmagnitude>` 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 over which a shake sequence must occur to be considered acceptable. Once 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 raised.

I've reduced the direction in which the device is moving to one of 8 directions (`North`, `East`, `South`, `West`, and the directions in between 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 someone else to understand.

```void _accelerometer_ReadingChanged(object sender, AccelerometerReadingEventArgs e)
{
//Does the current acceleration vector meet the minimum magnitude that we
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 it
if ((direction & _shakeRecordList[_shakeRecordIndex].ShakeDirection)
!= Direction.None)
return;
//This is a shake we care about. save in our list
ShakeRecord 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();
}
}```

The example code can be found in my SkyDrive account here. If you want to see the program in action, there is a video on YouTube.

## Share

 Software Developer Razorfish United States
I attended Southern Polytechnic State University and earned a Bachelors of Science in Computer Science and later returned to earn a Masters of Science in Software Engineering. I've largely developed solutions that are based on a mix of Microsoft technologies with open source technologies mixed in. I've got an interest in astronomy and you'll see that interest overflow into some of my code project articles from time to time.

Instagram: j2inet

## You may also be interested in...

 First Prev Next
 How do I prevent it from firing multiple events in single shake guesture? PrasadGVL5-Apr-12 0:38 PrasadGVL 5-Apr-12 0:38
 Minor fix - there should be a call to Dispose() on accelerometer object after calling Stop() Vlad P.9-Oct-11 12:58 Vlad P. 9-Oct-11 12:58
 Missing reinitialization Niko Bertele19-Sep-11 6:54 Niko Bertele 19-Sep-11 6:54
 My vote of 5 Niko Bertele19-Sep-11 6:45 Niko Bertele 19-Sep-11 6:45
 Formatting issue with blog entry DaveAuld2-Aug-10 9:11 DaveAuld 2-Aug-10 9:11
 Re: Formatting issue with blog entry Joel Ivory Johnson2-Aug-10 9:27 Joel Ivory Johnson 2-Aug-10 9:27
 Last Visit: 22-Mar-19 8:28     Last Update: 22-Mar-19 8:28 Refresh 1