/elec/quadcopter

To get this branch, use:
bzr branch http://bzr.ed.am/elec/quadcopter
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <Wire.h> // I2C Arduino Library

#define address 0x1E // 0011110b, I2C 7bit address of HMC5883

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
  }
  
  // print out values of each axis
  Serial.print("x: ");
  Serial.print(x);
  Serial.print("  y: ");
  Serial.print(y);
  Serial.print("  z: ");
  Serial.println(z);
  
  delay(250);
}