如何与String Arduino一起打印Integer? [英] How to print Integer alongside String Arduino?

查看:102
本文介绍了如何与String Arduino一起打印Integer?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在字符串旁边打印一个整数,但是它实际上并不能解决问题,并且变得令人困惑.

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 : " + cmdSeries);// That's where the problem occur

在Visual Basic中,我们通常采用这种方式:

In visual basic we used to do it this way:

Dim cmdSeries As Integer
Console.Writeline(""Series : {0}", cmdSeries)

所以我已经用Serial.println尝试过,但是它返回此错误: 重载的'println(const char [14],int&)'的调用是不明确的

So i've tried it with Serial.println but it returns this error : call of overloaded 'println(const char [14], int&)' is ambiguous

任何人都可以帮忙吗,我想在不使用任何库的情况下以干净的方式实现这一目标.

Can anyone help my out, I want to achieve this without using any libraries and in a clean way.

推荐答案

Arduino字符串类和常规C字符串之间存在巨大差异. 第一个重载加法运算符,但是动态内存几乎过多使用.主要是如果您使用类似的东西:

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;

使用以下方法会更好:

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

顺便说一句,此字符串文字位于Flash和RAM内存中,因此,如果您只想强制使用Flash:

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

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

但这仅适用于基于AVR的Arduino.如果您使用大量文字,则此宏可以节省大量RAM.

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

有时候我用这个:

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.

这篇关于如何与String Arduino一起打印Integer?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆