I trying to get back into programming with Arduino. My last programming was in 8080/Z80 assembler, with some 8088/Z8000 assembler. Then I changed jobs. I am totally confused as to what is the difference between "char buf" and "const char" in Arduino, and how to convert one to the other. What I'm trying to do is, I have a float with value 0.13. I convert it into char *buf with a function given to me:
void fmtDouble(double val, byte precision, char *buf, unsigned bufLen = 0xffff);
with call fmtDouble(CPHval, 0, CPSval, sizeof(CPSval));
Serial.print gives me 0.13 for CPSval. I pass CPSval to the u8glib LCD handler which wants:
u8g_uint_t, u8g_uint_t, const char*
with call u8g.drawStr( 0, 15, CPSval);
It gives no error but only displays 0 on the LCD. If CPSval is 12.05 the LCD displays 2.
Jim
A "const" is a constant. So if a function/method receives a parameter and says "const char*" it says I need a pointer to a character (usually an array of characters) and I promise I will not change that characters.
So "u8g_uint_t, u8g_uint_t, const char*" while u8g_uint_t is not a standard type I appears to be an unsigned integer. I'm guessing x/y coordinates and the text to be written there.
I you can print the the serial port the character string CPSval (i.e. Serial.print(CPSval)
But it won't work with LCD library, you might look at your library or LCD connections. I have never used the LCD library you are using so I can not say what the problem is. I know that some LCD libraries require you to set a font before you attempt to write text. Since you did not include very much code, I can only guess.
Post more code, snippets don't prove much in this case.
What we are attempting to do is measure the flow of a gas in a long tube by using the pressures at both ends to calculate. There is a valve at the start to control the flow and we need a readout right there. This is one of several parameters we are measuring. Including the whole sketch was much too large for the forum. I have reduced things to the bare minimum to show what I have:
#include "U8glib.h"
int inPin = 1;
float fVal;
char sVal[5];
U8GLIB_ST7920_128X64 u8g(4, 2, 3, U8G_PIN_NONE); // SPI Com: SCK = en = 4, MOSI = rw = \2, CS = di = 3
//function to make float a string
void fmtDouble(double val, byte precision, char *buf, unsigned bufLen = 0xffff);
unsigned fmtUnsigned(unsigned long val, char *buf, unsigned bufLen = 0xffff, byte width = 0);
void setup(void) {
Serial.begin(9600);
// flip screen, if required
// u8g.setRot180();
// assign default color value
if ( u8g.getMode() == U8G_MODE_R3G3B2 )
u8g.setColorIndex(255); // white
else if ( u8g.getMode() == U8G_MODE_GRAY2BIT )
u8g.setColorIndex(1); // max intensity
else if ( u8g.getMode() == U8G_MODE_BW )
u8g.setColorIndex(1); // pixel on
}
void loop(void) {
fVal = analogRead(inPin);// read the input pin
fVal = (fVal/1024)*5;//convert to voltage
fVal =((fVal/5)-.04)/.09;
fVal = fVal/.249;
fmtDouble(fVal, 0, sVal, sizeof(sVal));
u8g.firstPage();
do {
draw();
} while( u8g.nextPage() );
}
void draw(void) {
// graphic commands to redraw the complete screen should be placed here
u8g.setFont(u8g_font_unifont);
//u8g.setFont(u8g_font_osb21);
u8g.drawStr( 0, 15, sVal);
}
//
// Produce a formatted string in a buffer corresponding to the value provided.
// If the 'width' parameter is non-zero, the value will be padded with leading
// zeroes to achieve the specified width. The number of characters added to
// the buffer (not including the null termination) is returned.
//
unsigned
fmtUnsigned(unsigned long val, char *buf, unsigned bufLen, byte width)
{
if (!buf || !bufLen)
return(0);
// produce the digit string (backwards in the digit buffer)
char dbuf[10];
unsigned idx = 0;
while (idx < sizeof(dbuf))
{
dbuf[idx++] = (val % 10) + '0';
if ((val /= 10) == 0)
break;
}
// copy the optional leading zeroes and digits to the target buffer
unsigned len = 0;
byte padding = (width > idx) ? width - idx : 0;
char c = '0';
while ((--bufLen > 0) && (idx || padding))
{
if (padding)
padding--;
else
c = dbuf[--idx];
*buf++ = c;
len++;
}
// add the null termination
*buf = '\0';
return(len);
}
//
// Format a floating point value with number of decimal places.
// The 'precision' parameter is a number from 0 to 6 indicating the desired decimal places.
// The 'buf' parameter points to a buffer to receive the formatted string. This must be
// sufficiently large to contain the resulting string. The buffer's length may be
// optionally specified. If it is given, the maximum length of the generated string
// will be one less than the specified value.
//
// example: fmtDouble(3.1415, 2, buf); // produces 3.14 (two decimal places)
//
void
fmtDouble(double val, byte precision, char *buf, unsigned bufLen)
{
if (!buf || !bufLen)
return;
// limit the precision to the maximum allowed value
const byte maxPrecision = 6;
if (precision > maxPrecision)
precision = maxPrecision;
if (--bufLen > 0)
{
// check for a negative value
if (val < 0.0)
{
val = -val;
*buf = '-';
bufLen--;
}
// compute the rounding factor and fractional multiplier
double roundingFactor = 0.5;
unsigned long mult = 1;
for (byte i = 0; i < precision; i++)
{
roundingFactor /= 10.0;
mult *= 10;
}
if (bufLen > 0)
{
// apply the rounding factor
val += roundingFactor;
// add the integral portion to the buffer
unsigned len = fmtUnsigned((unsigned long)val, buf, bufLen);
buf += len;
bufLen -= len;
}
// handle the fractional portion
if ((precision > 0) && (bufLen > 0))
{
*buf++ = '.';
if (--bufLen > 0)
buf += fmtUnsigned((unsigned long)((val - (unsigned long)val) * mult), buf, bufLen, precision);
}
}
// null-terminate the string
*buf = '\0';
}
This compiles with no errors. The problem is in the transfer of sVal from the output of fmtDouble to the input of u8g.drawStr. The results I'm getting are in my original post.
Jim
Jim,
Please edit your post removing the "quote" tags around your code and replace them with "code" tags in the same manner.
Creds for at least trying ... 
-Enjoy
fh : )_~
If you do that, you'll also have to remove the colour formatting tags.
Or just cut and paste the original code between code tags.
So basically this is a function to print floats?
First problem:
char sVal[5];
That's too small to hold 12.05. You need an extra byte to hold the terminating 0x00 byte.
void fmtDouble(double val, byte precision, char *buf, unsigned bufLen = 0xffff);
Why have a default buffer length of 65535 when the code tests for if the buffer is zero or not? Wouldn't a default of zero be better? Or no default?
Judging by their web site u8glib is derived from the Print class (just checked, yes it is). So you can use the Streaming library to simplify things.
http://arduiniana.org/libraries/streaming/
So if you add this to your file:
#include <Streaming.h>
Then to print fVal to 2 decimal places (the default) all you have to do is:
u8g << fVal;
And your entire sketch simplifies down to:
#include "U8glib.h"
#include "Streaming.h"
int inPin = 1;
float fVal;
U8GLIB_ST7920_128X64 u8g(4, 2, 3, U8G_PIN_NONE); // SPI Com: SCK = en = 4, MOSI = rw = \2, CS = di = 3
void setup(void) {
Serial.begin(9600);
// assign default color value
if ( u8g.getMode() == U8G_MODE_R3G3B2 )
u8g.setColorIndex(255); // white
else if ( u8g.getMode() == U8G_MODE_GRAY2BIT )
u8g.setColorIndex(1); // max intensity
else if ( u8g.getMode() == U8G_MODE_BW )
u8g.setColorIndex(1); // pixel on
}
void loop(void) {
fVal = analogRead(inPin);// read the input pin
fVal = (fVal/1024)*5;//convert to voltage
fVal =((fVal/5)-.04)/.09;
fVal = fVal/.249;
u8g.firstPage();
do {
draw();
} while( u8g.nextPage() );
}
void draw(void) {
// graphic commands to redraw the complete screen should be placed here
u8g.setFont(u8g_font_unifont);
u8g << fVal;
}
Nick asks:
]
Why have a default buffer length of 65535 when the code tests for if the buffer is zero or not? Wouldn't a default of zero be better? Or no default?That's too small to hold 12.05. You need an extra byte to hold the terminating 0x00 byte.
void fmtDouble(double val, byte precision, char *buf, unsigned bufLen = 0xffff);
fmtDouble(fVal, 0, sVal, sizeof(sVal));
I thought sizeof would limit buffer and 0 set max number of digits to 2, the largest number is 25.
Then Nick Gives me a great answer. Thanks Nick!! I don't know how you do this, answering the same dummy questions every day. I looked at Google Code Archive - Long-term storage for Google Code Project Hosting.. The documentation for print said:
Arguments:
Returns:
verrry informative!
Again THANKS
Jim
alfiesty:
Nick asks:
Then Nick Gives me a great answer. Thanks Nick!! I don't know how you do this, answering the same dummy questions every day.
It's amazing what folks will tolerate for passion ... Mr. Gammon has passion for helping folks! A true teacher.
-Enjoy
fh : )_~
Arguments:
Returns:
verrry informative!
ok, got the point, but at least I have refered to Serial.print. 
And yes: Thanks to Nick!
Seems, that you became also a specialist for u8glib. Thanks for answering.
Oliver