Determining heading for a robot based on the Arduino platform

I suggest you do some reading on "tilt compensated compass". You'll will find a lot of information on these forums and other websites about digital compasses.