Go Down

Topic: Converting G values into floats from accelerometer (Read 403 times) previous topic - next topic

buttcheekbutter

I am having an issue with trying to change the G values from the accelerator from int to float. When I simply change the variable type from int to float, it's serial output looks like this while sitting still:

Code: [Select]
-26844 -17153 -26844
mph/s: -0.17
-20133 -17217 -26844
mph/s: -0.17
-26844 -17153 -26844
mph/s: -0.17
-20133 -17217 -26844
mph/s: -0.17
-20133 -17217 -26844
mph/s: -0.17
-20133 -17217 -26844
mph/s: -0.17
-20133 -17217 -26844
mph/s: -0.17


While when it's just a plain int variable type, it looks normal:

Code: [Select]
0 0 1
mph/s: 0
0 0 1
mph/s: 0
0 0 1
mph/s: 0
0 0 1
mph/s: 0
0 0 1
mph/s: 0
0 0 1
mph/s: 0


However, I require the float version of the G values so that I can get an accurate calculation of the mph/s.

Here is the full Arduino code using an ADXL345 accelerometer:

Code: [Select]
#include <Wire.h>

#define DEVICE (0x1D)    //ADXL345 device address
#define TO_READ (6)        //num of bytes we are going to read each time (two bytes for each axis)

byte buff[TO_READ] ;    //6 bytes buffer for saving data read from the device
char str[512];                      //string buffer to transform data before sending it to the serial port

void setup()
{
  Wire.begin();        // join i2c bus (address optional for master)
  Serial.begin(9600);  // start serial for output

  //Turning on the ADXL345
  writeTo(DEVICE, 0x2D, 0);     
  writeTo(DEVICE, 0x2D, 16);
  writeTo(DEVICE, 0x2D, 8);
}

void loop()
{
  int regAddress = 0x32;    //First axis-acceleration-data register on the ADXL345
  int x, y, z;
  float xg, yg, zg;
  float mphs;

  readFrom(DEVICE, regAddress, TO_READ, buff); //Read the acceleration data from the ADXL345

   //Each axis reading comes in 10 bit resolution, ie 2 bytes.  Least Significat Byte first!
   //Thus, we are converting both bytes in to one int.
  x =   ((((int)buff[1]) << 8) | buff[0]);
  y =   ((((int)buff[3]) << 8) | buff[2]);
  z =   ((((int)buff[5]) << 8) | buff[4]);

  // Convert the raw XYZ values into g values.
  xg = x * 0.0078;
  yg = y * 0.0078;
  zg = z * 0.0078;

  // Convert the forward axis (Y) g value into mph/s (miles per hour per second).
  mphs = yg * 21.93685129;

  //we send the x y z values as a string to the serial port
  sprintf(str, "%d %d %d", xg, yg, zg);
  Serial.print(str);
  Serial.write(10);

  Serial.print("mph/s: ");
  Serial.println(mphs);

  //It appears that delay is needed in order not to clog the port
  delay(15);
}

void writeTo(int device, byte address, byte val) {
   Wire.beginTransmission(device); //start transmission to device
   Wire.write(address);        // send register address
   Wire.write(val);        // send value to write
   Wire.endTransmission(); //end transmission
}

void readFrom(int device, byte address, int num, byte buff[]) {
  Wire.beginTransmission(device); //start transmission to device
  Wire.write(address);        //sends address to read from
  Wire.endTransmission(); //end transmission

  Wire.beginTransmission(device); //start transmission to device (initiate again)
  Wire.requestFrom(device, num);    // request 6 bytes from device

  int i = 0;
  while(Wire.available())    //device may send less than requested (abnormal)
  {
    buff[i] = Wire.read(); // receive a byte
    i++;
  }
  Wire.endTransmission(); //end transmission
}

PaulS

Code: [Select]
  sprintf(str, "%d %d %d", xg, yg, zg);
xg, yg, and zg are not ints. Using a integer format specifier for floats will NOT work. Nor will using the %f specifier, since it is not supported on the Arduino.

On the other hand, this whole function call is unnecessary. Just print the variables one at a time.

The mphs value looks OK. The float value truncated to an int matches. That the value is not what you expect says more about your expectations.
The art of getting good answers lies in asking good questions.

buttcheekbutter


Code: [Select]
  sprintf(str, "%d %d %d", xg, yg, zg);
xg, yg, and zg are not ints. Using a integer format specifier for floats will NOT work. Nor will using the %f specifier, since it is not supported on the Arduino.

On the other hand, this whole function call is unnecessary. Just print the variables one at a time.

The mphs value looks OK. The float value truncated to an int matches. That the value is not what you expect says more about your expectations.


Thank you so much, it worked perfectly!  I've been trying to fix this for over 2 weeks now!

Go Up