Data from sensor into an array

Hello everyone.

I’m using a LSM303DLH Sensor (accelerometer + magnetometer). The Arduino library from the producer allows to display data in the serial port continuosly. I’d like to put the received data into an array and display it into the serial port. I’d like to maximise the values, so I was thinking to use arudino loop in free running and when the data in the array fill the flash memory, send the data stored into the array to the serial port. The problem is that this is my very first work with Arduino and I really don’t know what to do to accomplish this. I’ve read the basics for the arrays in general, but I have not clear how I can modify the code for this.

This is the sketch of Polulu for my sensor:

/*
The sensor outputs provided by the library are the raw 16-bit values
obtained by concatenating the 8-bit high and low accelerometer and
magnetometer data registers. They can be converted to units of g and
gauss using the conversion factors specified in the datasheet for your
particular device and full scale setting (gain).

Example: An LSM303D gives a magnetometer X axis reading of 1982 with
its default full scale setting of +/- 4 gauss. The M_GN specification
in the LSM303D datasheet (page 10) states a conversion factor of 0.160
mgauss/LSB (least significant bit) at this FS setting, so the raw
reading of -1982 corresponds to 1982 * 0.160 = 317.1 mgauss =
0.3171 gauss.

In the LSM303DLHC, LSM303DLM, and LSM303DLH, the acceleration data
registers actually contain a left-aligned 12-bit number, so the lowest
4 bits are always 0, and the values should be shifted right by 4 bits
(divided by 16) to be consistent with the conversion factors specified
in the datasheets.

Example: An LSM303DLH gives an accelerometer Z axis reading of -16144
with its default full scale setting of +/- 2 g. Dropping the lowest 4
bits gives a 12-bit raw value of -1009. The LA_So specification in the
LSM303DLH datasheet (page 11) states a conversion factor of 1 mg/digit
at this FS setting, so the value of -1009 corresponds to -1009 * 1 =
1009 mg = 1.009 g.
*/

#include <Wire.h>
#include <LSM303.h>

LSM303 compass;

char report[80];

void setup()
{
  Serial.begin(9600);
  Wire.begin();
  compass.init();
  compass.enableDefault();
}

void loop()
{
  compass.read();

  snprintf(report, sizeof(report), "A: %6d %6d %6d    M: %6d %6d %6d",
    compass.a.x, compass.a.y, compass.a.z,
    compass.m.x, compass.m.y, compass.m.z);
  Serial.println(report);

  delay(100);
}

I think it’s possible to modify this to have the values of the accelerometer and magnetometer dispayed.

Thanks in advance.

Since I don't have the LM library installed, I don't really understand the compass class. However, you state that the correct answer for Z is 1.009. If that's the case, shouldn't the %d specs in the snprintf() be %f?

If you want to put the data into an array:

void loop()
{
  compass.read();

  //Put data into an array.
  int data[6] = {compass.a.x, compass.a.y, compass.a.z, compass.m.x, compass.m.y, compass.m.z};

  delay(100);
}

The only thing you need to double-check is what the compass data type is. I assumed they were integers since the example code uses %d, but they may in fact be floats. If they are indeed floats, change "int data[6]" to "float data[6]".

Thanks pcdangio, this is very useful. At the moment I'm trying to use a for cycle to store as much data as possible into the array.