Pages: 1 [2]   Go Down
Author Topic: Odd sprintf behaviour  (Read 1495 times)
0 Members and 1 Guest are viewing this topic.
Worst state in America
Offline Offline
God Member
*****
Karma: 32
Posts: 819
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

He used Serial.println() to print both. That adds cr/lf.

Yeah, that works, but personally I prefer to use only Serial.print(), then put the "\r\n" in the string if I want a new line. It's easier for me to keep it all straight in my mind.

(actually, I create a "stdin" and "stdout" pointer to the serial device, then use "printf()", but that's a whole 'nuther story!) smiley
Logged

Gentlemen may prefer Blondes, but Real Men prefer Redheads!

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 99
Posts: 4835
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Floating Point is slow on non-FPU chips. Why use it if you don't really need it?
And in general, you don't if you can handle fixed-point integers and understand what units are.

Quote
Suppose cTemp is 12.07. What gets put in currTemp?

will this do?
Code:
sprintf(currTemp, "Current Temp: %0d.%2d C", (int)cTemp, cTemp1);

and yeah, I was waiting and sure he'd spot it. Guess I owe someone a buck.
Logged

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

Worst state in America
Offline Offline
God Member
*****
Karma: 32
Posts: 819
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Ultimately, quite possible.  Right now I'm hovering around 29-30K

Well, if you are running out of memory then of course using an extra 1.5K would be a bad thing for sure.

My "slightly sarcastic" comment was really directed to those who write sketches that end up being 9K, then they complain about the extra 1.5K of memory that the floating point code uses and instead use tons of "Serial.print()" calls in series to build up one line that could have been much more easily done with a simply "sprintf()".

The Arduino IDE and programming "language" is certainly wonderful for rank beginners, but it doesn't take long for a user to outgrow the hand-holding and wanting to start writing code the "right way".
Logged

Gentlemen may prefer Blondes, but Real Men prefer Redheads!

Worst state in America
Offline Offline
God Member
*****
Karma: 32
Posts: 819
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Floating Point is slow on non-FPU chips. Why use it if you don't really need it?
And in general, you don't if you can handle fixed-point integers and understand what units are.

If I want to print something like "Temperature: 20.4 C" using "%4.2f", do I REALLY care about the few extra microseconds the code takes to execute?
Logged

Gentlemen may prefer Blondes, but Real Men prefer Redheads!

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 310
Posts: 26620
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
will this do?
Code:
sprintf(currTemp, "Current Temp: %0d.%2d C", (int)cTemp, cTemp1);
No.  smiley-wink
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Colorado
Offline Offline
Edison Member
*
Karma: 47
Posts: 1562
Reviving dead brain cells with Arduinos.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

My "slightly sarcastic" comment was really directed to those who write sketches that end up being 9K, then they complain about the extra 1.5K of memory that the floating point code uses and instead use tons of "Serial.print()" calls in series to build up one line that could have been much more easily done with a simply "sprintf()".

The Arduino IDE and programming "language" is certainly wonderful for rank beginners, but it doesn't take long for a user to outgrow the hand-holding and wanting to start writing code the "right way".

I'm already blowing away the bootloader to reclaim the 2K it takes. smiley  But yes, eventually I will rewrite the whole thing.

will this do?
Code:
sprintf(currTemp, "Current Temp: %0d.%2d C", (int)cTemp, cTemp1);

Based on his example, no that won't.  It will translate 12.07 into 12 and 7, as opposed to 12 and 07.  The end result will be 12.7C instead of 12.07C.  And I'm well aware of that, however, as I pointed out, I know what the incoming temperature reading looks like, it will never have a .0x ...

Now, that doesn't necessarily fix the next conversion, which is computing the F value.  If that ends with a .0x, I could have issues.
Logged

Worst state in America
Offline Offline
God Member
*****
Karma: 32
Posts: 819
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm already blowing away the bootloader to reclaim the 2K it takes. smiley  But yes, eventually I will rewrite the whole thing.

You said you were nearing 30K code size which to me implied an Arduino UNO or similar 32K board.

The UNO has a 1/2K (512 byte) bootloader. If you gained 2K by doing away with the bootloader, what board do you have? It sounds like a MEGA2560 (if so, then you have 256K of flash, not 32).

I'm confused.
Logged

Gentlemen may prefer Blondes, but Real Men prefer Redheads!

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 99
Posts: 4835
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It's been a long damned time since I used sprintf regularly and I know there is a format string to put the leading zero in but I guess I'm as lazy as anyone else that didn't look it up. Difference being, none of my code is waiting for it!
Logged

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

Colorado
Offline Offline
Edison Member
*
Karma: 47
Posts: 1562
Reviving dead brain cells with Arduinos.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You said you were nearing 30K code size which to me implied an Arduino UNO or similar 32K board.

The UNO has a 1/2K (512 byte) bootloader. If you gained 2K by doing away with the bootloader, what board do you have? It sounds like a MEGA2560 (if so, then you have 256K of flash, not 32).

Yeah sorry, no.  I'm still used to the old bootloaders taking up 2K worth of space.  You're right, it's just that 512 space.  I'm not using any specific board, I'm designing my own based on the 328p.

Either way, the end result of all of this is that a) for now, using the method I choose works for the time being, and b) once I rewrite it all and optimize it, I may very well have plenty of room to add floating point support (and watch 1.5K magically fly away.)
Logged

UK
Offline Offline
Faraday Member
**
Karma: 100
Posts: 4153
Where is your SSCCE?!?!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


Can anyone explain to me why it appears that the 'currTime' variable gets tacked onto the end of the 'currDate' one?  If I just output 'currDate', I get:
Code:
Current Date: 2013/01/13Current Time: 23:32:27

Why's that?

You need to have cr/lf sequences in your sprintf format strings. Example:

Code:
sprintf(buffer, "Hello, my name is %s", "Roger");
Serial.print(buffer);
sprintf(buffer, "I am %d years old", 56);
Serial.print(buffer);

...produces:

Hello, my name is RogerI am 56 years old

However, this:

Code:
sprintf(buffer, "Hello, my name is %s\r\n", "Roger");
Serial.print(buffer);
sprintf(buffer, "I am %d years old\r\n", 56);
Serial.print(buffer);

...produces:

Hello, my name is Roger
I am 56 years old


The "\r" is a "return" (carriage return - 0x0D or 13 decimal) and "\n" is a "newline" (moves to the next line - 0x0A or 10 decimal).

Nope, about 23 foot wide of the mark.

See my answer just before yours:

Quote
The first sprintf is placing the data in the first array fine, but the terminating null character is ending up as the first character of the second array.  The second sprintf is then overwriting that null character.
Logged

Get 10% off all 4D Systems TFT screens this month: use discount code MAJENKO10

Pages: 1 [2]   Go Up
Jump to: