views:

19

answers:

1

Hello All,

Actually i want to apply a loop on accelerometer, means i want to start a accelerometer on behalf of loop. if i want to start loop one then want to perform accelerometer reading one time. if loop will run twice then want to run accelerometer twice. But it not happens. What should i do to control accelerometer.

Have a quick look on code

in viewdidload

for (int i=0; i<3; i++) 

    {
        NSLog(@"Hellooooooooo",i);
        [[UIAccelerometer sharedAccelerometer] setDelegate:self];
    }

and in accelerometer didAccelerate

{
        float xx = -[acceleration x]; 
    float yy = [acceleration y]; 

} 

I don't know what is going wrong ?

help me if u have some idea about it.

thanks in advance for any help.

A: 

set the update interval to something not 0.
And if you don't need the accelerometer anymore set it back to 0 and remove your delegate. If you want 3 measurements you have to change your code, setting the delegate 3 times has no effect.

You could use something like this:

- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {
    static int count = 0;
    count++;
    float xx = -[acceleration x]; 
    float yy = [acceleration y]; 
    if (count >= 3) {
        [accelerometer setUpdateInterval:0];
        [accelerometer setDelegate:nil];
        count = 0;
    }
    NSLog(@"%f %f", xx, yy);
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [[UIAccelerometer sharedAccelerometer] setDelegate:self];
    [[UIAccelerometer sharedAccelerometer] setUpdateInterval:1.0/measurementsPerSecond];
}
fluchtpunkt