Arduino Uno 在计算时出错

Arduino Uno is making errors when doing calculations

所以我一直在用我的 Arduino 制作一个计算器,这就是我所做的。虽然,它没有像我预期的那样工作。当我输入简单的计算时,它会吐出来,但是当我输入复杂的计算时,它会发狂!它告诉我 9999 * 9 大约是 -14554 或类似的东西。这是代码:

#include <LiquidCrystal_I2C.h>
#include <LCD.h>
#include <Keypad.h>
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
const byte ROWS = 4;
const byte COLS = 4;
char keys [ROWS] [COLS] = {
  {'1', '2', '3', '+'},
  {'4', '5', '6', '-'},
  {'7', '8', '9', '*'},
  {'C', '0', '=', '/'}
};
byte rowPins[ROWS] = {13,12,11,10};
byte colPins[COLS] = {9,8,7,6};
Keypad myKeypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
boolean ansPresent = false;
boolean opSelected = false;
boolean final = false;
String num1, num2;
int answer;
char op;
void setup() {
  lcd.begin(16,2);
  lcd.setCursor(0,0);
  lcd.print("Arduino");
  lcd.setCursor(0,1);
  lcd.print("Calculator");
  delay(2000);
  lcd.clear();
  lcd.setCursor(0,0);
}
void loop(){
  char key = myKeypad.getKey();
  if (ansPresent == false && key != NO_KEY && (key=='1'||key=='2'||key=='3'||key=='4'||key=='5'||key=='6'||key=='7'||key=='8'||key=='9'||key=='0')) {
    if (opSelected == false) {
      num1 = num1 + key;
      lcd.setCursor(0, 0);;
      lcd.print(num1);
    }
    else {
      num2 = num2 + key;
      lcd.setCursor(0, 1);
      lcd.print(num2);
      final = true;
    }
  }
  else if (ansPresent == false && opSelected == false && key != NO_KEY && (key == '/' || key == '*' || key == '-' || key == '+')) {
    opSelected = true;
    op = key;
    lcd.setCursor(15, 0);
    lcd.print(op);
    lcd.setCursor(15, 1);
    lcd.print("=");
  }
  else if (ansPresent == false && final == true && key != NO_KEY && key == '=') {
    if (op == '+'){
      answer = num1.toInt() + num2.toInt();
    }
    else if (op == '-') {
      answer = num1.toInt() - num2.toInt();
    }
    else if (op == '*') {
      answer = num1.toInt() * num2.toInt();
    }
    else if (op == '/') {
      answer = num1.toInt() / num2.toInt();
    }     
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print(answer);
      ansPresent = true;
  }
  else if (key != NO_KEY && key == 'C') {
    lcd.clear();
    ansPresent = false;
    opSelected = false;
    final = false;
    num1 = "";
    num2 = "";
    answer = 0;
    op = ' ';
  }
}

这样做有什么原因吗?

这看起来确实像是 what the Arduino Uno uses internally 的 16 位有符号整数的溢出。数字 > 32767 不能用这个表示。这不是错误,这是硬件的限制,它只是 16 位。

您需要使用多个 int 值才能容纳更大的东西。

Arduino Uno 使用 ATmega。在该板上,一个 int 是一个 16 位有符号值,范围为 -32,768 到 32,767。

这意味着您的乘法溢出并且您看到的打印出来的是——根据文档——“不可预测”。所以千万不要那样做。

https://www.arduino.cc/reference/en/language/variables/data-types/int/

Notes and Warnings When signed variables are made to exceed their maximum or minimum capacity they overflow. The result of an overflow is unpredictable so this should be avoided. A typical symptom of an overflow is the variable "rolling over" from its maximum capacity to its minimum or vice versa, but this is not always the case. If you want this behavior, use unsigned int.