How to print Integer alongside String Arduino?

后端 未结 2 576
悲&欢浪女
悲&欢浪女 2021-01-23 04:57

I am trying to print an integer alongside a string but it\'s not really working out and am getting confused.

int cmdSeries = 3;

Serial.println(\"Series : \" + c         


        
相关标签:
2条回答
  • 2021-01-23 05:24

    There is a huge difference between Arduino String class and regular C-string. The first one overloads addition operator, but there is almost excessive usage of dynamic memory. Mainly if you use something like:

    String sth = String("blabla") + intVar + "something else" + floatVar;
    

    Much better is just using:

    Serial.print("Series : ");
    Serial.println(cmdSeries);
    

    Btw, this string literal resides in Flash and RAM memory, so if you want to force using flash only:

    Serial.print(F("Series : "));
    

    But it's for AVR based Arduinos only. This macro can save a lots of RAM, if you are using lots of literals.

    EDIT: Sometimes I use this:

    template <class T> inline Print & operator<<(Print & p, const T & val) {
      p.print(val);
      return p;
    }
    
    // ...
    
    Serial << F("Text ") << intVar << F("...") << "\n";
    

    It prints each part separately, no concatenations or so.

    0 讨论(0)
  • 2021-01-23 05:27

    Try this

    int cmdSeries = 3;
    Serial.println(String("Series : ") + cmdSeries);
    
    0 讨论(0)
提交回复
热议问题