Go Down

Topic: Control LED ON/OFF by HMC5883L (Read 974 times) previous topic - next topic

davidwang928


I want to control LED on/off by HMC5883L
If X angle between 70 and 80 LED on, else LED off.

But my test results is LED all on when  X angle equal to any value.
Please help.

Thank you

My code is as follows:

#include <Wire.h>
#include <HMC5883L.h>
HMC5883L compass;
void setup()
{
  Serial.begin(9600);
  Wire.begin();
  compass = HMC5883L();
  compass.SetScale(1.3);
  compass.SetMeasurementMode(Measurement_Continuous);
  pinMode(8, OUTPUT);
}
void loop()
{
  MagnetometerRaw raw = compass.ReadRawAxis();
  MagnetometerScaled scaled = compass.ReadScaledAxis();
  float xHeading = atan2(scaled.YAxis, scaled.XAxis);

  if(xHeading < 0) xHeading += 2*PI;

  int xDegrees = xHeading * 180/M_PI;

  Serial.print(xDegrees);
  Serial.print(",");
  if(70< xDegrees <80){
  digitalWrite(8, HIGH);
  }
  else{
    digitalWrite(8, LOW); 
  }
 
 
  delay(100);
}

AWOL

Code: [Select]
if((xDegrees > 70) &&  (xDegrees < 80)){

Code tags, please.

davidwang928


Go Up