简体   繁体   English

如何使用Arduino的Serial.println打印带有浮点数的字符串

[英]How to print strings with floats using Arduino's Serial.println

I've been printing a combination of strings and numbers with Arduino's Serial.println() like: 我一直在用Arduino的Serial.println()打印字符串和数字的组合,例如:

Serial.println(((String)"some value: " + some_value);

And this seems to work for most types, but fails for floats, which give me the compilation error: 这似乎适用于大多数类型,但对于浮点数却失败,这给了我编译错误:

ambiguous overload for 'operator+' (operand types are 'String' and 'float') 'operator +'的模棱两可的重载(操作数类型为'String'和'float')

I've tried different calls like: 我尝试过不同的电话,例如:

Serial.println(((String)"some float:" + ((String)some_float));
Serial.println(((String)"some float: " + String(some_float));
Serial.println(((String)"some float: " + String(some_float, 2));

but they give similar errors. 但是它们给出了类似的错误。 Arduino's docs on String seem to imply that these should work. Arduino的String文档似乎暗示它们应该可以工作。 What am I doing wrong? 我究竟做错了什么?

Update 更新资料

After correcting my original errors, I now have this issue: 更正了原始错误之后,我现在遇到了这个问题:

Serial.println(String("some value: ") + String(some_value));
>> call of overloaded ‘String(float&)’ is ambiguous

I also tried: 我也尝试过:

Serial.println(String("some float: ") + String(some_float, 2));
>> call of overloaded ‘String(float&, int)’ is ambiguous

First, you have an extra open bracket in all of your tests. 首先,您在所有测试中都有一个额外的空白。

Next, as the error says, you are attempting to add a float value to a String type. 接下来,如错误所示,您正在尝试将浮点值添加到String类型。 The docs say the + operator only supports adding two String types together. 文档说+运算符仅支持将两种String类型加在一起。

You'll need to convert your float into a String then add the two Strings together. 您需要将浮点数转换为字符串,然后将两个字符串加在一起。 There is an example of this on the doc page you linked: 您链接的文档页面上有一个示例:

String stringOne = String(5.698, 3); 字符串stringOne =字符串(5.698,3); // using a float and the decimal places //使用浮点数和小数位

The last example you gave that gives 'similar errors' looks like it should work when you remove the extra open bracket, according to the docs. 根据文档,您给出的最后一个给出“类似错误”的示例看起来像在删除多余的方括号后应该可以工作。

EDIT: in reference to the new error message ">> call of overloaded 'String(float&)' is ambiguous" can you paste some additional code to show at least: - the definitions of some_float and some_value - any namespaces you are including (eg std etc) Generally with these sorts of problems, you should reduce the code to the simplest example which shows the error and post that whole example in the thread. 编辑:引用新的错误消息“重载的'String(float&)'>>调用不明确”,您能否粘贴一些其他代码以至少显示:-some_float和some_value的定义-您包括的任何名称空间(例如通常,由于此类问题,您应该将代码简化为显示错误的最简单示例,并将整个示例发布到线程中。 (Quite often when you reduce it to the simplest example, you learn what the problem is anyway!) (将其简化为最简单的示例时,经常会发现问题出在哪里!)

The error says that String(float&) is ambiguous , so I'm guessing you have another namespace with a String definition in it which is conflicting with the Arduino version. 该错误表明String(float&)是不明确的 ,因此我猜您在其中具有另一个String定义的名称空间与Arduino版本冲突。

It should be something else. 应该是别的东西。 I tried now with an Arduino UNO this code: 我现在尝试使用Arduino UNO这段代码:

float some_float;

void setup() {
  Serial.begin(9600);
  some_float = 13.3558;
}

void loop() {
  Serial.println(String("some float: ") + String(some_float, 2));
  delay(1000);
}

and the output is 输出是

some_float: 13.35
some_float: 13.35
some_float: 13.35
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM