You don't need to use a lot of fancy routines to subscribe to data generated by the iPhone's built in accelerometer. A single UIApplication delegate lets you know whenever the iPhone has updated its tilt data.
When you add a method for (void)acceleratedInX:(float)xx Y:(float)yy Z:(float)zz to any UIApplication, the iPhone sends regular messages to your application giving you values for X, Y, and Z. These values are not exactly what I first expected:
X = Roll X corresponds to roll, or rotation around the axis that runs from your home button to your earpiece. Values vary from 0.5 (rolled all the way to the left) to -0.5 (rolled all the way to the right).
Y = Pitch. Place your iPhone on the table and mentally draw a horizontal line about half-way down the screen. That's the axis around which the Y value rotates. Values go from 0.5 (the headphone jack straight down) to -0.5 (the headphone jack straight up).
Z = Face up/face down. I expected the Z value to correspond to yaw. And it does not. It refers to whether your iPhone is face up (-0.5) or face down (0.5). When placed on it side, either the side with the volume controls and ringer switch, or the side directly opposite, the Z value equates to 0.0.
I went to expert Nate True for some advice about these values. He explained the reason that Z is not yaw. It's because yaw is a compass value, something the iPhone can't measure. X, Y, and Z are linear acceleration values. They point to whichever way gravity is operating. Together, X, Y and Z form a 3-D acceleration vector that indicates the direction of gravity. "Say you're tossing the phone up into the air, then all the values will fall to 0 you could make an app that makes the phone scream if you drop it."
You can use the X and Y values to determine the highest point of the iPhone by using an arctan function, i.e. float angle = atan2(yy, xx);. I've put together a sample application that demonstrates this. As the screen shot here shows, it provides the raw X, Y and Z values and keeps the arrow pointing to whatever point is "up" for your iPhone.
If you'd like to play some more with the accelerometer, I've also thrown together a simple game called Butterfly that allows you to slide a net around your screen to catch a virtual monarch butterfly.













Reader Comments (Page 1 of 1)
9-10-2007 @ 5:37PM
neodymium said...
too bad there's no compass; if there were you could make a digital brunton for calculating strike/dip of sedimentary strata. that would be cool.
Reply
9-10-2007 @ 5:40PM
neodymium said...
... although you could still make a cool program to calculate the height of tress and buildings if you type in how far you are from the base and then sight along the phone to the top of the object...
Reply
9-10-2007 @ 6:27PM
Graham said...
Did I fall asleep when this became The Unofficial Objective C Weblog? I'm getting fed up with these "programming For iPhone" posts that make sense to very few people. Aren't there plenty of other objective c sites that this would be more appropriate for?
Reply
9-10-2007 @ 6:41PM
Donald Burr said...
#3,
Bugger off.
Reply
9-10-2007 @ 6:41PM
Donald Burr said...
BTW I would definitely like a "getting up and running with the toolchain" post. My feeble attempt at getting it going ended in pain and misery.
Reply
9-10-2007 @ 7:12PM
Sahil said...
I'm with Donald on this, I also tried numerous guides all over the internet to get Toolchain going, and although it does compile things, I run into all sorts of errors regarding missing files with compiling various of the open source apps (eg. apollo, mobilechat etc).
Please I will write an awesome app if I can just get off my ass (the proverbial "onto my feet")
Reply
9-10-2007 @ 7:22PM
Alex Polson said...
I definitely agree with the need for a "How to setup the tool chain" post. That would help out a lot.
Reply
9-10-2007 @ 9:32PM
Ross Byers said...
Warning: Science content
There is a reason that you cannot get yaw from the acceleromter: It's an acceleromter, not a gyro. Assuming the iPhone is at rest, the only thing the accelerometer can measure is gravity. So imagine a line with a weight on it (a plumb-bob) tied to your iPhone. If you pitch or roll it, the angle of the string changes. This is measurable. Now rotate it in place, yawing it. The angle of the string does not change. Thus, the accelerometer cannot measure it.
This is why the Wiimote, which uses the same technology, requires the 'sensor bar' in order to get correct yaw.
PS: The numbers being reported at not angles in an arc, they are forces on an axis, where X=the narrow axis(left-right), Y=the long axis(forward-back), and Z=the vertical axis(up, down).
Reply
9-10-2007 @ 9:44PM
Rich said...
Agreed.... would love to see a "How to setup the tool chain" article!!!
Reply
9-11-2007 @ 12:28AM
Dmitry Kirillov said...
Hey, isn't it a right time to write "iPhone Saber" application, similar to MacSaber and WiiSaber? :)
Reply
9-11-2007 @ 4:32AM
Tom said...
I've been working on a few neat things with the iPhone's accelerometer, but it's incredibly frustrating having an accelerometer but not a gyro (or Wiimote's "sensor bar").
Reply
9-11-2007 @ 6:17AM
jgjay said...
+1 for a setting up the toolchain and compiling a simple Hello World app for iPhone!
Erica... make it so. :)
Reply
9-11-2007 @ 9:33AM
Rob Terrell said...
#11: I took Erica's code and did that: http://robterrell.com/iSaber.app.zip
Reply
9-11-2007 @ 4:08PM
Phil Harvey said...
I think these posts are great. One thing I think would help is a post on how to set up the iPhone toolchain and how to get a simple hello world up and running.
Thanks
Reply
9-16-2007 @ 5:42PM
C S said...
Theramin: Can someone pleeeeaaaaase make an app for the iphone to make Theramin sounds!?!
Reply
9-16-2007 @ 6:02PM
dalasv said...
Can someone make an app that lets me hit my iPhone to snooze the alarm?
Reply
9-17-2007 @ 10:02AM
Alex said...
so then I was wrong it seems :(
Once I figured out how to code for the iPhone I was gonna make a version of Nate True's thing that was a 3D arrow instead of 2D, but I guess without a Z it's not possible :(
or is it just me? (it is pretty early in the morning and I am not thinking this through on purpose)
Reply
9-17-2007 @ 10:02AM
Alex said...
ross byers, umm.. The Wii remote has yaw, and it doesn't have a gyro in it.
Reply
9-19-2007 @ 7:13AM
Charlie said...
I'm writing an iPhone compass.
Although it is of much more limited use. :)
http://mockdock.com/compass/
Reply