Teensy 2 - sprintf - datatypes for INT?

Status
Not open for further replies.

fré

New member
Hey,

I just recently ordered a teensy 2, after weeks of troubles with a fake one (w/o knowing! Bummer). :p
Now, it's just plain fun!

Yet, it's been over 10 years since I tried programming C in a decent way.
Being new in chips & microcontrollers, so to understand hardware-programming, I decided to try Arduino-teensyduino-interface first.

After blinking leds, I managed to get my teensy talking over Serial & Uart, having conversations with an old windows-based gps.
But, while using sprintf to build some strings, I wonder about performance & datatypes.

<snip>
char *SSTRING;
int Data1=0, Data2=0, Data3=0;

loop {

Data1=999999; // Need to be able to send at least 6 "9"s.
Data2=99999;
Data3=9999;

sprintf(SSTRING,"$SOFT,%u,%i,%u",Data1,Data2,Data3);

Uart.println(SSTRING);

delay(200);
}
</snip>

Not entirely okay, but something like that, you get the point. ;)

Now, it's sending all kind of stuff, but the problem is that "999999" gets turned in some number that doesn't reflect at all what I'm trying to send, turned in something like 19382, or -32000 and a bit... Probably just overflowed?

So, I've got a couple of questions:

1. What am I doing wrong? Which datatype is supported in teensy and what am I supposed to use to print integers like 999999 (6 9s)? int? double? long?
I don't need floatingpoint-possibilities, just positive numbers. And some negatives but those are quite smaller (temperature-like).

2. Am I actually using the correct flags for sprintf (%u, %i) to build my string?

3. Performance-wise, is there a better option than sprintf to build my string? And get it over the Uart?

4. Is it okay to use char* for my send-string or would it be better to just use a fixed char[xxxx]-size-thing? Ending it with "\0"?



Pretty newbie-questions, I know... but after trying some different things with doubles, longs & floats, I seem to be doing something wrong.
Thanks in advance.

Fré
 
Last edited:
Ints on AVR ATmegas like the Teensy 2.0 are only 16-bits. This means that you can only represent -32768 to +32767. You could use long which is 32-bits. However, I believe the sprintf in the library may not be able to handle long data types. If it does you would use sprintf (buffer, "%lu, %ld, %lu"). Whether you use %i or %d is up to you (%i was added to make scanf support multiple bases, and it was added to printf as well).
 
Or as MichaelMeissner also pointed out in another post, there is a siprint() function which--if you don't need floating point--is smaller.

Also, your
char *SSTRING;
doesn't actually allocate any space for the string -- you'll get strange hard-to-detect bugs.
do
char SSTRING[]="123456";

will allocate space for 6 characters (plus the \0 at the end)
 
Okay,

Seems like I managed to fix some things... So, thank you for that.
Float & Long's get parsed totally absurdly with sprintf, so I tried using other types.
When defining my vars with "int32_t" instead of "long", I can print the numbers I need, in combination with the right "%lu"'s.

I'm just not too sure it's the right solution for my problem, but hey, it works. (Supposing Arduino does handle float & long the way it should for other functions & libraries.)

Happily working at my next issues... :)
 
Last edited:
Status
Not open for further replies.
Back
Top