Medallia Blog

Fun with the iPhone accelerometer

By Erling Ellingsen on August 27, 2007
Note (9/12): there is an application in the iBrickr PXL repository called 'Balls' which links to this page. I have nothing to do with that app; it was created by Grudgnor over at the MacRumors forum.

Those who have followed this blog will know that I like to like to play with unusual input methods (see my earlier posts on , ambient light sensors, and the SmackBook).

As it turns out, the iPhone has a built-in LIS302DL, a tiny 3-axis accelerometer. While some have attempted to use it from within the Safari browser (the Tilt game detects changes to the width of the browser page; it is basically used as a 1-bit input device), its potential is still somewhat untapped.

After a rather lengthy bout of reverse-engineering (I had barely touched ARM assembly before this), I finally figured out how to access the raw data from the accelerometer itself, as can be seen in the video above. Source code will be posted as soon as possible is posted here. (update: yes, it is possible to access the accelerometer directly through UIKit without this hack -- however, you'll be locked to the default sample rate, which is too slow for some of the fun stuff)

Straw poll: What would you like to see on the iPhone?

TrackBack

TrackBack URL for this entry:
http://blog.medallia.com/cgi-bin/mt/mt-tb.cgi/28

Show Trackbacks

Comments

2611

What did you have to reverse engineer? Is it that hard to look in UIApplication and find acceleratedInX:Y:Z: ? And what does this have to do with ARM assembly?

2612

Care to elaborate? I could never get the acceleratedInX:Y:Z: approach to work. What's the secret sauce?

It is, of course, very possible that I missed something trivial and could have saved lots of time :)

However, right now, I can only find two Google hits for 'acceleratedInX':

- a frustrated user over at http://code.google.com/p/iphonedoom/wiki/ControllerIdeas complaining about not getting it to work.
- your post (as grudgnor) over at the macrumors.com thread about this video, claiming the API is well-known and trivial to use.

2613

Erling, I'm looking forward to your applications!!

I have a (very ugly) working app that uses acceleratedInX.

http://www.tokash.org/iphone/AccTest.zip

I just compiled my code, renamed AccTest to Hello and stuck it in the Hello.app folder. In fact, it's really just a stripped down version of the Hello World app.

Note: It did not work until Kroo on IRC (THANKS KROO!) told me that you have to launch from Launcher or SpringBoard. Apps launched from ssh cannot use acceleratedInX, for some reason, even if they subclass UIApplication.

2614

Hi Erling,

In your main application code files (myApp.h and myApp.m), just override the acceleratedInX method, like this:

myApp.h:
@interface myApp : UIApplication {
}

- (void)acceleratedInX:(float)xTilt Y:(float)yTilt Z:(float)zTilt;


myApp.m:
#import "myApp.h"

@implementation myApp

- (void)acceleratedInX:(float)xTilt Y:(float)yTilt Z:(float)zTilt
{
// do something with the tilt values, like write them to a textView
//[_textView setText:[NSString stringWithFormat:@"%f,%f,%f",xTilt,yTilt,zTilt]];
}

@end

Obviously I cut out all of the code to setup the textview and window and stuff. I'm going to post my code for the simple moving ball app over at macrumors. This was my first ever Cocoa application (I'm a C# developer) so I was kind of weary about releasing it, especially because the collision detection never worked right.

2615

John Tokash nailed it --- I tested my stuff from the command line, which is why I never got the simple acceleratedX thing to work.

Jeremy -- you don't need to re-declare the method in @interface -- your superclass did that.

What sample rate are you seeing with the UIKIt method? Some comments on YouTube claim that the sample rate for the accelerometer is too slow to do the bouncing-ball demo -- my method gives you the full 100hz if you want.

2627

Nifty! It would be awesome if you could post some of the compiled apps you show in your video. I'm not too lazy to jailbreak my phone, but I'm definitely too lazy to write my own bobble head Steve Jobs.

2631

I'd vote for the tilty maze - very cool. Slick piece of work.

2634

Bored of iphone stories, move on now pleeeease

2635

I wish someone would develop a UI where moving the Palm would navigate the menus (think : the ipod menus, instead of the click-wheel use the motion sensor, move the iPhone toward you to scroll down etc...). We could end-up with a 1 button navigation system! Unfortunately my programming skillz suck, even though I had the idea the first time I saw the motion sensor on a Mac...

2636

Have you been able to access the Z (depth access) in any demo. For example to zoom in and out?

2672

There's only one practical use for this technology: A lightsaber simulator!

Wuzzz, Fzzzz, Wuuummmm, Wum, Fzzz!

2675

I can imagine using the quick jerk left/right to move between records in, say, the AddressBook, or back and forth through Safari's history. Up and down jerks could be a click on the scroll bar.

2677

I think a version of "macsaber" for iPhone would be totally awesome. I love that app on my MacBook, but the computer is too hard to swing around.

2688

here's Steve Strachan's light sabre (on a Nokia 5500) http://www.youtube.com/watch?v=qBAE1n9utSI

2724

How about making a pedometer?

2729

Would it at all be possible if you released the Bobble head Steve Jobs app along with the marbles bouncing and 3d maze? Those look like a lot of fun already!

2857

Where and when i can download the Bobble head Steve Jobs app with the marbles bouncing and 3d maze???

2858

Comment #2672.. jesus christ hilarious

good idea though :)

3113

When is the release of the marbles bouncing and 3d maze??? 1 month and nothing released in pxl repository!??

3187

hi, i'd installed the game "balls" from the iPhone installer but then i start get weired reaction when i try to scroll in the main page (the page we get when we press the home button) and just in the main page.
would you please tell me what the reason may be? And how can i solve it?
thanks

[Erling says: Read the note at the top of the page. I have nothing to do with the "balls" application.]

Post a comment

(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)