iPhone Accelerometer still not registering!

I have a game where you control the character with the accelerometer. The problem is the accelerometer works about 90% of the time, the other 10% it doesn't seem to be sending any data.(So the player is unable to move)

I've heard changing the color-format to kEAGLColorFormatRGBA8 often solves this problem but it didn't for my app. Here is the code
// Get the layer
CAEAGLLayer *eaglLayer = (CAEAGLLayer *)self.layer;

eaglLayer.opaque = YES;
eaglLayer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:NO], kEAGLDrawablePropertyRetainedBacking, kEAGLColorFormatRGBA8, kEAGLDrawablePropertyColorFormat, nil];


I've also heard you need to specific the delegate first when you start the accelerometer.
//Configure and start accelerometer
[[UIAccelerometer sharedAccelerometer] setDelegate:self];
[[UIAccelerometer sharedAccelerometer] setUpdateInterval:(1.0 / kAccelerometerFrequency)];


Finally, here is where I do my accelerometer calculations:
// Implement this method to get the lastest data from the accelerometer 
- (void)accelerometer:(UIAccelerometer*)accelerometer didAccelerate:(UIAcceleration*)acceleration
{
//Use a basic low-pass filter to only keep the gravity in the accelerometer values
_accelerometerX = acceleration.x * kFilteringFactor + _accelerometerX * (1.0 - kFilteringFactor);
}


Does anyone see where I'm making my mistake?

Mac Pro, Two 3GHz Dual-Core Intel Xeon, 410 GB HD, NVIDIA 7300 GT, Mac OS X (10.5.2), Macbook Air, 1.6GHz Core 2 Duo, 80GB HD, 2GB RAM

Posted on Sep 9, 2008 10:16 AM

Reply
23 replies

Sep 13, 2008 4:26 PM in response to Macmenace

You're not alone, I have the same problem despite basing my app off of Crash Landing. I assume Crash Landing has the same problem too, especially if you're saying that Bubble Level has the issue too.

I gave up trying to fix it as it seems to be an issue with everything that uses the accelerometer... sometimes, it just doesn't register and you have to restart the app.

Oct 1, 2008 8:21 AM in response to Macmenace

I think I have a solution for this problem; let me know whether it works for you.

After you use [[UIAccelerometer sharedAccelerometer] setDelegate:self] you should begin receiving notifications through the accelerometer function.

If you don't get any notifications within a short amount of time then you know that the problem has occurred.
I check this by counting down a value every game frame and resetting the value when I get a notification. Non-game applications could use a timer instead.

Reading through the docs, if you switch off your UIAcceleromter notification -and- disable orientation notifications then the accelerometer will be switched off.
My idea was to do this when you detect the problem, then switch back on. Note that you -do- need to disable orientation notifications even if you never enabled them in the first place.
The code I ended up with (perhaps not all necessary) was:


// reset accelerometer
// first switch it off (switch it on first to make sure the notification off works - might not be necessary)
[[UIAccelerometer sharedAccelerometer] setDelegate:NULL];
[[UIDevice currentDevice] beginGeneratingOrientationNotifications];
[[UIDevice currentDevice] endGeneratingOrientationNotifications];
[[UIDevice currentDevice] beginGeneratingOrientationNotifications];
[[UIDevice currentDevice] endGeneratingOrientationNotifications];
// then switch it back on
[[UIAccelerometer sharedAccelerometer] setDelegate:this];

Oct 1, 2008 11:29 AM in response to GaryBlue

That sounds like a great idea. So tell me if I'm correct in understanding.

I should increment a counter every game loop and if it gets to "x" amount I should reset the accelerometer, turn it off and then turn it back on with this code:
// reset accelerometer
// first switch it off (switch it on first to make sure the notification off works - might not be necessary)
[UIAccelerometer sharedAccelerometer] setDelegate:NULL;
[UIDevice currentDevice] beginGeneratingOrientationNotifications;
[UIDevice currentDevice] endGeneratingOrientationNotifications;
[UIDevice currentDevice] beginGeneratingOrientationNotifications;
[UIDevice currentDevice] endGeneratingOrientationNotifications;
// then switch it back on
[UIAccelerometer sharedAccelerometer] setDelegate:this;


Should I do this just once if need be or could this possibly need to be done whenever I'm not receiving accelerometer readings during the game loop?

Oct 1, 2008 12:14 PM in response to Macmenace

Another thing I've noticed, is that you should set up the sharedAccelerometer delegate as soon as possible. Do it in the appDelegate, before anything else major is instantiated.

Also, don't put it in a subview. I had my accelerometer delegate in a UIView for gameplay, which was 3 subviews deep. The accelerometer would break about 10% of the time. When I moved it to the appDelegate, it always works.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

iPhone Accelerometer still not registering!

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.