Tricky loop... help needed.(SOLVED)

Hello.
I recently started to learn Arduino but I´m too old to make reasonable progress…
Using a LSM303 Accelerometer/Magnetometer and a single button with Arduino Mega 2560, I wrote a sketch to accomplish the next actions.
At any moment, by pressing the button, the ‘heading’ is stored (‘storedHeading’ on sketch) and, simultaneously, a new variable ‘deltaHeading’ starts reading further azimut changes on LSM303.
At the end of this post I attach a Pic illustrating what seems to be the mentioned “Tricky loop”… (I can´t find the key to solve it)…
Here´s the code:

#include <Wire.h>
#include <LSM303.h> 

const int buttonPin = 2;                                         // Used to start comparations
int storedHeading;                                               // Desired course
int deltaHeading;                                                // +/- Difference (Course +/- Desired Course)
boolean buttonState;                                             // Initiates comparations
LSM303 compass;                                                  // Output from LSM303

void setup ()
{
  Serial.begin(9600);                                            // Initiates debuging serial COM
  Wire.begin();                                                  // Initiates I2C comunication
  compass.init();                                                // Initiates LSM303 
  compass.enableDefault();                                       // Enable dafault parameters on LSM303
  compass.m_min = (LSM303::vector<int16_t>){-361, -424, -268};   // Last parameter calibration readings made AUG-2014
  compass.m_max = (LSM303::vector<int16_t>){+371, +287, +378};   // Last parameter calibration readings made AUG-2014
}

void loop(void)
{ 
  compass.read();
  int heading = compass.heading();
  Serial.print("Heading: ");
  Serial.println(heading);
  int buttonState = digitalRead(buttonPin);
  if (buttonState == HIGH)
  {
    storedHeading = heading;
    Serial.print("Stored Heading: ");
    Serial.println(storedHeading);
  }
  deltaHeading = heading - storedHeading;                          /////////////////////////////////////////////
  if(abs(deltaHeading > 180))                                      //       errors when comparing data        //
  {                                                                //                 around                  //
    if(storedHeading < heading) deltaHeading = deltaHeading - 360; //              360 degrees                //
    if(storedHeading > heading) deltaHeading = 360 + deltaHeading; //          ¿how to prevent that?          //
  }                                                                /////////////////////////////////////////////
  Serial.print("Error: ");
  Serial.println(deltaHeading);
  delay (1000);
}

As shown on attached pick, on “case A” all is working great. On “case B” i fine, but when rotation exceeds 360º in CW direction, “deltaHeading” jumps to negative values that follow the rule on 'deltaHeading" pic.
I know the error is on the 3/4 lines commented on code included … but HOW?..
Thanks for your help…

deltaHeading.jpg

graph.jpg

Should this

if(abs(deltaHeading > 180))

be this?

if(abs(deltaHeading) > 180)

You want to take absolute value of deltaHeading (i.e. ignore minus sign) and compare with 180, I think.

answered your other post here

Yes Sir... ( Thanksfully...) I don´t know how to assign a (SOLVED POST) on this forum... (if there is something like that..) I would like to say, that there are plenty of sensors similar to LSM303 but is important to calibrate them before we can use them (metals influences mainly..). So, even assumed they are factory calibrated, it´s funda mental to Calibrate them before use. I read days ago about propper procedures to Calibrate LSM 303 here https://github.com/pololu/lsm303-arduino . Run Calibrate procedure and see the random values at beggining. Start turning sensor in alla possible axis until the readings get quiet. Note those readings and paste them on the setup of your sketches. See lines 17-18 here.. https://github.com/pololu/lsm303-arduino/blob/master/LSM303/examples/Heading/Heading.ino Readings will be more accurate...

Well, you could edit the first post in the thread's title.