#include <Wire.h> //I2C Arduino Library
#define address 0x0D
void setup()
{
//Initialize Serial and I2C communications
Serial.begin(9600);
Wire.begin();
//Put the QMC5883L IC into the correct operating mode:
Wire.beginTransmission(address); //Open communication with QMC5883L.
Wire.write(0x09); //Select mode register.
Wire.write(0x1D); //Continuous measurement mode.
Wire.endTransmission();
}
void loop()
{
int x,y,z;//Triple axis data.
Wire.beginTransmission(address); //Tell the QMC5883L where to begin reading data.
Wire.write(0x00); //Select 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.
}
float heading = atan2(y, x); //Calculate Heading in Radians.
//Correct for when signs are reversed:
if(heading < 0)
heading += 2*PI;
//Check for wrap due to addition of declination:
if(heading > 2*PI)
heading -= 2*PI;
float headingDegrees = heading * 180/M_PI; //Convert radians to degrees.
Serial.print("Heading: ");
Serial.println(headingDegrees);
delay(1000);
--
You received this message because you are subscribed to the Google Groups "London Hackspace" group.
To unsubscribe from this group and stop receiving emails from it, send an email to london-hack-sp...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
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.
xlsb = Wire.read();
xmsb= Wire.read();
ylsb = Wire.read();
ymsb = Wire.read();
zlsb = Wire.read();
zmsb = Wire.read();
x = ((xMSB<<8) | xLSB);
y = ((yMSB<<8) | yLSB);
z = ((zMSB<<8) | zLSB);