Discussion:
Arduino
(too old to reply)
Tomppa
2012-02-11 19:28:16 UTC
Permalink
Ongelma on varmasti yksinkertainen mutta nyybiölle vähän "hankala"

Kuinka saadaan esim. kun "lcd.print(volts);"
tulostaa näytölle 12.34v niin muutettua oikeaoppisesti
pyöristetyksi eli 12.3v

Mikä kaavakiikkareloitsu pitää tehdä.
Tuukka
2012-02-11 20:32:27 UTC
Permalink
Post by Tomppa
Ongelma on varmasti yksinkertainen mutta nyybiölle vähän "hankala"
Kuinka saadaan esim. kun "lcd.print(volts);"
tulostaa näytölle 12.34v niin muutettua oikeaoppisesti
pyöristetyksi eli 12.3v
Mikä kaavakiikkareloitsu pitää tehdä.
Voit käyttää sprintf():iä muotoiluun ja sitten tulostaa merkkijonon,
tai sitten tätä:

//***********************************************************************************
//subroutine to format numbers for display on LCD screen.
//Taken from the Arduino user forum, written by user 'mem'
//http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1207226548/13#13

void lcdPrintDouble( double 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: printDouble( 3.1415, 2); // prints 3.14 (two decimal places)

if(val < 0.0){
lcd.print('-');
val = -val;
}
if (int(val) < 100) {
lcd.print(" "); //pad with a space for values less than 100
lcd.print(int(val));
} else {
lcd.print (int(val)); //prints the int part
}
if( precision > 0) {
lcd.print("."); // print the decimal point
unsigned long frac;
unsigned long mult = 1;
byte padding = precision -1;
while(precision--)
mult *=10;

if(val >= 0)
frac = (val - int(val)) * mult;
else
frac = (int(val)- val ) * mult;
unsigned long frac1 = frac;
while( frac1 /= 10 )
padding--;
while( padding--)
lcd.print("0");
lcd.print(frac,DEC) ;
}
}

Loading...