How can I find out the direction of the accelerometer?

I am making a sound application on an accelerometer. It plays a different sound for movement by calculating the accelerometer value. But how can I find the direction of the accelerometer that the user moves along the x-axis plus or minus and along the Y-axis plus or minus. How can I find this value on the accelerometer.

Please give some instruction or help a code or project.

+1


a source to share


3 answers


You need to perform vector addition and compute the sum of the two vectors to get the resulting vector. The article above explains all the common methods for calculating it. But, doing it programmatically, you just need to apply the Pythagorean theorem and Tan theta = b / a



+1


a source


I think you will need the direction of the magnetometer (at least give you a bearing that you can always compare) and also using the vector math mentioned above. This article explains better how to add vectors (the first ignores the most likely case, just saying "difficult") ...



http://blog.dotphys.net/2008/09/basics-vectors-and-vector-addition/

+1


a source


You have to represent it using vectors, there is a delegate method below which describes in detail what you need to do.

Now I haven't covered the API too much yet, but I believe your direction vector is being returned to you from the accelerometer.

There is a delegate method that returns the values ​​you need. The following code might help from the tutorial you should look at here :

- (void)acceleratedInX:(float)xx Y:(float)yy Z:(float)zz
{
    // Create Status feedback string
    NSString *xstring = [NSString stringWithFormat:
    @"X (roll, %4.1f%%): %f\nY (pitch %4.1f%%): %f\nZ (%4.1f%%) : %f",
    100.0 - (xx + 1.0) * 100.0, xx,
    100.0 - (yy + 1.0) * 100.0, yy,
    100.0 - (zz + 1.0) * 100.0, zz
    ];
    self.textView.text = xstring;

    // Revert Arrow and then rotate to new coords
    float angle = atan2(xx, yy);
    angle += M_PI / 2.0;

    CGAffineTransform affineTransform = CGAffineTransformIdentity;
    affineTransform = CGAffineTransformConcat( affineTransform,       CGAffineTransformMakeRotation(angle));
    self.xarrow.transform = affineTransform;
}

- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {
    [self acceleratedInX:acceleration.x Y:acceleration.y Z:acceleration.z];
}

      

There is also an easy-to-read article that explains this explicitly here along with some sample code .

+1


a source







All Articles