c++ - 为什么我的 Arduino 不能超过 10?

标签 c++ variables arduino comparison sensors

我在一个 Arduino 项目中,我不断测量气压并使用 Adafruit_BMP085 库计算第一次测量的相对高度。

显示当前值和峰值。它基本上可以工作,但是一旦当前高度超过 10m,它就会停止更新峰值。

草图看起来像这样:

#include <Adafruit_BMP085.h>

String currentAltitude;
String peakAltitude;
int32_t groundpressure;

Adafruit_BMP085 bmp;

void setup() {
    groundpressure = bmp.readPressure();
}

void loop() {
    currentAltitude = bmp.readAltitude(groundpressure);
    if (currentAltitude > peakAltitude) { 
        peakAltitude = currentAltitude;
    }
    Serial.println("Current: " + currentAltitude + "m");
    Serial.println("Peak: " + peakAltitude + "m");
    delay(10);
}

我得到的(当我升高传感器/或增加气压时)是这样的:

Current: 0m
Peak: 0m

Current: 4m
Peak: 4m

Current: 11m
Peak: 4m (still)

为什么它停止比较。是因为我比较的变量类型吗?

最佳答案

是的,这是因为您正在对 String 类型使用重载的 > 运算符,这可能是在执行 lexographic,而不是数字比较。

在执行算术运算之前将字符串数据转换为数字。

关于c++ - 为什么我的 Arduino 不能超过 10?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46223818/

相关文章:

c++ - 如何在单个 for 循环中反转链表?

python - 动态变量创建

c - Arduino代码编译错误: "lvalue required as left operand of assignment"

c++ - 线程与事件同步

c++ - 在 Ubuntu 上构建库,在 Arch 上的项目中使用它 - 不构建

c++ - C++多线程程序中的串行代码执行

python - 在 python 中快速绘制数据

javascript - 使用变量更改 CSS 和 JS

java - 在 Eclipse 中使用数组没有未使用的变量警告

Arduino 显示 Ethernet.localIP()