I am having some issues when reporting data to the serial monitor. Below, I have included the relevant parts of the code and sample output. I first initialize variables and then run a series of functions within my void loop. When it comes to printing the data, it seems to be rounding or doing something that I do not quite understand.
For example, looking at the output, acceleration in gs seems to be rounded to the nearest integer even though I set it as a float. Accel (g) is calculated by taking the Raw Accel obtained from the appropriate registers and dividing by 8192, following the sensor specification document. This should result in a decimal answer and not zero or one. What am I doing wrong?
Code (Note that this is not the code in full):
long accelX, accelY, accelZ;
float gForceX, gForceY, gForceZ;
float AccelForceX, AccelForceY, AccelForceZ;
void loop() {
recordAccelRegisters();
processAccelData();
printData();
delay(3000);
}
void processAccelData(){
gForceX = accelX/8192;
gForceY = accelY/8192;
gForceZ = accelZ/8192;
AccelForceX = gForceX*9.80665;
AccelForceY = gForceY*9.80665;
AccelForceZ = gForceZ*9.80665;
}
void printData() {
Serial.print("Raw Accel");
Serial.print(" X=");
Serial.print(accelX);
Serial.print(" Y=");
Serial.print(accelY);
Serial.print(" Z=");
Serial.println(accelZ);
Serial.print("Accel (g)");
Serial.print(" X=");
Serial.print(gForceX,4);
Serial.print(" Y=");
Serial.print(gForceY,4);
Serial.print(" Z=");
Serial.println(gForceZ,4);
Serial.print("Accel (m/s)");
Serial.print(" X=");
Serial.print(AccelForceX,4);
Serial.print(" Y=");
Serial.print(AccelForceY,4);
Serial.print(" Z=");
Serial.println(AccelForceZ,4);
}
Output:
Raw Accel X=110 Y=368 Z=8392
Accel (g) X=0.0000 Y=0.0000 Z=1.0000
Accel (m/s) X=0.0000 Y=0.0000 Z=9.8066