I was hoping some one could advise on how to use the MMA7455 Accelerometer to tilt compensate my compass.
I am using an arduino mega and arduino pro if that helps at all.
Current Compass Sketch
#include <Wire.h> //I2C Arduino Library
#define address 0x1E //0011110b, I2C 7bit address of HMC5883
float angle;
void setup(){
//Initialize Serial and I2C communications
Serial.begin(9600);
Wire.begin();
//Put the HMC5883 IC into the correct operating mode
Wire.beginTransmission(address); //open communication with HMC5883
Wire.write(0x02); //select mode register
Wire.write(0x00); //continuous measurement mode
Wire.endTransmission();
}
void loop(){
int x,y,z; //triple axis data
//Tell the HMC5883 where to begin reading data
Wire.beginTransmission(address);
Wire.write(0x03); //select register 3, X MSB register
Wire.endTransmission();
//Read data from each axis, 2 registers per axis
Wire.requestFrom(address, 6);
if(6<=Wire.available()){
x = Wire.read()<<8; //X msb
x |= Wire.read(); //X lsb
z = Wire.read()<<8; //Z msb
z |= Wire.read(); //Z lsb
y = Wire.read()<<8; //Y msb
y |= Wire.read(); //Y lsb
}
angle= atan2((double)y,(double)x) * (180 / 3.14159265) + 180;
//Print out values of each axis
Serial.print("You are heading ");
if((angle < 22.5) || (angle > 337.5 ))
Serial.print("South");
if((angle > 22.5) && (angle < 67.5 ))
Serial.print("South-West");
if((angle > 67.5) && (angle < 112.5 ))
Serial.print("West");
if((angle > 112.5) && (angle < 157.5 ))
Serial.print("North-West");
if((angle > 157.5) && (angle < 202.5 ))
Serial.print("North");
if((angle > 202.5) && (angle < 247.5 ))
Serial.print("NorthEast");
if((angle > 247.5) && (angle < 292.5 ))
Serial.print("East");
if((angle > 292.5) && (angle < 337.5 ))
Serial.print("SouthEast");
Serial.println(angle);
delay(250);
}
With my code, it gives a heading for direction, but when the compass tilts a little, the readings change and become inaccurate. For example if its facing north and you tilt it to the side, it will read west.
im asking for advice on how to read from the accelerometer, and use that data to compensate the reading from the tilt
Your welcome.
We never know how someone is going to react when we give them more than one link.
I gave some guy a bunch of links the other day and he replied "Why did you puke a bunch of links on my screen ?"
I do the same thing with you and you reply :
Thank you so much thats awesome. Exactly what i was looking for.
It looked like at least a couple of those had the tilt-compensation code.
When you install the Bildr tutorial library let me know if you have any issues like getting errors. The zip file screws up the file sctructure when you unzip it directly into your libraries\sketch folder. It creates TWO folders, one inside the other , both with the same name. You have to cut all the files out of the folder at the lowest level and move them up and delete the empty folder
so the "Examples" folder is at the first level and the examples at the second level. You'll see what I mean.
The way i see it, you dont want people to give you the answer, you should be given advise on the way to go. In that way, the more reference the better.
So if i moaned after you went to so much answer simply to try and answer a question then thats ME being an arse. Screw that, you found loads of reference that enables me to LEARN to do it my self. Thats what people should want.
If you are given the answer right out, then you don't even know what does what then later on, when you need something else your lost, yet the answer could have been right there in the first place.