Arduino Compass HMC5883L code compiler error

33 Views Asked by At

I am trying to write Arduino code to use to Display compass readings to an LCD Display attached to a Arduino Mega 2560 board for one of my coding classes but I am running into a compiler error that I am unable to resolve. This error I think it is being caused by a library error with the Adafruit HMC5883L universal library. But I am unable to resolve it. Any help is appreciated Thanks!

Error: class HMC5883L' has no member named begin

#include <Wire.h>
#include <HMC5883L.h>
#include <LiquidCrystal_I2C.h> // Include the LCD library

HMC5883L compass;
LiquidCrystal_I2C lcd(0x27, 16, 2); // Set the LCD address and dimensions

float previousHeading = 0.0;

void setup()
{
Serial.begin(9600);
lcd.init(); // Initialize the LCD
lcd.backlight(); // Turn on the backlight

Serial.println("Initialize HMC5883L");
while (!compass.begin())
{
  Serial.println("HMC5883L not found, please check the connection!");
  delay(500);
}

compass.setRange(HMC5883L_RANGE_1_3GA);
compass.setMeasurementMode(HMC5883L_CONTINOUS);
compass.setDataRate(HMC5883L_DATARATE_15HZ);
compass.setSamples(HMC5883L_SAMPLES_4);
}

void loop()
{
Vector norm = compass.readNormalize();
float heading = atan2(norm.YAxis, norm.XAxis);
float declinationAngle = (4.0 + (26.0 / 60.0)) / (180 / M_PI);
heading += declinationAngle;

if (heading < 0)
{
  heading += 2 * PI;
}

if (heading > 2 * PI)
{
  heading -= 2 * PI;
}

// Output to Serial Monitor
Serial.print("Heading = ");
Serial.println(heading);

// Output to LCD Display
lcd.clear(); // Clear the LCD screen
lcd.setCursor(0, 0);
lcd.print("Heading: ");

if (heading >= 0 && heading < PI / 8) {
  lcd.print("N");
} else if (heading >= PI / 8 && heading < 3 * PI / 8) {
  lcd.print("NE");
} else if (heading >= 3 * PI / 8 && heading < 5 * PI / 8) {
  lcd.print("E");
} else if (heading >= 5 * PI / 8 && heading < 7 * PI / 8) {
  lcd.print("SE");
} else if (heading >= 7 * PI / 8 && heading < 9 * PI / 8) {
  lcd.print("S");
} else if (heading >= 9 * PI / 8 && heading < 11 * PI / 8) {
  lcd.print("SW");
} else if (heading >= 11 * PI / 8 && heading < 13 * PI / 8) {
  lcd.print("W");
} else if (heading >= 13 * PI / 8 && heading < 15 * PI / 8) {
  lcd.print("NW");
} else {
  lcd.print("N");
}

lcd.setCursor(0, 1); // Move cursor to the second line
lcd.print("Degrees: ");
lcd.print(heading * 180 / M_PI); // Display heading in degrees

delay(480);
}
0

There are 0 best solutions below