Look Here First

Curious why I created this blog? The following two posts explain it all. Click on the titles below to read more.

What this Blog is about.

Project Description.

Sunday, April 10, 2011

Accelerometer: Determining Direction

The accelerometer measures the amount of force by returning a voltage which is then converted to a digital value.  The digital value is a 10 bit number which means that the result can have 1024 different values ranging from.   2^10 = 1024.   1111111111b = 1023d. 

The accelerometer measures force in either a positive or negative direction with respect to an axis of direction.  Negative G's in a direction are will result in a value less than 511/512, positive G's will be greater than 511/512.  Tipping the device can result in positive or negative force with respect to a particular axis.   

The Pac-Man character can move in one of 4 directions.  To determine which of those directions to move the axis which is exhibiting the greatest displacement from center must first be found, then the direction of that displacement is then considered to come up with a direction that Pac-Man is to be directed to move to.

After the voltage is accurately measured in both directions the following code could be used to determine which direction the user wishes to move Pac-Man, or no direction at all if the device was held at a position close to rest.

//  enumerate directions
enum direction
    up = 0,
    right = 1, 
    down = 2,
    left = 3,
    none = 4

// The direction of force in relation to axis.
enum AxisDirection
    negative = 0,
    positive = 1

// A value which magnitude must be above to register movement.
const short kThreshHold = 5;

unsigned char AccelDirection( short xVal,  short yVal)
   short xMag = xVal  - 512;
   short yMag = yVal - 512; 
   unsigned char xDirection = positive;
   unsigned char yDirection = positive;
   if (xMag < 0)
      xMag = xMag * -1;
      xDirection = negative;
   if (yMag < 0)
      yMag = yMag * -1;
      yDirection = negative;
   if (xMag  - kThreshHold > 0 && xMag > yMag)
     if (xDirection == negative )
       return left;
       return right;
  else if (yMag -kThreshHold > 0 && yMag > xMag)
     if (yDirection == negative )
       return down;
       return up;
      return none;

No comments:

Post a Comment