Compass module

I want to print out the angle in degreese, like 43.1 from this module Triple Axis Magnetometer Breakout - HMC5843 - SEN-09371 - SparkFun Electronics

at the moment it prints out this rotating an whole round when its connected to 5v:

Direction 1: 75

Direction 2: 113

Direction 1: 76

Direction 2: 116

Direction 1: 76

Direction 2: 116

Direction 1: 76

Direction 2: 112

Direction 1: 74

Direction 2: 114

Direction 1: 77

Direction 2: 110

Direction 1: 76

Direction 2: 116

Direction 1: 77

Direction 2: 117

Direction 1: 77

Direction 2: 117

Direction 1: 77

Direction 2: 117

Direction 1: 77

Direction 2: 117

Direction 1: 77

Direction 2: 117

Direction 1: 77

Direction 2: 117

Direction 1: 77

Direction 2: 117

Direction 1: 77

Direction 2: 117

3volt gives this

Direction 1: 312

Direction 2: 300

Direction 1: 312

Direction 2: 300

Direction 1: 312

Direction 2: 300

Direction 1: 311

Direction 2: 300

Direction 1: 311

Direction 2: 300

/*
 * Smoothing
 * David A. Mellis <dam@mellis.org>
 *
 * Reads repeatedly from an analog input, calculating a running average
 * and printing it to the computer. 
 *
 * http://www.arduino.cc/en/Tutorial/Smoothing
 */

// Define the number of samples to keep track of.  The higher the number,
// the more the readings will be smoothed, but the slower the output will
// respond to the input.  Using a #define rather than a normal variable lets
// use this value to determine the size of the readings array.
#define NUMREADINGS 10
#define dir1  0
#define dir2  1


long previousMillis = 0;        // will store last time LED was updated
long interval = 3000;           // interval at which to blink (milliseconds)




int readings[NUMREADINGS][5];                // the readings from the analog input
int index = 0;                            // the index of the current reading

int total1,total2,direction1,direction2;



void setup()
{
  Serial.begin(9600); 
  Serial.println("No stratet serieporten");     // initialize serial communication with computer
 pinMode(2, OUTPUT); 
 digitalWrite(2,HIGH);
 delay(2000);
 digitalWrite(2,LOW);
 total1 = 0;
 total2 = 0;
}

void loop()
{

  total1 +=analogRead(dir1);  // add the reading to the total
  total2 += analogRead(dir2); // add the reading to the total
  


  index++;                    // advance to the next index

  if (index >= NUMREADINGS)   {            // if we're at the end of the array...
    index = 0;                            // ...wrap around to the beginning
   
    direction1 =  total1 / NUMREADINGS;          // calculate the average
    direction2 =  total2 / NUMREADINGS;          // calculate the average
    
    total1 = 0;
    total2 = 0;
    
  }

  if (millis() - previousMillis > interval) {
    previousMillis = millis();             // remember the last time we blinked the LED
    Serial.print("Direction 1: "); 
    Serial.println(direction1);               // send it to the computer (as ASCII digits)
    
    Serial.print("Direction 2: "); 
    Serial.println(direction2);               // send it to the computer (as ASCII digits)
    
  }
}

You need to calibrate the system.

Slowly rotate the unit until you read a minimum value. Mark that position then rotate it until you get a maximum value. Mark that position and measure the angle between the two positions.

Then convert that into an angle.

angle = (current reading - min Value) * ((angle you measured) / ((max value) - (min value) ) )

remember to use floating point variables.

Hi Mike, if you have one of those modules I am curious to know what values get for minimum and maximum readings.

I've been studying the documentation recently, and I was wondering if you could use the map() function to do this conversion as well?

http://www.arduino.cc/en/Reference/Map

Something like this:
angle = map(reading, minVal, maxVal, minValAngle, maxValAngle);

Hi Mike, if you have one of those modules I am curious to know what values get for minimum and maximum readings.

No sorry I am just going of the data sheet. :cry: