کد:
/*
#
# Example code for GY-271 HMC5883 COMPASS
# Company : AftabRayaneh
# Website : http://shop.aftabrayaneh.com
# Editor : mohammad omidvar -max
# Date : 12.10.2013
# Version : 1.0
*/
#include <Wire.h>
#include <HMC5883L.h>
HMC5883L compass;
void setup(){
Serial.begin(9600);
Wire.begin();
compass = HMC5883L(); //new instance of HMC5883L library
setupHMC5883L(); //setup the HMC5883L
}
// Our main program loop.
void loop(){
float heading = getHeading();
Serial.println(heading);
delay(100); //only here to slow down the serial print
}
void setupHMC5883L(){
//Setup the HMC5883L, and check for errors
int error;
error = compass.SetScale(1.3); //Set the scale of the compass.
if(error != 0) Serial.println(compass.GetErrorText(error)); //check if there is an error, and print if so
error = compass.SetMeasurementMode(Measurement_Continuous); // Set the measurement mode to Continuous
if(error != 0) Serial.println(compass.GetErrorText(error)); //check if there is an error, and print if so
}
float getHeading(){
//Get the reading from the HMC5883L and calculate the heading
MagnetometerScaled scaled = compass.ReadScaledAxis(); //scaled values from compass.
float heading = atan2(scaled.YAxis, scaled.XAxis);
// Correct for when signs are reversed.
if(heading < 0) heading += 2*PI;
if(heading > 2*PI) heading -= 2*PI;
return heading * RAD_TO_DEG; //radians to degrees
}