Grove 3 Axis Compass Issue

We are using the 3-axis compass with the sample code that came with the library we installed. But when we set the guass to 1.3 using the code that they have, and it gives an error. Then I uploaded the sample program itself, and it errors as well. Here is the relevant snippet of code:

// 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;

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

  Serial.println("Starting the I2C interface.");
  Wire.begin(); // Start the I2C interface.

  Serial.println("Constructing new HMC5883L");
    
  Serial.println("Setting scale to +/- 1.3 Ga");
  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));
  
  Serial.println("Setting measurement mode to continous.");
  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));
}

We also tried entering the other valid values (0.88, 5.6, 8.1, etc.) and none of them are accepted. Here is the serial data which contains the error:

Setting scale to +/- 1.3 Ga
Entered scale was not valid, valid gauss values are: 0.88, 1.3, 1.9, 2.5, 4.0, 4.7, 5.6, 8.1
Setting measurement mode to continous.
Entered scale was not valid, valid gauss values are: 0.88, 1.3, 1.9, 2.5, 4.0, 4.7, 5.6, 8.1

It may be the library code has a problem. Here is the setScale code from HMC5883L.cpp. I always specify a return a value, and this does not specify a return value, so I added one at the bottom. Give that a try.

int HMC5883L::setScale(float gauss)
{
	uint8_t regValue = 0x00;
	if(gauss == 0.88)
	{
		regValue = 0x00;
		m_Scale = 0.73;
	}
	else if(gauss == 1.3)
	{
		regValue = 0x01;
		m_Scale = 0.92;
	}
	else if(gauss == 1.9)
	{
		regValue = 0x02;
		m_Scale = 1.22;
	}
	else if(gauss == 2.5)
	{
		regValue = 0x03;
		m_Scale = 1.52;
	}
	else if(gauss == 4.0)
	{
		regValue = 0x04;
		m_Scale = 2.27;
	}
	else if(gauss == 4.7)
	{
		regValue = 0x05;
		m_Scale = 2.56;
	}
	else if(gauss == 5.6)
	{
		regValue = 0x06;
		m_Scale = 3.03;
	}
	else if(gauss == 8.1)
	{
		regValue = 0x07;
		m_Scale = 4.35;
	}
	else
	{
        	return ERRORCODE_1_NUM;
	}

	// Setting is in the top 3 bits of the register.
	regValue = regValue << 5;
	write(CONFIGURATION_REGISTERB, regValue);

        // return 0 added by SurferTim
       return 0;
}

edit: I also put the last else in brackets, just in case. If that doesn’t work, try a type override in the function call.

 error = compass.setScale((float)1.3); // Set the scale of the compass.

The library DEFINITELY has a problem. Expecting exact matches on float values is just plain stupid. It needs a serious rewrite.

The librAry is from Sparkfun. The Adafruit library passes the scale as an integer define.