You Are Here:

Community: Wiki

This page was last modified on 25 May 2009, at 04:13.

Retrieving accelerometer values in m

From Forum Nokia Wiki


ID Creation date May 24, 2009
Platform S60 3rd Edition, S60 5th Edition Tested on devices Nokia N95, Nokia 5800 XpressMusic
Category M Subcategory Sensor


Keywords (APIs, classes, methods, functions): accel

Overview

This article shows how to retrieve accelerometer data in m.

Preconditions

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"]

Postconditions

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
accel.new(140);
//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
end;
//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"]
end;
try
if math.abs(delta) <= 50 then print "Shake detected!" end;
catch exc by
end;
end

Related Wiki Articles

No related wiki articles found

Rate This

 
Bookmark this page: DeliciousDiggFacebookGoogleYahooStumbleUponRedditDiigoTechnocratiTwitter  Share this page Share this page Print this Page Print this page Invite a friend Invite a friend
京ICP备05048969号    Email Newsletters Press Terms & Conditions Privacy Policy Sitemap Contact Us © 2009 Nokia