Smoothing Accelerometer Data

Each time the accelerometer sends an update, the center of the view changes to reflect the orientation of the device. However, because the accelerometer’s updateInterval is constant and the device’s movement is variable, the center of the view jumps around instead of moving in calm, peaceful, smooth manner. To get smooth movement, you need smoothed data. To smooth accelerometer data, you apply a low-pass filter.

In HypnosisViewController.m, add the following lines of code to filter the accelerometer data:

-​ ​(​v​o​i​d​)​a​c​c​e​l​e​r​o​m​e​t​e​r​:​(​U​I​A​c​c​e​l​e​r​o​m​e​t​e​r​ ​*​)​m​e​t​e​r​
 ​ ​ ​ ​ ​ ​ ​ ​d​i​d​A​c​c​e​l​e​r​a​t​e​:​(​U​I​A​c​c​e​l​e​r​a​t​i​o​n​ ​*​)​a​c​c​e​l​
{​
 ​ ​ ​ ​H​y​p​n​o​s​i​s​V​i​e​w​ ​*​h​v​ ​=​ ​(​H​y​p​n​o​s​i​s​V​i​e​w​ ​*​)​[​s​e​l​f​ ​v​i​e​w​]​;​
 ​ ​ ​ ​f​l​o​a​t​ ​x​S​h​i​f​t​ ​=​ ​[​h​v​ ​x​S​h​i​f​t​]​ ​*​ ​0​.​8​ ​+​ ​[​a​c​c​e​l​ ​x​]​ ​*​ ​2​.​0​;​
 ​ ​ ​ ​f​l​o​a​t​ ​y​S​h​i​f​t​ ​=​ ​[​h​v​ ​y​S​h​i​f​t​]​ ​*​ ​0​.​8​ ​-​ ​[​a​c​c​e​l​ ​y​]​ ​*​ ​2​.​0​;​
 ​ ​ ​ ​[​h​v​ ​s​e​t​X​S​h​i​f​t​:​x​S​h​i​f​t​]​;​
 ​ ​ ​ ​[​h​v​ ​s​e​t​Y​S​h​i​f​t​:​y​S​h​i​f​t​]​;​

 ​ ​ ​ ​/​/​ ​R​e​d​r​a​w​ ​t​h​e​ ​v​i​e​w​
 ​ ​ ​ ​[​h​v​ ​s​e​t​N​e​e​d​s​D​i​s​p​l​a​y​]​;​
}​

Build and run your application. The application will have a smoother response and a nicer feel.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.221.133