Using the accelerometer

What is this document about?

This document will show you how to use the accelerometer’s API. In the end of this document we will show how to print the accelerometers values in the log of your application.

The JCPN file

Here the JCPN file is very simple. You only need to use two interfaces:
- the Debug, to print information in the log, and
- the accelerometers to retrieve the data send by the hardware.

The initial file

This is the only file of the project all you need is here.

The variables

First you need to create a set of variables

private final int 		ACC_SAMPLE_RATE = 20;

private BuffRef 		tmpAccX = new BuffRef(ACC_SAMPLE_RATE * 4);
private BuffRef 		tmpAccY = new BuffRef(ACC_SAMPLE_RATE * 4);
private BuffRef 		tmpAccZ = new BuffRef(ACC_SAMPLE_RATE * 4);
private IntRef 			date = new IntRef();
private int[] 			accX = new int[ACC_SAMPLE_RATE];
private int[] 			accY = new int[ACC_SAMPLE_RATE];
private int[] 			accZ = new int[ACC_SAMPLE_RATE]; 

The variable ACC_SAMPLE_RATE is the frequency in hertz of the sample you are using. For instance if the value is 20, that means that the accelerometer records a value each 50 ms.
The Buffref is used to save the value record by the accelerometers.
Then we create three tables in which the program will save the values of a one second recording.

The start() method

In the start() method, you only need to start the thread that the program uses.

public void start() {
		new Thread(this).start();

The getAccelValue() method

Next we need a method to convert the data recorded by the accelerometer from binary to decimal.
This method is named getAccelValue(BuffRef r, int index). The two parameters are the buffref you want to convert and the index of the table in which the method saves the value.

The code of this method is:

private int getAccValue(BuffRef r, int index) throws Exception {
		int b0 = r.byteAt(index * 4);
		int b1 = r.byteAt(index * 4 + 1);
		int b2 = r.byteAt(index * 4 + 2);
		int b3 = r.byteAt(index * 4 + 3);
		return (((b0 & 0x000000FF) << 24) + 
				((b1 & 0x000000FF) << 16) + 
				((b2 & 0x000000FF) << 8) + 
				((b3 & 0x000000FF) << 0));

The run() method

In this thread, you have to do two things.
First you need to recover the data record by the accelerometers, for this use the getAccel() method which is a method from the accelerometers API.
Then you use a loop to convert each data record and print it in the log.

public void run() {
		Accelerometers acc = Component.getInstance().getAccelerometers();
		Debug dbg = Component.getInstance().getDebug();
		int samplesCount = 0;


		while (true) {

			samplesCount = acc.getAccel(tmpAccX, tmpAccY, tmpAccZ, ACC_SAMPLE_RATE, date);

			for (int i = 0 ; i < samplesCount ; ++i) {
				try {
					accX[i] = getAccValue(tmpAccX, i);
					accY[i] = getAccValue(tmpAccY, i);	
					accZ[i] = getAccValue(tmpAccZ, i);
				} catch (Exception e) {
				dbg.debug("Acc:\t"+ accX[i] + "\t" + accY[i] + "\t" + accZ[i]);

			dbg.debug("Read " + samplesCount + " samples.");


You can download the project here