Accelerometer detecting side to side movements - instant detection of change in direction
Id have an app that does one thing if you shake it one way, and another if you shake it the oth开发者_StackOverflow中文版er.
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?
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.
精彩评论