13浏览
查看: 13|回复: 0

[入门] 用ARDUINO UNO作为控制器制作的四则运算计算器

[复制链接]
本帖最后由 匿名用户1 于 2025-7-12 21:31 编辑

       可以在小键盘上按数字和符号,LCD将显示方程式和结果-就像一个基本的计算器,通过使用I2C LCD显示。                                                                   用ARDUINO UNO作为控制器制作的四则运算计算器图1


材料清单:1.公-母杜邦线(若干)     2.Arduino uno r3(1个)    3.16键薄膜键盘(1个)     4.12C16x2LCD显示屏                   (1个)                                 5.公-公杜邦线(若干)

项目描述
LCD连接到Arduino PIN
GND-GND
VCC-5V
SDA   -  A4
SCL-A5   
注意:   
键盘字母“A”必须是加法或“
键盘字母“B”必须是“减法”或“-
键盘字母“C”必须是乘法或“x”
键盘字母“D”必须是除法或“/”
键盘符号“#”中必须是等号或“=”
键盘符号“*”中,必须是清除符号或“C” 如何工作
概述
这个项目将Arduino变成一个基本的计算器。输入数学表达式,如“3+4*2”,它将在小屏幕(LCD)上显示结果。它也正确遵循数学规则-在加法/减法之前进行乘法/除法。
分步说明
1.按键盘上的键
-屏幕(LCD)显示您正在键入的内容。
2.表达式存储为文本
-Arduino收集您按顺序输入的所有数字和符号。
-例如,如果您键入“2+3*4”,它将保存整个字符串。  
3.显示了最终答案
-结果显示在LCD的第二行。
-如果键入无效表达式(如除以零),将显示“0”。
4.按`*`清除
-可以通过按“C”键重置计算器重新开始。
接线图
                                                                    用ARDUINO UNO作为控制器制作的四则运算计算器图2
ARDUINO IDE代码

#include <Wire.h>#include <LiquidCrystal_I2C.h>
#include <Keypad.h>

LiquidCrystal_I2C lcd(0x27, 16, 2);
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] = {9, 8, 7, 6};
byte colPins[COLS] = {5, 4, 3, 2};

Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);

String expression = "";

void setup() {
  lcd.begin(16, 2, LCD_5x8DOTS);
  lcd.backlight();
  lcd.clear();
  lcd.print("Calculator Ready");
  delay(1500);
  lcd.clear();
}

void loop() {
  char key = keypad.getKey();

  if (key != NO_KEY) {
    if ((key >= '0' && key <= '9') || key == '+' || key == '-' || key == '*' || key == '/') {
      if (expression.length() < 16) {
        expression += key;
        lcd.setCursor(0, 0);
        lcd.print(expression);
      }
    } else if (key == '=') {
      float result = evaluateWithPrecedence(expression);
      lcd.setCursor(0, 1);
      lcd.print("= ");
      lcd.print(result);
      expression = "";
    } else if (key == 'C') {
      expression = "";
      lcd.clear();
    }
  }
}

// Evaluates an expression with operator precedence
float evaluateWithPrecedence(String expr) {
  float numbers[10]; // supports max 10 numbers
  char operators[10]; // supports max 9 operators
  int numIndex = 0, opIndex = 0;
  String temp = "";

  // Parse numbers and operators
  for (int i = 0; i < expr.length(); i++) {
    char c = expr.charAt(i);
    if (c >= '0' && c <= '9') {
      temp += c;
    } else {
      numbers[numIndex++] = temp.toFloat();
      temp = "";
      operators[opIndex++] = c;
    }
  }
  numbers[numIndex] = temp.toFloat();

  // First pass: *, /
  for (int i = 0; i < opIndex; i++) {
    if (operators == '*' || operators == '/') {
      float a = numbers;
      float b = numbers[i+1];
      float result = (operators == '*') ? a * b : (b != 0 ? a / b : 0);

      numbers = result;
      for (int j = i + 1; j < numIndex; j++) {
        numbers[j] = numbers[j + 1];
      }
      for (int j = i; j < opIndex - 1; j++) {
        operators[j] = operators[j + 1];
      }
      numIndex--;
      opIndex--;
      i--; // re-check this position
    }
  }

  // Second pass: +, -
  float total = numbers[0];
  for (int i = 0; i < opIndex; i++) {
    if (operators == '+') total += numbers[i + 1];
    else if (operators == '-') total -= numbers[i + 1];
  }

  return total;
}

//代码结束
        本文是作者发表的第一篇文章,参考了不少资料,有些地方有问题和不对的请各位高手多多指教。

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

为本项目制作心愿单
购买心愿单
心愿单 编辑
[[wsData.name]]

硬件清单

  • [[d.name]]
btnicon
我也要做!
点击进入购买页面
上海智位机器人股份有限公司 沪ICP备09038501号-4 备案 沪公网安备31011502402448

© 2013-2025 Comsenz Inc. Powered by Discuz! X3.4 Licensed

mail