1

How can i use the gravity or motion sensors inside iPhone to calculate how many times the device moved up and down. e.g. as if it were lifted like a dumbbell for a couple of times, i wanted to count it.

Forgive me if this is something very simply achievable but I'm pretty new to iOS development and hence the question.

Viral Patel
  • 32,418
  • 18
  • 82
  • 110
  • You'll probably want to use Core Motion for that. Here's a tutorial http://nshipster.com/cmdevicemotion/ and here's Apple's Core Motion framework reference https://developer.apple.com/library/ios/documentation/CoreMotion/Reference/CoreMotion_Reference/. You'd probably want to set up a variable that tracks a motion count and then set up a method to perform if the count hits a number. I haven't worked with it much, but that's where I'd start. – Adrian Jul 15 '15 at 12:57

2 Answers2

0

You need to use the Core Motion framework to access the gyroscope and accelerometer data.

let manager = CMMotionManager()
if manager.gyroAvailable {
  // CMMotionManager is available.
  manager.gyroUpdateInterval = 0.1
  manager.startGyroUpdates()

  // get gyro data...
  let queue = NSOperationQueue.mainQueue
  manager.startGyroUpdatesToQueue(queue) {
     (data, error) in
     // ... get data here
  }
}

// accelerometer data
if manager.accelerometerAvailable {
   manager.accelerometerUpdateInterval = 0.01
   manager.startAccelerometerUpdatesToQueue(NSOperationQueue.mainQueue()) {
      [weak self] (data: CMAccelerometerData!, error: NSError!) in
      // get data here ...
   }
}

Combining those 2 you can get the detection going. Experiment with the results until you get the right motion you want.

0

I experimented a bit with the HTML5 methods on the iPhone. I wanted to build something similar to you, an app that would count the number of chin-ups or sit-ups automatically. I tried a combination of these:

  • navigator.geolocation
  • window.ondevicemotion
  • window.ondeviceorientation

The app would count up when moving in one direction, then wait after movement in the opposite direction ends, then start counting again. I get a count, but it lacks precision. Calibration is difficult... Code available if needed.

Webomatik
  • 844
  • 7
  • 7