I have a HMC5883L compass, wired and working on a Uno. (with sketch for HMC5883L loaded) I also have a Sainsmart LCD display wired and working. (with sketch for LCD loaded)
How would I go about getting the LCD to display the values I can see on the serial monitor?
This is what I have so far...
#include <LiquidCrystal.h>
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
#include <Wire.h>
#include <HMC5883L.h>
HMC5883L compass;
void setup(){
Serial.begin(9600);
Wire.begin();
compass = HMC5883L(); //new instance of HMC5883L library
setupHMC5883L(); //setup the HMC5883L
lcd.begin(16, 2);
lcd.setCursor(0,0);
lcd.print("COMPASS ver 1");
lcd.setCursor(0,1);
lcd.print("Heading=");
}
void loop()
{
lcd.setCursor(1,10); // move cursor to second line "1" and 10 spaces over
{
float heading = getHeading();
Serial.println(heading);
delay(1000); //only here to slow down the serial print
}
}
void setupHMC5883L(){
int error;
error = compass.SetScale(1.3);
if(error != 0) Serial.println(compass.GetErrorText(error));
error = compass.SetMeasurementMode(Measurement_Continuous);
if(error != 0) Serial.println(compass.GetErrorText(error));
}
float getHeading(){
MagnetometerScaled scaled = compass.ReadScaledAxis();
float heading = atan2(scaled.YAxis, scaled.XAxis);
if(heading < 0) heading += 2*PI;
if(heading > 2*PI) heading -= 2*PI;
return heading * RAD_TO_DEG;
}
It displays the COMPASS ver 1 on 1st line and
Heading= on the second line.
Thanks!
I am surprised by your question.
The program already outputs to the LCD and you position the cursor ready to output the heading
lcd.setCursor(1, 10); // move cursor to second line "1" and 10 spaces overbut you never actually output heading to the LCD.
What is the problem ?
It will not display the compass heading on the LCD. Compass is connected to Uno's A4 and A5 pins.
lcd.setCursor(1, 10); // move cursor to second line "1" and 10 spaces overWrong. It is trying to set the cursor to column 1 of line 10.
Oh yeah, that would never display on a two line LCD!
Fixed it, but I'm still having trouble finding code that would print the Magnetic heading to the LCD. Did find a sketch another guy is having trouble with... but I can't identify what code is used to display the heading, and his code does not work either, and I did change the LCD pins to my values, not 8,9,4,5,6,7.
#include <Wire.h>
#include <HMC5883L.h>
HMC5883L compass;
int error = 0;
#include <LiquidCrystal.h>
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
void setup()
{
lcd.begin(16, 2);
Serial.begin(9600);
Serial.println("Starting the I2C interface.");
Wire.begin();
Serial.println("Constructing new HMC5883L");
compass = HMC5883L();
Serial.println("Setting scale to +/- 1.3 Ga");
error = compass.SetScale(1.3);
if(error != 0)
Serial.println(compass.GetErrorText(error));
Serial.println("Setting measurement mode to continous.");
error = compass.SetMeasurementMode(Measurement_Continuous);
if(error != 0)
Serial.println(compass.GetErrorText(error));
}
void loop()
{
MagnetometerRaw raw = compass.ReadRawAxis();
MagnetometerScaled scaled = compass.ReadScaledAxis();
int MilliGauss_OnThe_XAxis = scaled.XAxis;
float heading = atan2(scaled.YAxis, scaled.XAxis);
float declinationAngle = 0.0457;
heading += declinationAngle;
if(heading < 0)
heading += 2*PI;
if(heading > 2*PI)
heading -= 2*PI;
float headingDegrees = heading * 180/M_PI;
Output(headingDegrees);
}
void Output(float headingDegrees)
{
Serial.print(headingDegrees);
Serial.println(" Degrees \t");
delay(900);
lcd.write(Serial.read());
}
void Output(float headingDegrees)
{
Serial.print(headingDegrees);
Serial.println(" Degrees \t");
delay(900);
lcd.write(Serial.read());
}
Does this successfully print the headingDegrees on the serial monitor?
If so, just change the lcd.write(Serial.read()) to the correct lcd.setCursor and then lcd.print(headingDegrees).