Go Down

Topic: How to choose and print on the lcd the number of digit BEVORE the decimal piont (Read 34 times) previous topic - next topic

Bosch_et_Nelsy

Quote
Hi guy, I'am just beginning to work with arduino. Can you please help me with my code? I attach a copy below. So i want to print on a lcd a double(floating) value. My problem is, the user should choose the number bevore and after the decimal point. for example the value = 1234.5678. the user give "lcd_print_string(volts, lab[0], unity[0], 2, 2, 0, 0);". It should be print " 34.56 " on the display. Can anyone help me?
kind regards
Code: [Select]

int val = 0;
double volts    = 100.0233;
char *lab[2]    = {"U= ", "T= "};
char *unity[2] = {"V ", "C "};
void setup() {
 
  Serial.begin(9600);   // opens serial port, sets data rate to 9600 bps
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);

}
void loop()
{
   val=analogRead(A0);           
   volts=(val/1024.0)*5000.0; //this is an example
 
   lcd_print_string(volts, lab[0], unity[0], 5, 2, 0, 0);
 
   delay(500);
}
// *P_str = val; ===> P_Str = &val;

void lcd_print_string(double value, char label[], char unit[], unsigned int dec_number, unsigned int dec_places, unsigned int row, unsigned int column)
{
char Out_str[10];


lcd.setCursor(column, row);
/*unsigned int width is the minimun width,unsigned int dec_places is precision, value is copied into Out_str

unsigned int width = dec_number + dec_places;
 
dtostrf(value, width, dec_places, Out_str);
lcd.clear();

lcd.print(label);
lcd.print(Out_str);
lcd.print(" ");
lcd.print(unit);
}

david_prentice

Think about it.   The width = before + dot + after

If you want the punter to choose the before and the after, you just have to do the adding up.
I would expect you to use 2 decimal places e.g. for currency.
If you want the user to specify the dollar field,  you just use width = dollar_width + 3.

Invest in a pencil.

David.

Bosch_et_Nelsy

Think about it.   The width = before + dot + after

If you want the punter to choose the before and the after, you just have to do the adding up.
I would expect you to use 2 decimal places e.g. for currency.
If you want the user to specify the dollar field,  you just use width = dollar_width + 3.

Invest in a pencil.

David.
thanks David
 width = before + dot + after it's true
but the user should specify the number before and after the decimal point so this is an example:value = 1234.5678. lcd_print_string(volts, lab[0], unity[0], 3,2, 0, 0);". It should be print "234.56 " on the display

david_prentice

Ah-ha.   You want to ignore thousands of dollars.  Just deal in the small change.

I suggest that you use dstrof() to format the biggest possible value.   e.g. millions of dollars into a buffer.
Then you can left_print or right_print the buffer in whichever way you like.

dstrtof() and printf() tend to look at their format specifier.   If you exceed the space,  they still print the value.
Code: [Select]

e.g. sprintf(buf, "%7.2f") will give you "1234.57"
e.g. sprintf(buf, "%6.2f") will give you "1234.57"
e.g. sprintf(buf, "%10.2f") will give you "   1234.57"
subsequently, you use printf("%-6.6s", buf) to output the "234.57"

Untested.   You can try this on your PC.   Note that Arduino for AVR does not work with printf() and floats.   Hence the dstrtof() function.

David.

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy