views:

203

answers:

1

Id have an app that does one thing if you shake it one way, and another if you shake it the other.

atm my code is.

if (acceleration.x > 1.5) {

//arm to the right when facing you

    float duration = ([imageArray count]*0.04);

    //HUMAN EYE KEEPS IMAGES IN EYE For 40 MILLISECONDS//
    //NSLog(@"duration:%f", duration);
    [theFlash setAnimationImages:imageArray];
    [theFlash setAnimationRepeatCount:1];
    theFlash.animationDuration = duration;
    [theFlash startAnimating];
    NSLog(@"images flashed forward");   

}

if (acceleration.x < -1)
{
    //arm to the left when facing you

    float duration = ([imageArrayReversed count]*0.04);

    //HUMAN EYE KEEPS IMAGES IN EYE For 40 MILLISECONDS//
    NSLog(@"duration:%f", duration);
    [theFlash setAnimationImages:imageArrayReversed];
    [theFlash setAnimationRepeatCount:1];
    theFlash.animationDuration = duration;
    [theFlash startAnimating];
    NSLog(@"images flashed backward");  

}

the 1 and -1 values are working on it not being too sensitive.

however, this code is not giving me the results desired. Id like the images to flash (see the code) as soon as the person starts moving the device the other way.

Any way to do this?

A: 

Well, you're going to have to integrate the acceleration to get velocity. Unfortunately, the accelerometer is a bit noisy and will inevitably drift a bit, so you're going to have to filter that integral. I think the filter described at http://www.musicdsp.org/showone.php?id=92 will do what you need, with the cutoff frequency set to around 1/4 the sampling frequency. You need the band output, and you'll have to tune the resonance parameter.

Andrew McGregor
this is proving to be much trickier than I thought. Are you sure there is no way you can tell the phone changing direction using the accelerometershould the accleration go from a positive value to a negative?
Sam Jarman
what if i wrote an if statements like if (the acceleration is positive and has been 0 before){//action}and if (the acceleration is negative and has been 0 before){//action for reverse}and I know that when your accel is zero, it means you can still be moving, but in this case, it should not matter, what soever, since moving your device at a constant speed would be impossible ( or pretty much so)
Sam Jarman
Quite sure, but a simple filter (that one's only going to be 8 to 10 lines of code) should sort it right out. Thing is, if you want to time your action to the end of the motion, that's not going to be at a zero point of the acceleration.
Andrew McGregor