Tilt compensating a HMC5883 compass with a MMA7455 Accelerometer

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);
}

Why do you think that would work?

Because i remember seeing a tutorial about it some months ago but can't find it now.

The code above does work

You compensate compasses with an accelerometer

What does that code do for what you are asking?

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

Take a look at this :

http://bildr.org/2012/02/hmc5883l_arduino/

http://diy.powet.eu/2011/03/19/tilt-compensation-azimuth-pitch-le-roll/?lang=en

http://forums.parallax.com/showthread.php/152502-Tilt-Compensation-Magnetometer-(-HMC5883L)

http://www.timzaman.com/?p=1010

http://www.electrodragon.com/w/index.php?title=HMC5883L_Three-Axis_Compass_Magneticfield_Module

Thank you so much thats awesome. Exactly what i was looking for.

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.

Yeah thats great thank you i will.

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.

No mate, im greatful. REALLY greatful

Hi,

I am trying to make a network for RF24 modules but could not achieve. I loaded github RF24Network softwares but it gives error. What can i do ?

Regadrs

Hi,

I am trying to make a network for RF24 modules but could not achieve. I loaded github RF24Network softwares but it gives error. What can i do ?

Regards