Table of Content
Inspecting the accelerometer sensor
We are going to build an inspector of the accelerometer sensor. We will display the values of the 3 axises – X, Y and Z.
Before getting into code, you may want to check the demo in the following link.
http://mztests.herokuapp.com/motion/
The PhoneGap approach
First, here we have the PhoneGap approach. It is like most PhoneGap API call with success callback, fail callback and options parameters.
1var watchAcceleration = function() { 2 this.watchID = navigator.accelerometer.watchAcceleration( 3 //success callback: 4 function(acceleration){ 5 this.acceleration = acceleration; 6 }, 7 // error callback: 8 function(){ 9 // error on getting acceleration. 10 }, 11 // options: 12 { frequency: 3000 }); 13};
http://docs.phonegap.com/en/1.2.0/phonegap_accelerometer_accelerometer.md.html
The web standard approach
The code example
We use a list to display the value history.
We use an array to store the history of the value. And set a limit count to the array.
Note that this code only handled the X value. There are Y and Z value in accelerometer sensor.
What’s next? We’re going to take a look at “Accelerometer value bar chart”.