Skip to content

Instantly share code, notes, and snippets.

@michelleboisson
Created December 19, 2012 05:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michelleboisson/4334578 to your computer and use it in GitHub Desktop.
Save michelleboisson/4334578 to your computer and use it in GitHub Desktop.
Code for Compass Module test
//This reads the heading from HMC6352, spit it out via serial and lights up 1-2 lights to show North
//HMC6352 compass is hooked up this way: SDA is on analog input pin 4, and SCL is on analog pin 5
#include <Wire.h>
int HMC6352SlaveAddress = 0x42;
int HMC6352ReadAddress = 0x41; //"A" in hex, A command is:
int headingValue;
int EPin = 13;//east
int SPin = 12;//south
int WPin = 9;//west
int NPin = 8;//north
void setup(){
// "The Wire library uses 7 bit addresses throughout.
//If you have a datasheet or sample code that uses 8 bit address,
//you'll want to drop the low bit (i.e. shift the value one bit to the right),
//yielding an address between 0 and 127."
HMC6352SlaveAddress = HMC6352SlaveAddress >> 1; // I know 0x42 is less than 127, but this is still required
pinMode(WPin, OUTPUT);
pinMode(NPin, OUTPUT);
pinMode(EPin, OUTPUT);
pinMode(SPin, OUTPUT);
Serial.begin(9600);
Wire.begin();
}
void loop(){
//"Get Data. Compensate and Calculate New Heading"
Wire.beginTransmission(HMC6352SlaveAddress);
Wire.write(HMC6352ReadAddress); // The "Get Data" command
Wire.endTransmission();
//time delays required by HMC6352 upon receipt of the command
//Get Data. Compensate and Calculate New Heading : 6ms
delay(6);
Wire.requestFrom(HMC6352SlaveAddress, 2); //get the two data bytes, MSB and LSB
//"The heading output data will be the value in tenths of degrees
//from zero to 3599 and provided in binary format over the two bytes."
byte MSB = Wire.read();
byte LSB = Wire.read();
float headingSum = (MSB << 8) + LSB; //(MSB / LSB sum)
float headingInt = headingSum / 10;
Serial.print(headingInt);
Serial.println(" degrees");
//North
if (headingInt > 330 || headingInt < 30 ){
digitalWrite(NPin, HIGH);
digitalWrite(EPin, LOW);
digitalWrite(SPin, LOW);
digitalWrite(WPin, LOW);
}
//NorthEast
if (headingInt > 30 && headingInt < 60 ){
digitalWrite(NPin, HIGH);
digitalWrite(EPin, HIGH);
digitalWrite(SPin, LOW);
digitalWrite(WPin, LOW);
}
//East
if (headingInt > 60 && headingInt < 120 ){
digitalWrite(EPin, HIGH);
digitalWrite(SPin, LOW);
digitalWrite(WPin, LOW);
digitalWrite(NPin, LOW);
}
//SouthEast
if (headingInt > 120 && headingInt < 150 ){
digitalWrite(EPin, HIGH);
digitalWrite(SPin, HIGH);
digitalWrite(NPin, LOW);
digitalWrite(WPin, LOW);
}
//South
if (headingInt > 150 && headingInt < 210 ){
digitalWrite(SPin, HIGH);
digitalWrite(NPin, LOW);
digitalWrite(WPin, LOW);
digitalWrite(EPin, LOW);
}
//SouthWest
if (headingInt > 210 && headingInt < 240 ){
digitalWrite(SPin, HIGH);
digitalWrite(WPin, HIGH);
digitalWrite(EPin, LOW);
digitalWrite(NPin, LOW);
}
//West
if (headingInt > 240 && headingInt < 300 ){
digitalWrite(WPin, HIGH);
digitalWrite(SPin, LOW);
digitalWrite(EPin, LOW);
digitalWrite(NPin, LOW);
}
//NorthhWest
if (headingInt > 300 && headingInt < 330 ){
digitalWrite(NPin, HIGH);
digitalWrite(WPin, HIGH);
digitalWrite(SPin, LOW);
digitalWrite(EPin, LOW);
}
delay(100);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment