digital compass

very new to all this so go easy on me. basically just wanting to turn a couple leds on when a compass reading in degrees goes beyond a certain point. for now its set to turn leds on if heading is greater then 200(degrees)

so here is my problem my if statement compares “heading” to 200. And if greater then 200 turns on leds but it seams as if my heading only has a range of 1-8 and not 1-360 i know its something simple im not getting here but im going crazy trying to figure it out. please help me before i go totally nuts!!!

the serial print goes from 1-360 nicely. maybe “heading” is not what im supposed to compare to?


#include <Wire.h>
#include <HMC5883L.h>
//const int northled = 13
HMC5883L compass;
//const int point = 200;//where led turns on or off

void setup(){
pinMode(13, OUTPUT);
pinMode(12, OUTPUT);

 compass = HMC5883L(); //new instance of HMC5883L library
 setupHMC5883L(); //setup the HMC5883L

// Our main program loop.
void loop(){
  //int north = 250;
 float heading = getHeading();
 delay(100); //only here to slow down the serial print


void setupHMC5883L(){
 //Setup the HMC5883L, and check for errors
 int error; 
 error = compass.SetScale(1.3); //Set the scale of the compass.
 if(error != 0) Serial.println(compass.GetErrorText(error)); //check if there is an error, and print if so

 error = compass.SetMeasurementMode(Measurement_Continuous); // Set the measurement mode to Continuous
 if(error != 0) Serial.println(compass.GetErrorText(error)); //check if there is an error, and print if so

float getHeading(){
 //Get the reading from the HMC5883L and calculate the heading
 MagnetometerScaled scaled = compass.ReadScaledAxis(); //scaled values from compass.
 float heading = atan2(scaled.YAxis, scaled.XAxis);

 // Correct for when signs are reversed.
 if(heading < 0) heading += 2*PI;
 if(heading > 2*PI) heading -= 2*PI;
 if( heading > 200 )// here is  the section im having rouble with!!!!!

{digitalWrite(13, HIGH);
digitalWrite(12, HIGH);}

digitalWrite (13, LOW);
digitalWrite(12, LOW);

 return heading * RAD_TO_DEG; //radians to degrees

The variable heading is a float, but the if expression is written with:

   if (heading > 200)

which causes the compiler to view the 200 as an int. Try converting ALL expressions that use a numeric constant involving a float data type to floating point constants, like:

if(heading < 0.0) heading += 2.0 * PI;
 if(heading > 2.0 * PI) heading -= 2.0 * PI;
 if( heading > 200.0 )// here is  the section im having rouble with!!!!!

and see if that helps.

thanks for the suggestion but no change. leds don't some on at all. unless i change the 200 down to a 2 then i can turn the compass and turn them on and off. but not really in any relation to degrees

Heading is computed in radians, but you are comparing it to 200 degrees.
Multiply the final heading by (180./PI) to get degrees.

FINALY!! yes yes! is it wrong that i love you right now? lol