# Why does my LSM303 magnetometer reading not change in a while loop?

I am using a LSM303 sensor to compute a heading and I want to turn my robot to a heading.

I have the simple code here:

int mag;
mag = compass.heading((LSM303::vector){0,-1,0});;   //read the angle of the robot

Serial.println(mag);
while (mag != angle){
//while it isn't the desired angle turn and continue to update the robot angle
trex.write(0xE9);
trex.write(90);
trex.write(90);
mag = compass.heading((LSM303::vector){0,-1,0});;   //read the angle of the robot

}


In a function called with a speed and angle (heading), the trex part tells the motor controller to turn on a point and the while loop should test for when the desired heading is reached. However, testing using a couple of instances of Serial.println(mag); I have determined that once inside the while loop, mag never changes which just means the robot turns indefinitely.

I have no idea why this would happen. Perhaps someone here does?

Thanks.

• if mag is not changing, then compass.heading(...) should be investigated to see if your i2c implementation is correct. Did you test the compass for working condition at any time? May 2, 2013 at 19:01
• Yes - sorry probably should have mentioned, if I run mag = compass.heading((LSM303::vector){0,-1,0});; by itself and then move the compass and run it again it performs correctly and the heading changes as expected.
– mark mcmurray
May 2, 2013 at 21:43
• I am confused here, if mag changes when robot turns, it should change inside the while loop since the robot is rotating constantly? question: is angle the desired angle and mag the actual angle? Have you had a print inside the while loop? May 2, 2013 at 22:48
• Another thought... Do you need some time delay between readings? I am not familiar with your compass, but the documentation should indicate any constraints that you need to pay attention to. May 2, 2013 at 22:54
• "I am confused here, if mag changes when robot turns, it should change inside the while loop since the robot is rotating constantly?" That's what I thought, I doubted myself and thought perhaps I really didn't understand the fundamentals of while loops as much as I thought.
– mark mcmurray
May 3, 2013 at 0:16

## 1 Answer

I don't know Arduino from Aardvark, so this comment is based purely on how a modern C++ compiler would behave:

Were you asking about a plain ol' C++ problem, I would ask you if compass is declared volatile, or if the 'heading' method of compass's class is declared volatile, or if your system is based on cooperative multitasking.

In the first two cases (lack of things being tagged as volatile) the compiler will optimize the call to compass.heading to just outside the loop (to save you time -- isn't that nice?).

In the third case, whatever the code is that actually acquires the values for the compass structure will never get a chance to run (because your code is stuck in the while loop) and you'll never see results.

I hope this makes sense to the Arduino environment...