3

Hi I'm new to stack overflow, and to iOS applicationprogramming. I've learnt basic-intermediate Objective C coding and have done bits and pieces to do with actual iOS apps, but have not actually published one to the App Store yet.

The first one I'm interested in doing I just a basic app using the accelerometer. I want to make an app where there is a ruler on the iPhone's screen and you can move it along a flat object and in the end it will give you the length of it. I have read through the iOS documentation regarding the accerometer, but I don't know how to apply it to give me the distance of something...

Any help like links to read through, or code excerpts, or an open source app like it would be much appreciated!

falky
  • 589
  • 2
  • 11
  • 27
  • Hi, welcome to StackOverflow. Your message is not really a technical programming question StackOverflow is for. For example, you have not said what you have already tried, or exactly what problem you have. Typical questions on StackOverflow contain some code and are about specific problems. Please try to read out the accelerometer first by yourself. If you have any problems doing that, you can come back with a question. – Sjoerd Mar 28 '13 at 07:50
  • possible duplicate of [Need to find Distance using Gyro+Accelerometer](http://stackoverflow.com/questions/6647314/need-to-find-distance-using-gyroaccelerometer) – Ali Mar 28 '13 at 09:15
  • possible duplicate of [Android accelerometer accuracy (Inertial navigation)](http://stackoverflow.com/q/7829097/341970) and probably many others – Ali Mar 28 '13 at 09:15
  • Thanks for posting that. I had already seen that post. I was looking for something with more detail regarding the accelerometer and also how to get something like a scrollview moving. I specifically didn't understand the statement: "So, once you have your accelerometer signals passed through a low-pass filter and binned in time with sampling interval dt, you can find the displacement in x" – falky Mar 28 '13 at 10:48
  • HI @falky can you please post the code to get the distance . I am implementing it in one of my application . Thanks in advance. – Tendulkar Aug 14 '13 at 05:38

1 Answers1

13

Do you remember physics from school? Newton's laws of motion?

velocity = distance/time

so:

distance = velocity x time.

Acceleration is the change in velocity over time right? You do the rest...

Anyway, less rubbish, here's the Objective-C:

You'll need to conform to <UIAccelerometerDelegate> then you can:

    -(void)startMeasuringAcceleration{


        [[UIAccelerometer sharedAccelerometer] setUpdateInterval:0.125];
        [[UIAccelerometer sharedAccelerometer] setDelegate:self];

    }

    -(void)stopMeasuringAcceleration{

        [[UIAccelerometer sharedAccelerometer] setDelegate:nil];

    }

    //this is a delegate method
    - (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration{

        NSLog(@"%f, %f, %f, %f", acceleration.x, acceleration.y, acceleration.z, acceleration.timestamp);
}        
Adam Waite
  • 19,175
  • 22
  • 126
  • 148
  • Awesome man! your a legend! Cheers! And yes we are literally doing that in physics at school right now! Thanks! – falky Mar 28 '13 at 22:49
  • 2
    @falky: Note that even if this question has been closed, you can (should) "accept" the answer by clicking on the check mark if it helped. That gives some reputation points to you and to the author of the answer. – Martin R Apr 07 '13 at 06:50