0
  • 聊天消息
  • 系统消息
  • 评论与回复
登录后你可以
  • 下载海量资料
  • 学习在线课程
  • 观看技术视频
  • 写文章/发帖/加入社区
创作中心

完善资料让更多小伙伴认识你,还能领取20积分哦,立即完善>

3天内不再提示

怎样用4X4键盘和ArduinoUno制作Arduino计算器

454398 来源:工程师吴畏 2019-08-05 09:51 次阅读

电路图和说明

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

怎样用4X4键盘和ArduinoUno制作Arduino计算器

然后,将LCD连接到Arduino,如下所示:

除了数字按钮之外的按钮将执行以下任务:

‘A’用于添加

‘B’用于减法

‘C’用于清除

‘D’用于划分

‘*’用于乘法

完整的电路图如下所示。

Arduino计算器图。

代码细分和演练

我们来看看查看该项目所需的代码以及每个代码段的作用。

首先,您需要为键盘和I2C LCD显示添加库。使用的LCD显示器通过I2C通信与UNO配合使用,因此使用允许在Arduino上进行I2C通信的线程库。

然后,按照4X4键盘的引脚连接和键盘的说明进行操作按钮执行操作。

#include

#include

#include

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};

在设置功能中,显示屏将显示“MakerPro的Arduino计算器”。

lcd.begin();

lcd.setCursor(0, 0);

lcd.print(“Arduino Calculator”);

lcd.setCursor(0, 1);

lcd.print(“by MakerPro”);

delay(1000);

scrollDisplay();

clr();

在循环功能中,我们先来得到按下的键然后我们需要检查按下的键是否是数字键。如果是数字,则它将存储在firstNum字符串中。

char newKey = myKeypad.getKey();

if (newKey != NO_KEY && (newKey == ‘1’ || newKey == ‘2’ || newKey == ‘3’ || newKey == ‘4’ || newKey == ‘5’ || newKey == ‘6’ || newKey == ‘7’ || newKey == ‘8’ || newKey == ‘9’ || newKey == ‘0’)) {

if (firstNumState == true) {

firstNum = firstNum + newKey;

lcd.print(newKey);

}

else {

secondNum = secondNum + newKey;

lcd.print(newKey);

}

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

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

if (newKey != NO_KEY && (newKey == ‘+’ || newKey == ‘-’ || newKey == ‘*’ || newKey == ‘/’)) {

if (firstNumState == true) {

operatr = newKey;

firstNumState = false;

lcd.setCursor(15, 0);

lcd.print(operatr);

lcd.setCursor(5, 1);

}

}

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

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

if (newKey != NO_KEY && newKey == ‘=’) {

if (operatr == ‘+’) {

result = firstNum.toFloat() + secondNum.toFloat();

}

if (operatr == ‘-’) {

result = firstNum.toFloat() - secondNum.toFloat();

}

if (operatr == ‘*’) {

result = firstNum.toFloat() * secondNum.toFloat();

}

if (operatr == ‘/’) {

result = firstNum.toFloat() / secondNum.toFloat();

}

lcd.clear();

lcd.setCursor(0, 0);

lcd.print(firstNum);

lcd.print(operatr);

lcd.print(secondNum);

lcd.setCursor(0, 1);

lcd.print(“=”);

lcd.print(result);

firstNumState = true;

}

And if the key will be ‘C’, then it will clear the display screen.

if (newKey != NO_KEY && newKey == ‘C’) {

clr();

}

完整计算器项目代码

#include

#include

#include

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};

// Created instances

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

LiquidCrystal_I2C lcd(0x27, 16, 2);

boolean firstNumState = true;

String firstNum = “”;

String secondNum = “”;

float result = 0.0;

char operatr = ‘ ’;

void setup() {

lcd.begin();

lcd.setCursor(0, 0);

lcd.print(“Arduino Calculator”);

lcd.setCursor(0, 1);

lcd.print(“by MakerPro”);

delay(1000);

scrollDisplay();

clr();

}

void loop() {

char newKey = myKeypad.getKey();

if (newKey != NO_KEY && (newKey == ‘1’ || newKey == ‘2’ || newKey == ‘3’ || newKey == ‘4’ || newKey == ‘5’ || newKey == ‘6’ || newKey == ‘7’ || newKey == ‘8’ || newKey == ‘9’ || newKey == ‘0’)) {

if (firstNumState == true) {

firstNum = firstNum + newKey;

lcd.print(newKey);

}

else {

secondNum = secondNum + newKey;

lcd.print(newKey);

}

}

if (newKey != NO_KEY && (newKey == ‘+’ || newKey == ‘-’ || newKey == ‘*’ || newKey == ‘/’)) {

if (firstNumState == true) {

operatr = newKey;

firstNumState = false;

lcd.setCursor(15, 0);

lcd.print(operatr);

lcd.setCursor(5, 1);

}

}

if (newKey != NO_KEY && newKey == ‘=’) {

if (operatr == ‘+’) {

result = firstNum.toFloat() + secondNum.toFloat();

}

if (operatr == ‘-’) {

result = firstNum.toFloat() - secondNum.toFloat();

}

if (operatr == ‘*’) {

result = firstNum.toFloat() * secondNum.toFloat();

}

if (operatr == ‘/’) {

result = firstNum.toFloat() / secondNum.toFloat();

}

lcd.clear();

lcd.setCursor(0, 0);

lcd.print(firstNum);

lcd.print(operatr);

lcd.print(secondNum);

lcd.setCursor(0, 1);

lcd.print(“=”);

lcd.print(result);

firstNumState = true;

}

if (newKey != NO_KEY && newKey == ‘C’) {

clr();

}

}

void scrollDisplay() {

// scroll 13 positions (string length) to the left

// to move it offscreen left:

for (int positionCounter = 0; positionCounter 《 3; positionCounter++) {

// scroll one position left:

lcd.scrollDisplayLeft();

// wait a bit:

delay(300);

}

delay(1000);

// scroll 29 positions (string length + display length) to the right

// to move it offscreen right:

for (int positionCounter = 0; positionCounter 《 3; positionCounter++) {

// scroll one position right:

lcd.scrollDisplayRight();

// wait a bit:

delay(300);

}

delay(2000);

}

void clr() {

lcd.clear();

lcd.setCursor(0, 0);

lcd.print(“1st: ”);

lcd.setCursor(12, 0);

lcd.print(“op ”);

lcd.setCursor(0, 1);

lcd.print(“2nd: ”);

lcd.setCursor(5, 0);

firstNum = “”;

secondNum = “”;

result = 0;

operatr = ‘ ’;

}

声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉
  • 计算器
    +关注

    关注

    16

    文章

    424

    浏览量

    36488
  • Arduino
    +关注

    关注

    184

    文章

    6425

    浏览量

    184769
收藏 人收藏

    评论

    相关推荐

    AWTK 开源串口屏开发(13) - 计算器应用

    就需要这样一个应用。在计算器中会用到一些有意思的知识点,比如嵌入键盘,在数字输入或密码输入也会用到。这里我们实现一个简单的计算器,不需要编写代码,设计好界面,添加绑定
    的头像 发表于 03-16 08:23 127次阅读
    AWTK 开源串口屏开发(13) - <b class='flag-5'>计算器</b>应用

    怎样用ADAU1761设计DRC的压缩/扩展?

    请问怎样用ADAU1761设计DRC的压缩/扩展。我在SigmaStudio 4.5的模块中只找到RMS。如果ADAU1761设计DRC要怎样
    发表于 11-28 06:41

    怎样用32单片机测电压?

    怎样用32单片机测电压
    发表于 10-31 07:09

    怎样用现成的Eclipse插件来调试AT32系列芯片

    拥有很高的灵活性,软件可以以Eclipse为架构开发自己的IDE。这边文档主要描述怎样用现成的Eclipse插件来调试AT32系列芯片。
    发表于 10-24 07:12

    PLC中怎样用X和Y两个轴走出直线轨迹?

    PLC中怎样用X和Y两个轴走出直线轨迹呢?那么这两个轴需要配合成速度成线性比例,位置和速度应该如何云运算呢?
    发表于 09-12 09:58 353次阅读
    PLC中<b class='flag-5'>怎样用</b>X和Y两个轴走出直线轨迹?

    基于51单片机的简易计算器设计

    计算器系统51 系列的单片机进行的数字计算器系统设计,可以完成计算器键盘输入, 进行加、 减、乘、除的简单四则运算,并在 LCD屏幕上相应的显示结果。选择内部存储资源丰富的 51
    的头像 发表于 08-01 00:51 1494次阅读
    基于51单片机的简易<b class='flag-5'>计算器</b>设计

    基于FPGA的4x4矩阵键盘驱动设计

    本次设计采用FPGA驱动4x4矩阵键盘,这个原理其实很简单,但是我在做的时候曾经理解错了一个地方,导致走了一天的弯路,因为感觉比较有意思,所以想在这分享一下。
    的头像 发表于 07-23 11:41 1908次阅读
    基于FPGA的<b class='flag-5'>4x4</b>矩阵<b class='flag-5'>键盘</b>驱动设计

    Arduino运行QMK的宏键盘

    电子发烧友网站提供《Arduino运行QMK的宏键盘.zip》资料免费下载
    发表于 07-13 10:51 0次下载
    <b class='flag-5'>Arduino</b>运行QMK的宏<b class='flag-5'>键盘</b>

    Evive上的科学计算器(由Arduino MEGA提供支持)

    电子发烧友网站提供《Evive上的科学计算器(由Arduino MEGA提供支持).zip》资料免费下载
    发表于 07-10 11:25 0次下载
    Evive上的科学<b class='flag-5'>计算器</b>(由<b class='flag-5'>Arduino</b> MEGA提供支持)

    labview计算器

    labview计算器程序分享
    发表于 05-29 10:34 19次下载

    基于89C51单片机的矩阵键盘简易计算器源程序

    基于89C51单片机的矩阵键盘简易计算器源程序
    发表于 05-15 11:01 20次下载

    怎样用示波器测试lin总线的波特率呢?

    怎样用示波器测试lin总线的波特率呢?有什么方法吗?
    发表于 05-09 11:22

    怎样用FPGA实现FSK调制解调呢?

    最近想做这方面的,怎样用FPGA实现FSK调制解调?但是我一点头绪都没有,哪位高手帮帮忙,讲解一下什么的
    发表于 05-08 17:34

    基于AT89C51单片机12864LCD显示计算器键盘按键实验

    基于AT89C51单片机12864LCD显示计算器键盘按键实验Proteus仿真及程序
    发表于 05-04 15:14 3次下载

    请问Proteus仿真软件中怎样用探针测电压呢?

    请问Proteus仿真软件中怎样用探针测电压呢?
    发表于 04-26 15:55