Hi all,
Recently I met a HMC5883L magnetic compass reading problem. After I connected it with my arduino, I can read output values correctly. but after rotating it sometimes say 15 seconds later, something strange happened. the value shown on the Serial window was either stopped or just continusly shows the same value. First I was wondering maybe it is the Serial.println problem, but later I use the compass to control a servo.
It did the same thing as I described above. The servo rotation is just stopped, no matter how I rotate the sensor.
Could any one help me out.
Thanks.
below is my code
#include <Wire.h>
double res = 0;
void setup()
{
Wire.begin();
Serial.begin(9600);
initHMC5843();
delay(100);
}
void loop()
{
readxyz();
}
void initHMC5843()
{
delay(5);
Wire.beginTransmission(0x1E);// 7 bits address
Wire.send(0x02);
Wire.send(0x00); // continues reading
Wire.endTransmission();
}
void readxyz()
{
Wire.beginTransmission(0x1E);
Wire.send(0x03);
Wire.endTransmission();
delay(5);
Wire.requestFrom(0x1E, 6);
if(6 <= Wire.available())
{
int x,y,z;
x = Wire.receive() << 8;
x |= Wire.receive();
y = Wire.receive() << 8;
y |= Wire.receive();
z = Wire.receive() << 8;
z |= Wire.receive();
if(x>0)
{
res = map(x, 0, 246, 0, 90);
}
else if(x<0)
{
if(x!=0)
{
res = abs(x);
res = map(res,0,255,90,0);
res = 270+res;
}
else
res = 0;
}
Serial.println(res);
}
}