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
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);
}
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.
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.
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
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.
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.
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.
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.
thank for your answer but its not working (:
Nelsy
Go to the shops. Buy a pencil and some cigarettes.
Discard the cigarettes (because they are bad for you)
Draw some example values on the cigarette packet with your pencil.
If you do not understand, make a cup of tea. I find that tea always helps.
Let's say that you have reserved a big 21 char buffer for your dstrtof() result.
###########.####### i.e. 12 digits + dot + 7 decimals = 20 width
12345678901.2345678
901.2345678 print(buf + 20 - 3 - 1 - 7) to start at the 9th char in buf.
1234567890123456.78
456.78 print(buf + 20 - 3 - 1 - 2) to start at the 14th char in buf.
David.