As part of a larger project I need to get a compass system working so I've made a stand alone system using an Arduino Mega, a LSM303DLHC compass and a Nokia 5110 display, it's also battery powered. Everything is working fine with one small thing I would like to have, a fixed decimal place on the readout of the heading.
With a serial print statement you can define the number of decimal places like this "Serial.println(headingct, 2);"
I've tried the same format with writing to the LCD, "lcd.print(headingct, 2);" That doesn't work. Actually by changing that number it defines the total of digits in the displayed value but all with a floating decimal point. Obviously this is a function of the PCD8544.h library that I am using. I've looked through the library documentation, but I'm not very strong on how and what a library can do.
I've seen examples where people have divided the floating point number into 2 parts, the whole number and the fractional, then put them together as a string to be displayed. As with any programming problem there are always several ways to do the same thing. I'm looking for suggestions as to different and the better way to do this. As I see it I have a couple of options.
- See if there is a way to do this with the PCD8544 library
- Use a different library that has that function
- Divide the number into 2 parts and make a string
- Live with it as is. Other than my OCD to have it that way it's not a big deal, other things to work on
Below is my code, it is dived into 2 tabs, the main loop and the functions to read the LSM303 and tilt compensate the reading.
I could not include all of the code as it exceeded the max number of characters so I've just included the main loop.
Thanks in advance for all suggestions and comments
John
Main Loop
// Nokia demo from Raplh Bacon
// Added LSM303 Compass
#include "Arduino.h"
#include <PCD8544.h>
#include "Wire.h"
#include <Adafruit_Sensor.h>
#include <Adafruit_LSM303.h>
// Create the LCD object (using a library class)
static PCD8544 lcd;
// LSM303 variables
Adafruit_LSM303 lsm;
float Mx, My, Mz; // Raw Magnetometer measurements
float Mxc, Myc, Mzc; // Calibrated Magnetometer measurements
float Mxcn, Mycn, Mzcn; // Normalized Magnetometer measurements
float Ax, Ay, Az; // Raw Accelerometer measurments
float Axc, Ayc, Azc; // Calibrated Accelerometer mesurements
float Axcn, Aycn, Azcn; // Normalized Accelerometer measurements
float norm_a, norm_m;
double pitch, roll;
double pitch_print, roll_print;
double tiltcnf;
double Xh, Yh; // Tilt compensated values for heading calculation
float headingct; // Calibrated and tilt compensated heading
//calibrated_values[3] is the global array where the calibrated data will be placed
//calibrated_values[3]: [0]=Xc, [1]=Yc, [2]=Zc
float calibrated_mag_values[3];
float calibrated_acc_values[3];
float alpha = 0.25; // Low-Pass Filtercoefficient
// Filtered Magnetometer measurements
float Mxcnf = 0;
float Mycnf = 0;
float Mzcnf = 0;
// Filtered Accelerometer measurements
float Axcnf = 0;
float Aycnf = 0;
float Azcnf = 0;
// =============================================================================
// SETUP SETUP SETUP SETUP SETUP SETUP SETUP SETUP
// =============================================================================
void setup() {
Serial.begin(9600);
Wire.begin();
lsm.begin();
// Nokia 5110 has 84W x 48H pixels
lcd.begin(84, 48);
lcd.clear();
Serial.println("Setup Completed");
}
// =============================================================================
// LOOP LOOP LOOP LOOP LOOP LOOP LOOP LOOP LOOP
// =============================================================================
void loop() {
// call compass sketch
compass();
// Nokia Display***************************************
// Position is COL, ROW
lcd.setCursor(0, 0);
// For PRINT statement it works out the actual pixel start point
lcd.print("Heading");
// set the LCD contrast...
short level = 63; //contrast level = 0 to 127);
lcd.setContrast(level);
// Write out the heading
lcd.setCursor(0, 2);
lcd.print(headingct, 2); // DEC
} // end void loop