I have an ADXL335 accelerometer connected to my Arduino. Here is some code that I am running based off a tutorial:
xaccl[a] = float(analogRead(xpin) - 345);
yaccl[a] = float(analogRead(ypin) - 346);
zaccl[a] = float(analogRead(zpin) - 416);
float length = sqrt((xaccl[a] * xaccl[a]) + (yaccl[a] * yaccl[a]) + (zval[a] * zval[a]));
Serial.println(length);
When the accelerometer is flat on my desk, the vector length is around 128. When the accelerometer is tilted, the length of the vector increases even if it is not moving. The 3d vector length should stay constant regardless of the angle.
- Could someone explain what is happening?
- Also, what unit of measurement does the accelerometer output? I assume m/s^2.
- Lastly, some sample code I found for reading values:
xaccl[a] = float(analogRead(xpin) - 345);
yaccl[a] = float(analogRead(ypin) - 346);
zaccl[a] = float(analogRead(zpin) - 416);
Why is the code subtracting 345, 345, and 416?