I download the latest lcdPrintDouble from the forum and it did not work at all. with some debugging I found that the mult was wierd numbers. mult is supposed to be powers of ten but it was equalling 79345... and other strange things. so i modified the code a little bit and it mostly works but I am still having some trouble with pow() and mult.
#include <LiquidCrystal.h>
// LiquidCrystal display with:
LiquidCrystal lcd(2, 11, 3, 4, 5, 6, 7);
void setup()
{
Serial.begin(9600);
// Print a message to the LCD.
lcd.print("float test!");
delay(2000);
lcd.clear();
}
void loop()
{
lcd.setCursor(0,0) ;
lcdPrintFloat(-1.1 ,3);
lcd.setCursor(0,1) ;
lcdPrintFloat(2.012, 5);
lcd.setCursor(20,0) ;
lcdPrintFloat(-3.1234, 5);
lcd.setCursor(20,1) ;
lcdPrintFloat(2.004, 6);
delay(5000);
}
void lcdPrintFloat( float val, byte precision){
// prints val on a ver 0012 text lcd with number of decimal places determine by precision
// precision is a number from 0 to 6 indicating the desired decimial places
// example: lcdPrintFloat( 3.1415, 2); // prints 3.14 (two decimal places)
long toPrint = val*10000;
long frac;
long mult = 0;
int padding = precision - 1;
Serial.print("\n\n");
Serial.println(toPrint, DEC);
if(val < 0.0) {
lcd.print('-');
val = -val;
}
lcd.print ((long) val); //prints the integral part
if( precision > 0) {
lcd.print("."); // print the decimal point
mult = (long) pow(10,precision);
Serial.print("padding = ");
Serial.println(padding, DEC);
Serial.print("precision = ");
Serial.println(precision, DEC);
Serial.print("mult = ");
Serial.println(mult, DEC);
frac = (val - int(val)) * mult;
Serial.print("frac = ");
Serial.println(frac, DEC);
long frac1 = frac;
while( frac1 /= 10 ) { padding--;}
while( padding--) { lcd.print("0"); }
lcd.print(frac,DEC) ;
}
}
returns
-11000
padding = 2
precision = 3
mult = 999
frac = 99
20120
padding = 4
precision = 5
mult = 99999
frac = 1199
-31234
padding = 4
precision = 5
mult = 99999
frac = 12339
20040
padding = 5
precision = 6
mult = 999999
frac = 3999
the mult is calculated using pow(10,x) so I do not see how I am ending up with 99 or 999.
any help would be thankful