0

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?

Sam Jarman
  • 7,277
  • 15
  • 55
  • 100
  • 1
    Clarification... you want the image to flash as the device's velocity gets to zero when it turns around, rather than when it starts decelerating? – Andrew McGregor Jan 17 '10 at 02:13
  • well, yes. On the turn around. So momentarily the device will be a v = 0. That could be an effective trigger, how do I use that? – Sam Jarman Jan 17 '10 at 04:40

1 Answers1

0

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
  • 31,730
  • 2
  • 29
  • 28
  • 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 accelerometer should the accleration go from a positive value to a negative? – Sam Jarman Jan 17 '10 at 06:04
  • 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 Jan 17 '10 at 06:09
  • 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 Jan 17 '10 at 06:29