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

    文章

    441

    浏览量

    39002
  • Arduino
    +关注

    关注

    190

    文章

    6527

    浏览量

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

扫码添加小助手

加入工程师交流群

    评论

    相关推荐
    热点推荐

    TMS570LS31x4/21x4微控制:设计与应用深度解析

    TMS570LS31x4/21x4微控制:设计与应用深度解析 在电子工程领域,高性能、高安全性的微控制是众多应用的核心。TMS570LS31x4
    的头像 发表于 04-23 14:55 49次阅读

    STPMC1可编程多相电能计算器IC:设计与应用详解

    STPMC1可编程多相电能计算器IC:设计与应用详解 在电力计量领域,精准且高效的电能计算是关键。STPMC1作为一款可编程多相电能计算器IC,为电力线系统的有效电能测量提供了强大的解决方案。今天
    的头像 发表于 03-02 16:55 586次阅读

    TMS570LS31x4/21x4微控制:高安全标准下的强大之选

    TMS570LS31x4/21x4微控制:高安全标准下的强大之选 在电子设计领域,对于高性能且适用于安全关键应用的微控制的需求与日俱增。德州仪器(TI)的TMS570LS31
    的头像 发表于 02-09 17:10 510次阅读

    探索DS90CP04:1.5 Gbps 4x4 LVDS交叉点开关的卓越性能

    探索DS90CP04:1.5 Gbps 4x4 LVDS 交叉点开关的卓越性能 在当今高速数据传输的时代,低抖动、低偏斜的高速开关设备对于确保数据的高效、准确传输至关重要。DS90CP04作为一款由
    的头像 发表于 12-30 14:50 523次阅读

    解析SN65LVDS125A和SN65LVDT125A:4x4非阻塞交叉点开关的卓越性能

    解析SN65LVDS125A和SN65LVDT125A:4x4非阻塞交叉点开关的卓越性能 在电子设计领域,高速信号处理和灵活的信号路由是许多应用的关键需求。德州仪器(TI)的SN65LVDS125A
    的头像 发表于 12-29 17:40 735次阅读

    探索SN65LVDS125A和SN65LVDT125A:4x4非阻塞交叉点开关的卓越性能

    探索SN65LVDS125A和SN65LVDT125A:4x4非阻塞交叉点开关的卓越性能 在当今高速发展的电子领域,数据传输和信号处理的速度和效率至关重要。而SN65LVDS125A
    的头像 发表于 12-29 17:40 870次阅读

    探索SN65LVDS250和SN65LVDT250:高性能LVDS 4x4交叉点开关

    探索SN65LVDS250和SN65LVDT250:高性能LVDS 4x4交叉点开关 在高速数据传输的领域中,对于实现高效、稳定的数据交换,合适的开关器件至关重要。今天咱就来深入探讨一下TI推出
    的头像 发表于 12-29 17:10 652次阅读

    深入解析SN65LVDS250与SN65LVDT250:高性能4x4 LVDS交叉点开关

    深入解析SN65LVDS250与SN65LVDT250:高性能4x4 LVDS交叉点开关 在高速数据处理与传输的领域中,交叉点开关作为关键组件,对于数据的灵活路由和高效传输起着至关重要的作用。今天
    的头像 发表于 12-29 17:10 673次阅读

    探索SN65LVCP404:高性能千兆4x4交叉点开关的技术剖析

    探索SN65LVCP404:高性能千兆4x4交叉点开关的技术剖析 在高速数据传输的电子领域,一款性能卓越的交叉点开关对于确保数据的高效、稳定传输至关重要。今天,我们就来深入剖析德州仪器(TI
    的头像 发表于 12-26 14:25 439次阅读

    深入剖析DS10CP154A:1.5 Gbps 4x4 LVDS交叉点开关的卓越性能与应用

    深入剖析DS10CP154A:1.5 Gbps 4x4 LVDS 交叉点开关的卓越性能与应用 在高速信号处理和路由领域,德州仪器(TI)的DS10CP154A 1.5 Gbps 4x4 LVDS
    的头像 发表于 12-26 11:15 527次阅读

    探索DS25CP104A/DS25CP114 3.125 Gbps 4x4 LVDS交叉点开关的奥秘

    探索DS25CP104A/DS25CP114 3.125 Gbps 4x4 LVDS交叉点开关的奥秘 在高速信号路由和切换的领域里,DS25CP104A和DS25CP114这两款由德州仪器推出
    的头像 发表于 12-24 17:45 799次阅读

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

    STMicroelectronics X-NUCLEO-53L4A2扩展板设计用于配备Arduino R3连接的任何STM32 Nucleo开发板。X-NUCLEO-53L4A2扩展
    的头像 发表于 10-30 16:10 987次阅读
    基于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 1835次阅读
    Qorvo全新设计<b class='flag-5'>计算器</b>:晶振选型、能耗预算<b class='flag-5'>计算器</b>和链路预算与覆盖范围<b class='flag-5'>计算器</b>

    VirtualLab:衍射角计算器

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

    HMC629ALP4E 3dB LSB GaAs MMIC 4位数字衰减,DC-10GHz技术手册

    /TTL,可接受三线式串行输入或4位并行字。HMC629ALP4E采用符合RoHS标准的4x4 mm QFN无铅封装,无需外部匹配元件。
    的头像 发表于 04-24 09:31 1179次阅读
    HMC629ALP<b class='flag-5'>4</b>E 3dB LSB GaAs MMIC <b class='flag-5'>4</b>位数字衰减<b class='flag-5'>器</b>,DC-10GHz技术手册