Tutorial:Shake Detection
From DragonFireSDK Wiki
About
Here is a simple example of detecting when the user shakes the device. It only uses the X and Y axis which is sufficient for general purposes(like shake to generate x event). You can modified the sensitivity as well.
Code
#include "DragonFireSDK.h"
//Last Tiltx/y are used to determine distance of current vs last tilt.
int iLastTiltx =0;
int iLastTilty=0;
//iCurx/y is used to get the current tilt
int iCurx;
int iCury;
//iDistancex/y are used to hold the actual distance between cur and last. They get summed up to iTotal.
int iDistancex=0;
int iDistancey=0;
//iTotal used to keep a running record oftilt changes(thoughb it does get reset after a period of inactivity)
int iTotal=0;
//the thresh hold is the total running distance of tilts before an event happens. 5000 means they are shaking it on purpose
//change this number to decrease or increase shake time required
int iTotalThreshold = 5000;
//iDelay is used to prevent the successful shake event from happening many times in a row.
//For my purposes, this method was fine but you may want to come up with a different way if firing the event x2 would be deterimental
int iDelay = 30;
//the iDelayThreshold is used to prevent an event from happening mujltiple times. Set at 45, it means 1.5 seconds. Increase or decrease this
//delay between potential events firing as suits your purposes.
int iDelayThreshold = 45;
// iSamecounter keeps track of inactivity. If the tilt stays the same for a period of time, it will reset the iTotal counter.
int iSameCounter =0;
void AppMain()
{
}
void AppExit()
{
}
void OnTimer()
{
//Get current tilt x and y
iCurx = TiltGetx();
iCury = TiltGety();
//Check if the last tiltx/y = current. If so, we need to add a +1 to the same counter
if ((iCurx == iLastTiltx) && (iCury == iLastTilty))
{
iSameCounter++;
}
//If same counter > 25(almost a second), reset iTotal
if ((iSameCounter > 25))
{
iTotal = 0;
iSameCounter = 0;
}
//Some math to determine distance between points. pretty basic but if you are like me
//it took a while for high school geometry to come back...
iCurx = iCurx -iLastTiltx;
iCurx = iCurx * -1;
iCury = iCury -iLastTilty;
iCury = iCury *-1;
iDistancex = iCurx + iLastTiltx;
iDistancey = iCurx + iLastTilty;
iTotal = iDistancex + iDistancey;
if (iTotal > iTotalThreshold && iDelay > iDelayThreshold)
{
iDelay =0;
Vibrate();
iTotal = 0;
}
iLastTiltx = TiltGetx();
iLastTilty = TiltGety();
iDelay++;
}