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

    文章

    440

    浏览量

    38713
  • Arduino
    +关注

    关注

    190

    文章

    6515

    浏览量

    195959
收藏 人收藏
加入交流群
微信小助手二维码

扫码添加小助手

加入工程师交流群

    评论

    相关推荐
    热点推荐

    基于VL53L4CX的飞行时间传感扩展板:X-NUCLEO-53L4A2技术解析

    STMicroelectronics X-NUCLEO-53L4A2扩展板设计用于配备Arduino R3连接的任何STM32 Nucleo开发板。X-NUCLEO-53L4A2扩展
    的头像 发表于 10-30 16:10 319次阅读
    基于VL53L<b class='flag-5'>4</b>CX的飞行时间传感<b class='flag-5'>器</b>扩展板:<b class='flag-5'>X-NUCLEO-53L4</b>A2技术解析

    Qorvo全新设计计算器:晶振选型、能耗预算计算器和链路预算与覆盖范围计算器

    款功能强大的PC端计算工具 。这些工具—— 晶振采购工具 、 能耗预算计算器 和 链路预算与覆盖范围计算器 ——让优化晶振选型、预测电池续航时间以及评估RF链路性能变得前所未有地简单。 接下来,让我们深入了解每一款
    的头像 发表于 06-24 17:51 1471次阅读
    Qorvo全新设计<b class='flag-5'>计算器</b>:晶振选型、能耗预算<b class='flag-5'>计算器</b>和链路预算与覆盖范围<b class='flag-5'>计算器</b>

    VirtualLab:衍射角计算器

    介质的折射率、结构的周期和入射角。这种相关性在数学上被编码在光栅方程中。在这个例中,我们介绍了VirtualLab Fusion的衍射角计算器,这是一个用于计算光栅方程的方便工具。 打开衍射角
    发表于 06-16 08:48

    HMC695LP4/HMC695LP4E x4有源倍频,11.4-13.2GHz输出技术手册

    HMC695LP4(E)是一款利用InGaP GaAs HBT技术制造而成的有源微型x4倍频,采用4x4 mm无铅表贴封装。 在+5V电源电压下,功率输出为+7 dBm(典型值)且相
    的头像 发表于 04-18 14:07 664次阅读
    HMC695LP<b class='flag-5'>4</b>/HMC695LP<b class='flag-5'>4</b>E <b class='flag-5'>x4</b>有源倍频<b class='flag-5'>器</b>,11.4-13.2GHz输出技术手册

    HMC444LP4/444LP4E有源x8倍频,采用SMT封装技术手册

    HMC444LP4(E)是一款有源微型x8倍频,使用InGaP GaAs HBT技术,采用4x4 mm无引脚表面贴装封装。 功率输出为+6 dBm(典型值),电源电压为5V,在不同的
    的头像 发表于 04-17 17:03 927次阅读
    HMC444LP<b class='flag-5'>4</b>/444LP<b class='flag-5'>4</b>E有源<b class='flag-5'>x</b>8倍频<b class='flag-5'>器</b>,采用SMT封装技术手册

    HMC443LP4/443LP4E x4有源倍频,采用SMT封装技术手册

    HMC443LP4(E)是一款有源微型x4倍频,使用InGaP GaAs HBT技术,采用4x4 mm无引脚表面贴装封装。 功率输出为+4
    的头像 发表于 04-17 16:42 717次阅读
    HMC443LP<b class='flag-5'>4</b>/443LP<b class='flag-5'>4</b>E <b class='flag-5'>x4</b>有源倍频<b class='flag-5'>器</b>,采用SMT封装技术手册

    HMC370LP4/370LP4E x4有源倍频SMT技术手册

    HMC370LP4(E)是一款利用InGaP GaAs HBT技术制造而成的有源微型x4倍频,采用4x4 mm无铅表面贴装封装。 在5V电源电压下,功率输出为0 dBm(典型值)且相
    的头像 发表于 04-17 11:30 666次阅读
    HMC370LP<b class='flag-5'>4</b>/370LP<b class='flag-5'>4</b>E <b class='flag-5'>x4</b>有源倍频<b class='flag-5'>器</b>SMT技术手册

    HMC368LP4/368LP4E x2有源倍频SMT技术手册

    HMC368LP4(E)是一款利用GaAs PHEMT技术制造而成的微型放大器倍频,采用4x4 mm无铅表面贴装封装。 由+2 dBm信号驱动时,该倍频在9至16 GHz范围内提供
    的头像 发表于 04-17 11:15 765次阅读
    HMC368LP<b class='flag-5'>4</b>/368LP<b class='flag-5'>4</b>E <b class='flag-5'>x</b>2有源倍频<b class='flag-5'>器</b>SMT技术手册

    Sky5® LB/LMB/MB/HB 和 4x4 MIMO 分集接收模块 skyworksinc

    电子发烧友网为你提供()Sky5® LB/LMB/MB/HB 和 4x4 MIMO 分集接收模块相关产品参数、数据手册,更有Sky5® LB/LMB/MB/HB 和 4x4 MIMO 分集接收模块
    发表于 04-11 15:21
    Sky5® LB/LMB/MB/HB 和 <b class='flag-5'>4x4</b> MIMO 分集接收模块 skyworksinc

    VirtualLab Fusion应用:相干时间和相干长度计算器

    摘要 在本例中,我们介绍了一种计算器,它可以根据给定光源的波谱信息快速估计其时间相干特性。然后,可以将该计算器的结果自动复制到通用探测中,以便在考虑时间相干性时应用近似方法,而无
    发表于 04-08 08:48

    VirtualLab:衍射角计算器

    介质的折射率、结构的周期和入射角。这种相关性在数学上被编码在光栅方程中。在这个例中,我们介绍了VirtualLab Fusion的衍射角计算器,这是一个用于计算光栅方程的方便工具。 打开衍射角
    发表于 04-08 08:46

    HMC596 CMOS 4x2开关矩阵,采用SMT封装技术手册

    HMC596LP4(E)是一款低成本4x2开关矩阵产品,采用无引脚QFN 4x4 mm表贴封装,可用于卫星/DBS、LNB和200 MHz至3000 MHz的多路开关。 开关上集成由正电压控制的
    的头像 发表于 03-07 16:50 1391次阅读
    HMC596 CMOS <b class='flag-5'>4x</b>2开关矩阵,采用SMT封装技术手册

    VirtualLab Fusion应用:相干时间和相干长度计算器

    摘要 在本例中,我们介绍了一种计算器,它可以根据给定光源的波谱信息快速估计其时间相干特性。然后,可以将该计算器的结果自动复制到通用探测中,以便在考虑时间相干性时应用近似方法,而无需
    发表于 12-27 08:48

    Debye-Wolf积分计算器的用法

    Wolf积分计算器。 •接下来,我们分别设置光源,光学设置和数值参数。 光源-入射场 •此处的波长设置为532 nm。 •全局偏振设置为线性。角度0表示场矢量在x轴上。 •也可以选择其他类型的偏振,如圆
    发表于 12-26 08:59

    LP光纤模式计算器

    的 Bessel 和用于渐变折射率光纤的 Laguerre。 此例展示了如何使用计算器以及如何配置模式的采样参数。 配置光纤结构:Step-Index Fiber(阶跃折射率光纤) 光纤模式计算器允许定义
    发表于 12-18 13:36