I was creating Arduino function which sends two int
and additional char
s between them and at the beginning and end of char
array - then print that array to the serial port in almost real time. (For example "X50Y100T".)
I don't want to use String
object in Arduino and I found two possible solutions:
Using
sprintf()
:char sendBuffer[16]; void dataSend(int first, int second) { sprintf(sendBuffer, "X%dY%dT", first, second); Serial.println(sendBuffer); };
Or something like this:
char sendBuffer[16]; void dataSend(int first, int second){ char convert[6]; sendBuffer[0] = 'X'; itoa(first, convert, 10); strcat(sendBuffer, convert); sendBuffer[strlen(sendBuffer)] = 'Y'; itoa(second, convert, 10); strcat(sendBuffer, convert); sendBuffer[strlen(sendBuffer)] = 'T'; Serial.println(sendBuffer); memset(sendBuffer,0,sizeof(sendBuffer)); };
My question is - which one is better from technical point of view?
The sprintf()
looks better but it increases the sketch size by ~1,5KB. Are there any other drawbacks of sprintf()
? The second solution do many things to achieve same thing and I don't know if it's efficient... Maybe there are other solutions for sending this kind of char
array?