风筝
发表于: 2019-3-4 17:17:35 | 显示全部楼层

了解如何使用Ardunio UNO、4x4键盘和LCD显示屏制作自己的计算器!


在本篇文章中,您将学习如何使用4X4键盘和Arduino Uno制作Arduino计算器。该计算器将能够执行简单的数学运算,如加法、减法、乘法和除法。它也可以支持带小数点运算。


所需的组件

●    Arduino UNO开发板

●    4x4键盘

●    16x2 I2C LCD显示器

●    跳线

●    Arduino IDE


电路图和说明

4X4键盘有8个引脚需要连接到从D2到D9的Arduino引脚,如下所示:

1.jpg

然后,按如下方式将LCD与Arduino连接:

2.jpg

除数字按钮外,其他按钮的作用如下:

●    'A'用于加法

●    'B'用于减法

●    'C'用于清零

●    'D'用于除法

●    '*'用于乘法


完整的电路图如下。

3.png

Arduino计算器连接图。


代码解释

让我们来看看这个项目所需的代码以及每个代码段的作用。完整代码可在本文结尾处找到。


首先,您需要为键盘和I2C LCD显示添加库。使用的LCD显示屏通过I2C通信与UNO配合使用,因此使用允许Arduino上的I2C通信的wire库。然后,按照4X4键盘的引脚连接以及键盘按钮执行操作的说明进行操作。

  1. #include<Keypad.h>
  2. #include<LiquidCrystal_I2C.h>
  3. #include<Wire.h>

  4. const byte ROWS = 4;
  5. const byte COLS = 4;
  6. char keys[ROWS][COLS] = {
  7.   {'1', '2', '3', '+'},
  8.   {'4', '5', '6', '-'},
  9.   {'7', '8', '9', 'C'},
  10.   {'*', '0', '=', '/'}
  11. };

  12. byte rowPins[ROWS] = {9, 8, 7, 6};
  13. byte colPins[COLS] = {5, 4, 3, 2};
复制代码

setup()函数中,显示屏将显示“Arduino calculator”。

  1. lcd.begin();
  2.   lcd.setCursor(0, 0);
  3.   lcd.print("Arduino Calculator");
  4.   delay(1000);
  5.   scrollDisplay();
  6.   clr();
复制代码

loop()函数中,我们首先得到按下的键然后我们需要检查按下的键是否是数字键。如果是数字,则它将存储在firstNum字符串中。

  1. char newKey = myKeypad.getKey();
  2.   if (newKey != NO_KEY && (newKey == '1' || newKey == '2' || newKey == '3' || newKey == '4' || newKey == '5' || newKey == '6' || newKey == '7' || newKey == '8' || newKey == '9' || newKey == '0')) {

  3.     if (firstNumState == true) {
  4.       firstNum = firstNum + newKey;

  5.       lcd.print(newKey);
  6.     }
  7.     else {
  8.       secondNum = secondNum + newKey;

  9.       lcd.print(newKey);
  10.     }
复制代码

如果按下的键不是数字,请检查它是'+'、' - '、'/'、'*'(在键盘上,这些键是'A'、'B'、'D'、'*' )。如果它来自这些键,我们将存储稍后将使用的值。它还会将firstNum设置为false,这意味着我们现在将得到第二个数字。

现在,其他数值将存储在secondNum字符串中。

  1. if (newKey != NO_KEY && (newKey == '+' || newKey == '-' || newKey == '*' || newKey == '/')) {
  2.     if (firstNumState == true) {
  3.       operatr = newKey;
  4.       firstNumState = false;
  5.       lcd.setCursor(15, 0);
  6.       lcd.print(operatr);
  7.       lcd.setCursor(5, 1);
  8.     }
  9.   }
复制代码

最后,我们将其设置为如果按下的键不是来自操作键,它将检查它是否为'='。如果是这个键,那么它将对第一个和第二个数字执行存储操作并输出结果。


设置完代码后,计算器将能够执行方程式。

4.jpg

  1. if (newKey != NO_KEY && newKey == '=') {
  2.     if (operatr == '+') {
  3.       result = firstNum.toFloat() + secondNum.toFloat();
  4.     }

  5.     if (operatr == '-') {
  6.       result = firstNum.toFloat() - secondNum.toFloat();
  7.     }

  8.     if (operatr == '*') {
  9.       result = firstNum.toFloat() * secondNum.toFloat();
  10.     }

  11.     if (operatr == '/') {
  12.       result = firstNum.toFloat() / secondNum.toFloat();
  13.     }
  14.     lcd.clear();
  15.     lcd.setCursor(0, 0);
  16.     lcd.print(firstNum);
  17.     lcd.print(operatr);
  18.     lcd.print(secondNum);
  19.     lcd.setCursor(0, 1);
  20.     lcd.print("=");
  21.     lcd.print(result);
  22.     firstNumState = true;
  23.   }


  24. And if the key will be 'C', then it will clear the display screen.
  25. if (newKey != NO_KEY && newKey == 'C') {
  26.     clr();
  27.   }
复制代码

本文使用的完整代码如下: main.rar (995 Bytes, 下载次数: 883)

跳转到指定楼层
24824
发表于: 2019-6-3 17:10:36 | 显示全部楼层

有小视频吗
回复

使用道具 举报

24824
发表于: 2019-6-3 17:12:26 | 显示全部楼层

喜欢这个哦⊙∀⊙
回复

使用道具 举报

yangcheng
发表于: 2020-2-26 21:29:31 | 显示全部楼层

为什么我按了数字没有显示
回复

使用道具 举报

风筝
发表于: 2020-2-26 21:34:25 | 显示全部楼层

yangcheng 发表于 2020-2-26 21:29
为什么我按了数字没有显示

看一下接线是否正确
回复

使用道具 举报

yangcheng
发表于: 2020-2-26 21:43:07 | 显示全部楼层

风筝 发表于 2020-2-26 21:34
看一下接线是否正确

接线是对的,屏幕也是亮着的,就是不显示数字
回复

使用道具 举报

yangcheng
发表于: 2020-2-26 21:52:03 | 显示全部楼层

风筝 发表于 2020-2-26 21:34
看一下接线是否正确

还有,如果将你的完整代码直接粘贴到我的arduino上面会显示
Arduino:1.5.6-r2 (Windows 8), 板:"Arduino Uno"

sketch_feb26a.ino: In function 'void setup()':
sketch_feb26a:28: error: no matching function for call to 'LiquidCrystal_I2C::begin()'
C:\Users\17976\Documents\Arduino\libraries\LiquidCrystal_I2C/LiquidCrystal_I2C.h:58: note: candidates are: void LiquidCrystal_I2C::begin(uint8_t, uint8_t, uint8_t)

这些错误
回复

使用道具 举报

风筝
发表于: 2020-2-26 21:58:03 | 显示全部楼层

yangcheng 发表于 2020-2-26 21:52
还有,如果将你的完整代码直接粘贴到我的arduino上面会显示
Arduino:1.5.6-r2 (Windows 8), 板:"Arduino ...

看提示应该是显示屏的库有问题 试试重新安装LiquidCrystal_I2C库
回复

使用道具 举报

yangcheng
发表于: 2020-2-26 21:59:29 | 显示全部楼层

能否提供一下库文件,我怀疑是库的问题
回复

使用道具 举报

风筝
发表于: 2020-2-27 08:52:04 | 显示全部楼层

yangcheng 发表于 2020-2-26 21:59
能否提供一下库文件,我怀疑是库的问题

可以在库管理器或者Github下载  https://github.com/fdebrabander/Arduino-LiquidCrystal-I2C-library
回复

使用道具 举报

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

本版积分规则

主题 700 | 回复: 1479



手机版|

GMT+8, 2024-3-28 16:45 , Processed in 0.100783 second(s), 9 queries , Gzip On, MemCache On. Powered by Discuz! X3.5

YiBoard一板网 © 2015-2022 地址:河北省石家庄市长安区高营大街 ( 冀ICP备18020117号 )

快速回复 返回顶部 返回列表