c++ - 即使我的 if 语句成立,我也遇到了麻烦

标签 c++ arduino

我的 AltCheckInt 声明不成立。它从 9999-33000 开始工作,然后切换到 false,在 55000 时再次切换回 true,然后由于某种未知原因在 70000 时再次切换到 false。 com 的值为 SPEED 34482。有人看到我没看到的东西吗?

void AltDisplay(String com){

String AltCheckStg;
int AltCheckInt;
String Action;
String Status;
int StatusInt;
Action = com.substring(0,6);
if (Action == "SPEED "){
    Status = com.substring(6,10);
    AltCheckStg = com.substring(6,18);
    AltCheckInt = AltCheckStg.toInt();
    StatusInt = Status.toInt();
    Serial.println(AltCheckInt); 
    if(AltCheckInt >= 9999){
      digitalWrite(7, HIGH);
      StatusInt = StatusInt/100;
    }
    s7s.write(0x77);
    s7s.write(0b0000001);
    s7s.print(StatusInt);
    delay(500);
    s7s.write(0x76);
    digitalWrite(7, LOW);
    }
}

最佳答案

Arduino 是 16 位的,这意味着最大值为 int可以容纳的是32,767。由于 integer overflow ,您将在 32,767 之后看到的下一个值为 -32,768。

如果你想要更大的范围,你可能想要切换到 long .

值得注意的是,toInt() 已经返回一个long,因此您需要做的就是更改AltCheckInt 的类型。

关于c++ - 即使我的 if 语句成立,我也遇到了麻烦,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27768947/

相关文章:

C++ 异常 : out_of_range_error from Programming Principles

c++ - 有运行时错误的业力发生器有什么调试技巧

c++ - 如果其中一个进程意外终止,进程间内存会发生什么情况?

c++ - 对 COleVariant 进行空检查

c++ - 如何识别生成警告的 header ?

linux - 使用 Telnet 连接到 Linux Prompt

c++ - Arduino MKR Wifi 1010与Arduino Mega的串行通信

javascript - 错误 : fs. readFileSync 不是函数

C - 如何使用 PROGMEM 存储和读取字符数组

c# - 如何使用 .NET 在图像中查找橙色球?