×
Namespaces

Variants
Actions

Archived:Retrieving accelerometer values in m

From Nokia Developer Wiki
Jump to: navigation, search

Archived.pngArchived: This article is archived because it is not considered relevant for third-party developers creating commercial solutions today. If you think this article is still relevant, let us know by adding the template {{ReviewForRemovalFromArchive|user=~~~~|write your reason here}}.

M on Symbian is not maintained and may not run on current Symbian versions. All M articles have been archived.

Article Metadata
Tested with
Devices(s): Nokia N95, Nokia 5800 XpressMusic
Compatibility
Platform(s): S60 3rd Edition, S60 5th Edition
Symbian
Platform Security
Capabilities: )
Article
Created: bogdan.galiceanu (24 May 2009)
Last edited: hamishwillee (09 May 2013)

Contents

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
This page was last modified on 9 May 2013, at 05:52.
47 page views in the last 30 days.