How to read the accelerometer values

So now we can have some fun creating an app that will display accelerometer values as you turn your phone around πŸ™‚

Create a new project (if you forgot how to do that, click here).
Now first we want some controls on the scene that will display the values. You can add 4 labels and 3 text fields to make it look like this:

Hello Accelerator iPhone development tutorial

We want the text fields just to display the data, so to make it read-only uncheck the “User Interaction Enabled” in the properties of each field.
Create the outlets for the text fields and connect them to the controls in the storyboard (if you need a reminder check here). We also need to create a pointer to the UIAccelerometer object and make our ViewController class a delegate of UIAccelerometer (so we can pickup his messages to our app)

Finally the ViewController.h will look like this:

@interface ViewController : UIViewController <UIAccelerometerDelegate>
    IBOutlet UITextField *textX;
    IBOutlet UITextField *textY;
    IBOutlet UITextField *textZ;

    UIAccelerometer *accelerometer;

@property (nonatomic, retain) UITextField *textX;
@property (nonatomic, retain) UITextField *textY;
@property (nonatomic, retain) UITextField *textZ;

@property (nonatomic, retain) UIAccelerometer *accelerometer;


Now in the ViewController.m, in the viewDidLoad function, we will setup our accelerometer object.

- (void)viewDidLoad
    [super viewDidLoad];

   Β // Do any additional setup after loading the view, typically from a nib.
    self.accelerometer = [UIAccelerometer sharedAccelerometer];
    self.accelerometer.updateInterval = 0.1f;
    self.accelerometer.delegate = self;   

The first thing you should know here is that the instances of UIAccelerometer should never be created. They should only be acquired by getting the system’s shared accelerometer object, just like we did here.
The updateInterval property is used to instruct the UIAccelerometer object how often it should send updates to its delegate (we’ve set it to 0.1 seconds). And finally you need to set the delegate to the UIViewController.

Now we’ve come to the most interesting part πŸ™‚
We will now implement the delegate’s method to receive accelerometer updates (you can put it under the shouldAutorotateToInterfaceOrientation function):

- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration 
    textX.text = [NSString stringWithFormat:@"%f", acceleration.x];
    textY.text = [NSString stringWithFormat:@"%f", acceleration.y];
    textZ.text = [NSString stringWithFormat:@"%f", acceleration.z];

Here, we read the acceleration variable which the delegate function provides us with, and assign that variable to the text of our text fields (of course we have to format it using the stringWithFormat function, to convert the float to string).
Now if you run this app in the Simulator the text fields will remain empty because the Simulator has no accelerator.
Just plug your iPhone to the computer and run it on the actual device and you will see that your app is working! πŸ™‚



Leave a Reply