0
  • 聊天消息
  • 系統(tǒng)消息
  • 評論與回復(fù)
登錄后你可以
  • 下載海量資料
  • 學(xué)習(xí)在線課程
  • 觀看技術(shù)視頻
  • 寫文章/發(fā)帖/加入社區(qū)
會員中心
創(chuàng)作中心

完善資料讓更多小伙伴認(rèn)識你,還能領(lǐng)取20積分哦,立即完善>

3天內(nèi)不再提示

怎樣用4X4鍵盤和ArduinoUno制作Arduino計算器

454398 ? 來源:工程師吳畏 ? 2019-08-05 09:51 ? 次閱讀

電路圖和說明

4X4鍵盤有8個引腳需要連接到從D2到D9的Arduino引腳,如下所示:

怎樣用4X4鍵盤和ArduinoUno制作Arduino計算器

然后,將LCD連接到Arduino,如下所示:

除了數(shù)字按鈕之外的按鈕將執(zhí)行以下任務(wù):

‘A’用于添加

‘B’用于減法

‘C’用于清除

‘D’用于劃分

‘*’用于乘法

完整的電路圖如下所示。

Arduino計算器圖。

代碼細(xì)分和演練

我們來看看查看該項目所需的代碼以及每個代碼段的作用。

首先,您需要為鍵盤和I2C LCD顯示添加庫。使用的LCD顯示器通過I2C通信與UNO配合使用,因此使用允許在Arduino上進(jìn)行I2C通信的線程庫。

然后,按照4X4鍵盤的引腳連接和鍵盤的說明進(jìn)行操作按鈕執(zhí)行操作。

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

在設(shè)置功能中,顯示屏將顯示“MakerPro的Arduino計算器”。

lcd.begin();

lcd.setCursor(0, 0);

lcd.print(“Arduino Calculator”);

lcd.setCursor(0, 1);

lcd.print(“by MakerPro”);

delay(1000);

scrollDisplay();

clr();

在循環(huán)功能中,我們先來得到按下的鍵然后我們需要檢查按下的鍵是否是數(shù)字鍵。如果是數(shù)字,則它將存儲在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);

}

如果按下的鍵不是數(shù)字,請檢查是否為‘+’,‘ - ’,‘/’,‘*’(在thekeypad上,這些鍵是‘A’,‘B’,‘D’,‘*’)。如果它來自這些鍵,我們將存儲稍后將使用的值。它還會將firstNum設(shè)置為false,這意味著我們現(xiàn)在將得到第二個數(shù)字。

現(xiàn)在,其他數(shù)值將存儲在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);

}

}

最后,我們設(shè)置它,所以如果按下的鍵不是來自操作鍵,它將檢查它是否是‘=’。如果是這個鍵,那么它將對第一個和第二個數(shù)字執(zhí)行存儲操作并輸出結(jié)果。

設(shè)置完代碼后,計算器將能夠執(zhí)行方程式。

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 = ‘ ’;

}

聲明:本文內(nèi)容及配圖由入駐作者撰寫或者入駐合作網(wǎng)站授權(quán)轉(zhuǎn)載。文章觀點僅代表作者本人,不代表電子發(fā)燒友網(wǎng)立場。文章及其配圖僅供工程師學(xué)習(xí)之用,如有內(nèi)容侵權(quán)或者其他違規(guī)問題,請聯(lián)系本站處理。 舉報投訴
  • 計算器
    +關(guān)注

    關(guān)注

    16

    文章

    437

    瀏覽量

    37230
  • Arduino
    +關(guān)注

    關(guān)注

    187

    文章

    6455

    瀏覽量

    186388
收藏 人收藏

    評論

    相關(guān)推薦

    基于FPGA的計算器設(shè)計

    本文通過FPGA實現(xiàn)8位十進(jìn)制數(shù)的加、減、乘、除運算,通過矩陣鍵盤輸入數(shù)據(jù)和運算符,矩陣鍵盤的布局圖如下所示。該計算器可以進(jìn)行連續(xù)運算,當(dāng)按下等號后,可以直接按數(shù)字進(jìn)行下次運算,或者按運算符,把上次運算結(jié)果作為本次運算的第一個操
    的頭像 發(fā)表于 10-24 14:28 ?98次閱讀
    基于FPGA的<b class='flag-5'>計算器</b>設(shè)計

    開源項目!基于 Arduino DIY 漂亮的宏機(jī)械鍵盤

    。 我利用黑色 PLA 材料,通過 3D 打印技術(shù)精心制作鍵盤的外殼及其蓋子。外殼上巧妙設(shè)置了一個網(wǎng)格,用于安裝按鍵。內(nèi)部空間則用于放置 Arduino 主板及連接線。此外,我特意在外殼背面預(yù)留了一個孔
    發(fā)表于 08-19 17:02

    OC5865X資料(參數(shù)計算器&amp;原理圖)

    電子發(fā)燒友網(wǎng)站提供《OC5865X資料(參數(shù)計算器&原理圖).zip》資料免費下載
    發(fā)表于 07-17 12:03 ?0次下載

    SN65LVCP404千兆位4x4交叉點開關(guān)數(shù)據(jù)表

    電子發(fā)燒友網(wǎng)站提供《SN65LVCP404千兆位4x4交叉點開關(guān)數(shù)據(jù)表.pdf》資料免費下載
    發(fā)表于 07-08 11:12 ?0次下載
    SN65LVCP404千兆位<b class='flag-5'>4x4</b>交叉點開關(guān)數(shù)據(jù)表

    DS25CP104A/CP114 3.125 Gbps 4x4 LVDS交叉點開關(guān)數(shù)據(jù)表

    電子發(fā)燒友網(wǎng)站提供《DS25CP104A/CP114 3.125 Gbps 4x4 LVDS交叉點開關(guān)數(shù)據(jù)表.pdf》資料免費下載
    發(fā)表于 07-04 09:55 ?0次下載
    DS25CP104A/CP114 3.125 Gbps <b class='flag-5'>4x4</b> LVDS交叉點開關(guān)數(shù)據(jù)表

    DS10CP154A 1.5Gbps 4x4 LVDS交叉點開關(guān)數(shù)據(jù)表

    電子發(fā)燒友網(wǎng)站提供《DS10CP154A 1.5Gbps 4x4 LVDS交叉點開關(guān)數(shù)據(jù)表.pdf》資料免費下載
    發(fā)表于 07-04 09:24 ?0次下載
    DS10CP154A 1.5Gbps <b class='flag-5'>4x4</b> LVDS交叉點開關(guān)數(shù)據(jù)表

    LVDS 4x4交叉點開關(guān)SN65LVDS250數(shù)據(jù)表

    電子發(fā)燒友網(wǎng)站提供《LVDS 4x4交叉點開關(guān)SN65LVDS250數(shù)據(jù)表.pdf》資料免費下載
    發(fā)表于 06-26 11:12 ?0次下載
    LVDS <b class='flag-5'>4x4</b>交叉點開關(guān)SN65LVDS250數(shù)據(jù)表

    RUCKUS R760資料:室內(nèi) Wi-Fi 6E 4x4:4 接入點,8.35 Gbps數(shù)據(jù)速率

    電子發(fā)燒友網(wǎng)站提供《RUCKUS R760資料:室內(nèi) Wi-Fi 6E 4x4:4 接入點,8.35 Gbps數(shù)據(jù)速率.pdf》資料免費下載
    發(fā)表于 05-28 16:32 ?0次下載

    OpenHarmony開發(fā)案例:【分布式計算器

    使用分布式能力實現(xiàn)了一個簡單的計算器應(yīng)用,可以進(jìn)行簡單的數(shù)值計算,支持遠(yuǎn)程拉起另一個設(shè)備的計算器應(yīng)用,兩個計算器應(yīng)用進(jìn)行協(xié)同計算。
    的頭像 發(fā)表于 04-11 15:24 ?946次閱讀
    OpenHarmony開發(fā)案例:【分布式<b class='flag-5'>計算器</b>】

    AWTK 開源串口屏開發(fā)(13) - 計算器應(yīng)用

    就需要這樣一個應(yīng)用。在計算器中會用到一些有意思的知識點,比如嵌入鍵盤,在數(shù)字輸入或密碼輸入也會用到。這里我們實現(xiàn)一個簡單的計算器,不需要編寫代碼,設(shè)計好界面,添加綁定
    的頭像 發(fā)表于 03-16 08:23 ?4952次閱讀
    AWTK 開源串口屏開發(fā)(13) - <b class='flag-5'>計算器</b>應(yīng)用

    使用Arduino Nano制作一個4×4×4 LED立方體

    在這個項目中,我們將使用 Arduino Nano 制作一個很酷的 4×4×4 LED立方體。LED 立方體,也稱為 LED矩陣,可以照亮您
    的頭像 發(fā)表于 02-11 12:07 ?2901次閱讀
    使用<b class='flag-5'>Arduino</b> Nano<b class='flag-5'>制作</b>一個<b class='flag-5'>4</b>×<b class='flag-5'>4</b>×<b class='flag-5'>4</b> LED立方體

    根號計算器在線計算怎么

    表示3的立方根。 平方根是指某個數(shù)的平方等于給定的數(shù),而立方根是指某個數(shù)的立方等于給定的數(shù)。以平方根為例,如果一個數(shù)x的平方等于給定數(shù)a,那么我們說a的平方根為x。數(shù)學(xué)上可以x=√a
    的頭像 發(fā)表于 01-25 11:15 ?2637次閱讀

    pcb過孔電流計算器怎么

    PCB過孔電流計算器是一種用于計算PCB板上過孔電流的工具。過孔是PCB板上的重要元件,用于連接不同層之間的電路。過孔的電流大小對于PCB板的性能和穩(wěn)定性具有重要影響。因此,正確使用PCB過孔電流計算器
    的頭像 發(fā)表于 12-14 16:20 ?7041次閱讀

    怎樣用ADAU1761設(shè)計DRC的壓縮/擴(kuò)展?

    請問怎樣用ADAU1761設(shè)計DRC的壓縮/擴(kuò)展。我在SigmaStudio 4.5的模塊中只找到RMS。如果ADAU1761設(shè)計DRC要怎樣
    發(fā)表于 11-28 06:41

    怎樣用32單片機(jī)測電壓?

    怎樣用32單片機(jī)測電壓
    發(fā)表于 10-31 07:09