After a few weeks of off and on messing with this, The compass part works, I think.
I was moving it on the counter top. I think there are nails, screws, whatever that is messing with readings. I bring in a cardboard box and use that for the flat surface and I'm getting readings that are a complete 360 degrees.
If I hold it above the box and rotate it, the results aren't as good which leads me to believe that I need to implement the tilt compensation.
If anyone one can point me to how to do this it would be greatly appreciated. I'm not finding anything with searches.
Here's the code I'm using at this time.
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_LSM303_U.h>
/* Assign a unique ID to this sensor at the same time */
Adafruit_LSM303_Mag_Unified mag = Adafruit_LSM303_Mag_Unified(12345);
void setup(void)
{
Serial.begin(9600);
Serial.println("Magnetometer Test"); Serial.println("");
/* Initialise the sensor */
if(!mag.begin())
{
/* There was a problem detecting the LSM303 ... check your connections */
Serial.println("Ooops, no LSM303 detected ... Check your wiring!");
while(1);
}
}
void loop(void)
{
/* Get a new sensor event */
sensors_event_t event;
mag.getEvent(&event);
float Pi = 3.14159;
// Calculate the angle of the vector y,x
float heading = (atan2(event.magnetic.y,event.magnetic.x) * 180) / Pi;
// Normalize to 0-360
if (heading < 0)
{
heading = 360 + heading;
}
Serial.print("Compass Heading: ");
Serial.println(heading);
delay(500);
}
Thanks in advance!