电路图和说明
4x4键盘有8个引脚需要连接到从d2到d9的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 = ‘ ’;
}
北京现代将出售重庆工厂 起售价36.8亿元
撬动全球市场 NB-IoT标准发展将面临六大挑战
国产 ARM 芯片设计大多数只能满足嵌入式设计?
Bridgtek推出最新EVE图形控制器 具ASTC功能可提升数据存储能力
微机实验:显示和键盘
怎样用4X4键盘和ArduinoUno制作Arduino计算器
LG电子在墨西哥雷诺萨的工厂正为北美市场加大力度生产电视机
内置晶振极高精度的实时时钟芯片PCF2129介绍
PWM控制器的主要参数
开启无卡生活智付时代 vivo携手中国银联正式发布vivo Pay
基于USB和便携式医疗设备的数据采集系统实现
为什么量子计算与AI一样重要量子计算到底是什么
利天万世20GWh锂电池项目举行开工仪式 总投资达15亿美元
台电按键加密U盘上手评测:目标明确 主打安全
这款全面屏手机仅2499,为什么热度还不如三个月前的小米6
差分信号是5V的还是24V的?差分信号电压是多少?
思倍云荣膺2023年度“毕马威中国领先不动产科技企业50”
最新云计算预测:云安全、自动化融合、数据中心成关键
存储器市场需要寻找一个新的发展方向
近10亿元!国内将增2个SiC相关项目