| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,19 @@ | |||
| 1 | + import processing.io.*; | ||
| 2 | + HMC6352 compass; | ||
| 3 | + | ||
| 4 | + // see setup.png in the sketch folder for wiring details | ||
| 5 | + | ||
| 6 | + void setup() { | ||
| 7 | + // the module's I2C address can be changed by modifying values in its EEPROM | ||
| 8 | + // 0x21 is however the default address | ||
| 9 | + | ||
| 10 | + //printArray(I2C.list()); | ||
| 11 | + compass = new HMC6352("i2c-1", 0x21); | ||
| 12 | + } | ||
| 13 | + | ||
| 14 | + void draw() { | ||
| 15 | + background(255); | ||
| 16 | + float deg = compass.heading(); | ||
| 17 | + println(deg + " degrees"); | ||
| 18 | + line(width/2, height/2, width/2+sin(radians(deg))*width/2, height/2-cos(radians(deg))*height/2); | ||
| 19 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,38 @@ | |||
| 1 | + import processing.io.I2C; | ||
| 2 | + | ||
| 3 | + // HMC6352 is a digital compass using I2C | ||
| 4 | + // datasheet: https://www.sparkfun.com/datasheets/Components/HMC6352.pdf | ||
| 5 | + | ||
| 6 | + class HMC6352 extends I2C { | ||
| 7 | + int address; | ||
| 8 | + | ||
| 9 | + HMC6352(String dev, int address) { | ||
| 10 | + super(dev); | ||
| 11 | + this.address = address; | ||
| 12 | + setHeadingMode(); | ||
| 13 | + } | ||
| 14 | + | ||
| 15 | + void setHeadingMode() { | ||
| 16 | + beginTransmission(address); | ||
| 17 | + // command byte for writing to EEPROM | ||
| 18 | + write(0x77); | ||
| 19 | + // address of the output data control byte | ||
| 20 | + write(0x4e); | ||
| 21 | + // give us the plain heading | ||
| 22 | + write(0x00); | ||
| 23 | + endTransmission(); | ||
| 24 | + } | ||
| 25 | + | ||
| 26 | + float heading() { | ||
| 27 | + beginTransmission(address); | ||
| 28 | + // command byte for reading the data | ||
| 29 | + write(0x41); | ||
| 30 | + byte[] in = read(2); | ||
| 31 | + endTransmission(); | ||
| 32 | + // put bytes together to tenth of degrees | ||
| 33 | + // & 0xff makes sure the byte is not interpreted as a negative value | ||
| 34 | + int deg = (in[0] & 0xff) << 8 | (in[1] & 0xff); | ||
| 35 | + // return degrees | ||
| 36 | + return deg / 10.0; | ||
| 37 | + } | ||
| 38 | + } | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments