Hey,
I just recently ordered a teensy 2, after weeks of troubles with a broken copy (?).
Now, it's just plain fun!
It's been over 10 years since I tried programming C in a decent way. Being new in chips & microcontrollers, 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 the way I dit it, but something like that, you get the point. It prints (something) in real life.
Now, it's sending all kind of stuff, but the problem is that "999999" and "99999" get 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 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é
Teensy 2 - sprintf - datatype for numbers?
Moderators: adafruit_support_bill, adafruit
Please be positive and constructive with your questions and comments.
- arctic_eddie
- Posts: 233
- Joined: Tue Feb 28, 2012 6:01 pm
Re: Teensy 2 - sprintf - datatype for numbers?
Your data1 and data2 exceed the range of an integer. Use long as the data type.
The sprintf function does not work for float type in the Arduino IDE. You have to do it piece meal by scaling up to a larger long, print the whole part as long, add a decimal point, then print the fractional part as another int/long.
The sprintf function does not work for float type in the Arduino IDE. You have to do it piece meal by scaling up to a larger long, print the whole part as long, add a decimal point, then print the fractional part as another int/long.
- westfw
- Posts: 2008
- Joined: Fri Apr 27, 2007 1:01 pm
Re: Teensy 2 - sprintf - datatype for numbers?
Code: Select all
char *SSTRING;
:
sprintf(SSTRING,"$SOFT,%u,%i,%u",Data1,Data2,Data3 )
%i is correct for a signed integer (-32768 to 32767)2. Am I actually using the correct flags for sprintf (%u, %i) to build my string?
You'll need a "long" for bigger numbers (up to about 2billion), in which case the correct formats would be %l or %ul.
Performance wise, sprintf should be OK.3. Performance-wise, is there a better option than sprintf to build my string? And get it over the Uart?
In fact, your current usage is WRONG. You create the pointer, but don't reserve any storage for the actual characters in the string. When you write via the pointer, you'll be overwriting random parts of memory. You would have to add SSTRING = malloc(size); somwhere.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"?
It is more common to use a static buffer (char SSTRING[20]; or similar.) sprintf will put the terminating null in there for you...
-
- Posts: 4
- Joined: Sat Feb 16, 2013 6:01 pm
Re: Teensy 2 - sprintf - datatype for numbers?
Okaaaay, it did the trick.
No Floats, and replaced longs by int32_t.
The right %ul and I had what I wanted.
Thanks!
Trying to fix my other pitfalls now...
No Floats, and replaced longs by int32_t.
The right %ul and I had what I wanted.
Thanks!
Trying to fix my other pitfalls now...
Please be positive and constructive with your questions and comments.