The idea behind the code is to never turn more than 180 degrees, otherwise it is faster to turn the other way : TurnRight(300) ==TurnLeft(60)
A piece of code like this should be somewhere in your sketch
void loop()
{
...
wantedDir = 10; // or determined some other way e.g. XBEE.Read()
currentDir = compass.ReadDirection(); // or something like that
doSmartTurn(currentDir, wantedDir);
...
}
Of course it can be in another separate function instead of in loop()
I looked at the doSmartTurn() => fixed a bug as directions could be >> 360 and a typo
// version 0.2
doSmartTurn(unsigned int curDirection, unsigned int newDirection)
{
// force directions between 0..359
curDirection %= 360;
newDirection %= 360;
int delta = newDirection - curDirection;
if (0 == delta) return; // no need to turn; // better? if (abs(delta) <= threshold) return; // don't do small angles.
if (delta > 180) turnRight(360 - delta);
else if (delta > 0) turnLeft(delta);
else if (delta > -180) turnRight(-delta);
else turnLeft(360+delta);
}