const int xInput = A0;

const int yInput = A1;

const int zInput = A2;

// initialize minimum and maximum Raw Ranges for each axis

int RawMin = 0;

int RawMax = 1023;

// Take multiple samples to reduce noise

const int sampleSize = 10;

void setup()

{

//It works

//Configures the reference voltage used for analog input

//analogReference(EXTERNAL);

Serial.begin(9600);

}

void loop()

{

//Read raw values

int xRaw = ReadAxis(xInput);

int yRaw = ReadAxis(yInput);

int zRaw = ReadAxis(zInput);

// Convert raw values to 'milli-Gs"

long xScaled = map(xRaw, RawMin, RawMax, -3000, 3000);

long yScaled = map(yRaw, RawMin, RawMax, -3000, 3000);

long zScaled = map(zRaw, RawMin, RawMax, -3000, 3000);

// re-scale to fractional Gs

float xAccel = xScaled / 1000.0;

float yAccel = yScaled / 1000.0;

float zAccel = zScaled / 1000.0;

Serial.print(F("X, Y, Z :: "));

Serial.print(xRaw);

Serial.print(F(", "));

Serial.print(yRaw);

Serial.print(F(", "));

Serial.print(zRaw);

Serial.print(F(" :: "));

Serial.print(xAccel,0);

Serial.print(F("G, "));

Serial.print(yAccel,0);

Serial.print(F("G, "));

Serial.print(zAccel,0);

Serial.println("G");

}

It actually works when you add a , 0 on for example Serial.print (zAccel,0) because it rounds to the nearest numbers. When you take off ,0 it will give you the decimals places and that is when the problem arises. I took a look at the data sheet and when you have the Z axis facing upward it gives me .56G force and -.77G force when in reality it is supposed to be +1 when facing up and -1 when facing down due to Earth's force of gravity. Can anyone please help me out. Im using the 5V on the Arduino Mega board as well. Any help would be much appreciated, thank you!

More Reyki Garcia's questions See All
Similar questions and discussions