Arduino Lcd with compass

I am doing a Compass (HMC5883L) to Lcd were the compass shows the bearings on the lcd but the last number displayed stays there eg. if the heading is 355 and then pointed north the lcd reads 055 instead of 0. So is there any way to stop it from doing that.

Heres my code

#include <LiquidCrystal.h>

// Reference the I2C Library
#include <Wire.h>
// Reference the HMC5883L Compass Library
#include <HMC5883L.h>

// Store our compass as a variable.
HMC5883L compass;
// Record any errors that may occur in the compass.
int error = 0;
LiquidCrystal lcd(8,9,4,5,6,7);
//rounding angle
int RoundDegreeInt;

int PreviousDegree = 0;
int heading = 0;

// Out setup routine, here we will configure the microcontroller and compass.
void setup()
{
  // Initialize the serial port.
  
  lcd.begin(16, 2);

  Wire.begin(); // Start the I2C interface.

  compass = HMC5883L(); // Construct a new HMC5883 compass.
  
  error = compass.SetScale(1.3); // Set the scale of the compass.
  if(error != 0) // If there is an error, print it out.
    Serial.println(compass.GetErrorText(error));

  error = compass.SetMeasurementMode(Measurement_Continuous); // Set the measurement mode to Continuous
  if(error != 0) // If there is an error, print it out.
    Serial.println(compass.GetErrorText(error));
}

// Our main program loop.
void loop()
{
  // Retrive the raw values from the compass (not scaled).
  MagnetometerRaw raw = compass.ReadRawAxis();
  // Retrived the scaled values from the compass (scaled to the configured scale).
  MagnetometerScaled scaled = compass.ReadScaledAxis();

  // Values are accessed like so:
  int MilliGauss_OnThe_XAxis = scaled.XAxis;// (or YAxis, or ZAxis)

  // Calculate heading when the magnetometer is level, then correct for signs of axis.
  float heading = atan2(scaled.YAxis, scaled.XAxis);

  // Once you have your heading, you must then add your 'Declination Angle', which is the 'Error' of the magnetic field in your location.
  // Find yours here: http://www.magnetic-declination.com/
  // Mine is: 2? 37' W, which is 2.617 Degrees, or (which we need) 0.0456752665 radians, I will use 0.0457
  // If you cannot find your Declination, comment out these two lines, your compass will be slightly off.
  float declinationAngle = 0.009 ;
  heading += declinationAngle;

  // Correct for when signs are reversed.
  if(heading < 0)
    heading += 2*PI;
  
  // Check for wrap due to addition of declination.
  if(heading > 2*PI)
    heading -= 2*PI;
  
  // Convert radians to degrees for readability.
  float headingDegrees = heading * 180/M_PI;

  //correcting the angle issue
  if (headingDegrees >= 1 && headingDegrees < 240) 
  {
    headingDegrees = map(headingDegrees,0,239,0,179);
  }
  else if (headingDegrees >= 240)
  {
    headingDegrees =  map(headingDegrees,240,360,180,360);
  }

  //rounding the angle
  RoundDegreeInt =round(headingDegrees);

  //smoothing value
  if( RoundDegreeInt < (PreviousDegree + 3) && RoundDegreeInt > (PreviousDegree - 3) ) {
    RoundDegreeInt = PreviousDegree;
  }

if (RoundDegreeInt >= 0 && RoundDegreeInt <= 44)
{
 
  heading = lcd.print("N");

} 
else if (RoundDegreeInt >= 45 && RoundDegreeInt <= 89)
{

  heading = lcd.print("NE");

}
else if (RoundDegreeInt >= 90 && RoundDegreeInt <= 134)
{


  heading = lcd.print("E");

}
else if (RoundDegreeInt >= 135 && RoundDegreeInt <= 179)
{

  heading = lcd.print("SE");

}  
else if (RoundDegreeInt >= 180 && RoundDegreeInt <= 224)
{

  heading = lcd.print("S");

}    
else if (RoundDegreeInt >= 225 && RoundDegreeInt <= 269)
{

  heading = lcd.print("SW");

}    
else if (RoundDegreeInt >= 270 && RoundDegreeInt <= 314)
{
  heading = lcd.print("W");

}        
else if (RoundDegreeInt >= 315 && RoundDegreeInt <= 360)
{


  heading = lcd.print("NW");

}    
  

  
  Output(RoundDegreeInt);

  PreviousDegree = RoundDegreeInt;


}

// Output the data down the serial port.
void Output(int RoundDegreeInt)
{
   //Serial.println();
   lcd.setCursor(0,0);
   lcd.print(RoundDegreeInt);
   
   lcd.setCursor(4,0);
   lcd.print(heading);



}

Overwrite the field with blanks before writing the new value:

   lcd.setCursor(0,0);
   lcd.print("   ");
   lcd.setCursor(0,0);
   lcd.print(RoundDegreeInt);

Thank you very much :slight_smile:

johnwasser:
Overwrite the field with blanks before writing the new value:

   lcd.setCursor(0,0);

lcd.print("   ");
   lcd.setCursor(0,0);
   lcd.print(RoundDegreeInt);