Revision as of 12:03, 24 June 2011 by hamishwillee (Talk | contribs)

Archived:Retrieving accelerometer values in m

From Nokia Developer Wiki
Jump to: navigation, search
Article Metadata
Tested with
Devices(s): Nokia N95, Nokia 5800 XpressMusic
Platform(s): S60 3rd Edition, S60 5th Edition
Platform Security
Capabilities: )
Keywords: accel
Created: bogdan.galiceanu (24 May 2009)
Last edited: hamishwillee (24 Jun 2011)



This article shows how to retrieve accelerometer data in m.


Note: The accel module is only available in S60 3rd Edition and later.

Source code

use accel
//Get the data for the 3 axes
values = accel.get()
//Display it
print "X: " + values["x"]
print "Y: " + values["y"]
print "Z: " + values["z"]


The values from the accelerometer are displayed.

Practical use: detecting shakes

As more and more devices are equipped with an accelerometer sensor it is becoming a frequent practice to make applications that respond to the user shaking the phone. The following code examplifies how to implement such a feature.

It should be noted that, according to the m documentation, values returned by the get function vary from one device to another and are rather unstable. A great deal of tweaking should be done in order to ensure decent functionality.

use accel, math
while true do
x = false;
y = false;
z = false;
//Read the initial values
values = accel.get();
//Wait until one of the values changes by at least 140 points
//Acquire new values
new_values = accel.get();
//See which one changed
if math.abs(new_values["x"] - values["x"]) >= 140 then x = true
elsif math.abs(new_values["y"] - values["y"]) >= 140 then y = true
elsif math.abs(new_values["z"] - values["z"]) >= 140 then z = true
//Wait for the phone to return to the initial position (meaning approximately the same accelerometer values)
//with a margin of error and within a reasonable time interval (2000 milliseconds, for example)
accel.new(120, 2000);
//Acquire new values
new_values = accel.get();
//Check if the necessary one changed and if so, print the message
if x = true then delta = new_values["x"] - values["x"]
elsif y = true then delta = new_values["y"] - values["y"]
elsif z = true then delta = new_values["z"] - values["z"]
if math.abs(delta) <= 50 then print "Shake detected!" end;
catch exc by
47 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.


Thank you!

We appreciate your feedback.