|
|
各位大神好,
我想請教各位,我想要使用HMC5883L控制LED,
當X角度介於70~80時,LED才亮燈,否則熄燈。
可是我測試結果無論X角度為何,LED接亮燈。
懇請各位大神指導。
謝謝
我的原碼如下:
#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);
}
|
|