匿名用户1 发表于 7 天前

用ARDUINO UNO作为控制器制作的四则运算计算器

本帖最后由 匿名用户1 于 2025-7-12 21:31 编辑

       可以在小键盘上按数字和符号,LCD将显示方程式和结果-就像一个基本的计算器,通过使用I2C LCD显示。                                                                  


材料清单: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 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 = {
{'1','2','3','+'},
{'4','5','6','-'},
{'7','8','9','*'},
{'C','0','=','/'}
};

byte rowPins = {9, 8, 7, 6};
byte colPins = {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; // supports max 10 numbers
char operators; // 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 = temp.toFloat();
      temp = "";
      operators = c;
    }
}
numbers = temp.toFloat();

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

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

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

return total;
}

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

页: [1]
查看完整版本: 用ARDUINO UNO作为控制器制作的四则运算计算器