1

I just yesterday started programming on Arduino, and I have a little problem.

I want to print "#S|SKAITYMAS|[1]#" in one line, except instead of "1" I would like to use an int value. However, this one doesn't work:

Serial.println ("#S|SKAITYMAS|[",LINENR,"]#");

Any hint or tips would be appreciated.

3 Answers 3

3

Do something like this:

Start with your string and integer:

String string = "PI is equal to ";
int integer   = 3.1416;

Then put this in your loop:

Serial.print(string);  //string here
Serial.print(integer); //int here

Serial.println();      //change line

Serial.print() doesn't change line. Its Serial.println() which is the print that changes line so everything that is printed without println will be on the same line.

7
  • The method above will work fine, but if you want to speed up the process I will recommend to use sprint and use only once the print function from Serial.
    – max246
    Oct 8, 2016 at 13:02
  • Well as he said, he started with Arduino yesterday, let's not nuke him with some complicated stuff. keep it simple and keep it cool
    – Dat Ha
    Oct 8, 2016 at 13:06
  • Yep, just pointing out that if you want a faster way, use sprintf. Your solution works fine.
    – max246
    Oct 8, 2016 at 13:17
  • 1
    @max246: Do you have a benchmark to substantiate your claim? Oct 8, 2016 at 19:37
  • 3
    @max246: I just did a simple comparison printing a constant string (or format string) and an int variable. Not only is the sprintf() version 871 CPU cycles slower than individual Serial.print()s, it also makes the program 1314 bytes larger, and it creates an extra copy of the message in RAM. I'm glad sprintf() helped you, but that is likely related to some condition very specific to your program. You should not assume sprintf() to be generally faster. And you should not blindly recommend it over individual print()s, especially to beginners. Oct 9, 2016 at 11:55
4

The answer by canadiancyborg is fine. However, it is always better to avoid using String objects if at all possible, because they use dynamic memory allocation, which carries some risk of memory fragmentation. Simple and safe:

int answer = 42;
Serial.print("The answer is ");
Serial.println(answer);

The only drawback is that the constant string ("The answer is ") takes some RAM. This can be avoided by embedding it in the F() macro, which means “keep this in Flash memory only”. Then, the most memory-friendly idiom is:

int answer = 42;
Serial.print(F("The answer is "));
Serial.println(answer);
2
  • He started Arduino yesterday, keep it simple for him. He ain't doing any memory large programs yet so don't comfuse him too much.
    – Dat Ha
    Oct 8, 2016 at 20:09
  • 1
    @canadiancyborg: That's why I made my first proposal simpler than yours. Oct 9, 2016 at 7:36
-1

It can be done the C way:

sprintf() can be used to generate a formatted string, and then Serial.println() to send the string

char buf[32];
sprintf(buf, "#S|SKAITYMAS|[%d]#", LINENR);
Serial.println(buf);

or with String class as explained here:

https://www.arduino.cc/en/Tutorial/StringAdditionOperator

  String str = "#S|SKAITYMAS|["
  str += LINENR;
  str += "]#";
  Serial.println(str);  

Do not do everything in just one line, as explained on the page link above.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.