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?
thanks
#include <Wire.h>
#include <HMC5883L.h>
//const int northled = 13
HMC5883L compass;
//const int point = 200;//where led turns on or off
void setup(){
Serial.begin(9600);
pinMode(13, OUTPUT);
pinMode(12, OUTPUT);
Wire.begin();
compass = HMC5883L(); //new instance of HMC5883L library
setupHMC5883L(); //setup the HMC5883L
}
// Our main program loop.
void loop(){
//int north = 250;
float heading = getHeading();
Serial.println(heading);
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);}
else
{
digitalWrite (13, LOW);
digitalWrite(12, LOW);
}
return heading * RAD_TO_DEG; //radians to degrees
}