风筝
发表于: 2020-4-8 14:11:51 | 显示全部楼层

使用Arduino开发板、LCD和键盘可以轻松实现一个简单的基于Arduino的计算器,使用键盘和LCD来解决数学计算。使用此项目可以轻松完成简单的数学计算,例如加法、减法、乘法和除法。


1602 LCD能够解决16位之内的数学计算,而无需延迟和快速处理。Arduino程序也很简单,几乎就像简单的C程序,仅基于加、减、乘和除的简单公式。那么,让我们实现这个简单的小项目。

arduino-calculator.jpg


电路图和要求

电路图非常简单,既可以直接在Arduino开发板上组装电路,也可以通过制作pcb组装电路。电路组件列表如下。

●    Arduino Uno开发板

●    1602液晶屏

●    4 * 4薄膜矩阵键盘

●    复位开关

●    电位器

●    9伏电池

●    面包板和连接导线

Calculator_bb.jpg


源代码

程序基于Arduino UNO开发板,使用Arduino语言完成。您需要为键盘添加一个额外的Arduino库文件,如#include <Keypad.h>。因此,请从adafruit库下载此文件。组装后,只需将源代码上传到Arduino Uno开发板中,就可以享受基于Arduino的计算器。

  1. #include <Keypad.h>
  2. #include <LiquidCrystal.h>

  3. // 1. LCD Pins
  4. LiquidCrystal lcd(7, 8, 9, 10, 11, 12);

  5. // 2. Keypad Pins
  6. const byte Rows = 4;
  7. const byte Cols = 4;
  8. char keys[Rows][Cols] =
  9. {
  10. {'1', '2', '3', '+'},
  11. {'4', '5', '6', '-'},
  12. {'7', '8', '9', '*'},
  13. {'C', '0', '=', '/'}
  14. };
  15. byte rowPins[Rows] = {A2, A3, A4, A5};
  16. byte colPins[Cols] = {2, 3, 4, 5};
  17. Keypad customKeypad = Keypad(makeKeymap(keys), rowPins, colPins, Rows, Cols);

  18. // 3. Dot Button
  19. int dot = A0;
  20. int dotFlag = 0;
  21. int dotButton = 0;

  22. // 4. Calculator Operators
  23. float num1, num2, fraction;
  24. float total;
  25. char operation, button;

  26. // 5. Loading Setup
  27. char input[16];
  28. int n = 1750;

  29. void setup()
  30. {
  31. // Initialize dot button as input to Arduino
  32. pinMode(dot, INPUT);

  33. // Initialize LCD Size
  34. lcd.begin(16, 2);

  35. // LCD Loading Setup Begin
  36. lcd.clear();
  37. lcd.setCursor(3, 0);
  38. lcd.print("LOADING...");
  39. for (int i = 0; i < 16; i++)
  40. {
  41. lcd.setCursor(i, 1);
  42. lcd.write(255);
  43. delay(50);
  44. }
  45. lcd.clear();
  46. lcd.setCursor(1, 0);
  47. lcd.print("Simple Arduino");
  48. lcd.setCursor(3, 1);
  49. lcd.print("Calculator");
  50. delay(n);
  51. lcd.clear();
  52. lcd.setCursor(0, 0);
  53. lcd.print("Designed By");
  54. lcd.setCursor(2, 1);
  55. lcd.print("Alex Newton");
  56. delay(n);
  57. lcd.clear();
  58. lcd.setCursor(1, 0);
  59. lcd.print("Alex Newton");
  60. lcd.setCursor(2, 1);
  61. lcd.print("how2electronics.com");
  62. delay(n);
  63. lcd.clear();
  64. lcd.setCursor(1, 0);
  65. lcd.print("Alex Newton");
  66. lcd.setCursor(2, 1);
  67. lcd.print("how2electronics.com");
  68. delay(n);
  69. lcd.clear();
  70. // LCD Loading Setup End
  71. }
  72. void loop()
  73. {
  74. // First while loop for num1.
  75. while (1)
  76. {
  77. dotButton = digitalRead(dot);
  78. button = customKeypad.getKey();
  79. if (button == 'C')
  80. {
  81. dotFlag = 0;
  82. num1 = 0;
  83. num2 = 0;
  84. fraction = 0;
  85. total = 0;
  86. operation = 0;
  87. lcd.clear();
  88. }
  89. else if (dotButton == LOW)
  90. {
  91. dotFlag = 1;
  92. }
  93. else if (button >= '0' && button <= '9')
  94. {
  95. if (dotFlag == 0)
  96. {
  97. num1 = num1 * 10 + (button - '0');
  98. lcd.setCursor(0, 0);
  99. lcd.print(num1);
  100. }
  101. else if (dotFlag == 1)
  102. {
  103. fraction = (button - '0');
  104. num1 = num1 + (fraction / 10);
  105. lcd.setCursor(0, 0);
  106. lcd.print(num1);
  107. dotFlag++;
  108. }
  109. else if (dotFlag == 2)
  110. {
  111. fraction = (button - '0');
  112. num1 = num1 + (fraction / 100);
  113. lcd.setCursor(0, 0);
  114. lcd.print(num1);
  115. dotFlag++;
  116. }
  117. }
  118. else if (button == '-' || button == '+' || button == '*' || button == '/')
  119. {
  120. operation = button;
  121. dotFlag = 0;
  122. lcd.setCursor(0, 1);
  123. lcd.print(operation);
  124. break;
  125. }
  126. }
  127. // Second while loop for num2.
  128. while (1)
  129. {
  130. dotButton = digitalRead(dot);
  131. button = customKeypad.getKey();
  132. if (button == 'C')
  133. {
  134. dotFlag = 0;
  135. num1 = 0;
  136. num2 = 0;
  137. fraction = 0;
  138. total = 0;
  139. operation = 0;
  140. lcd.clear();
  141. break;
  142. }
  143. else if (dotButton == LOW)
  144. {
  145. dotFlag = 1;
  146. }
  147. else if (button >= '0' && button <= '9')
  148. {
  149. if (dotFlag == 0)
  150. {
  151. num2 = num2 * 10 + (button - '0');
  152. lcd.setCursor(1, 1);
  153. lcd.print(num2);
  154. }
  155. else if (dotFlag == 1)
  156. {
  157. fraction = (button - '0');
  158. num2 = num2 + (fraction / 10);
  159. lcd.setCursor(1, 1);
  160. lcd.print(num2);
  161. dotFlag++;
  162. }
  163. else if (dotFlag == 2)
  164. {
  165. fraction = (button - '0');
  166. num2 = num2 + (fraction / 100);
  167. lcd.setCursor(1, 1);
  168. lcd.print(num2);
  169. dotFlag++;
  170. }
  171. }
  172. if (button == '=')
  173. {
  174. domath();
  175. break;
  176. }
  177. }
  178. // Third while loop for ensuring C button is executed after while loop 2.
  179. while (1)
  180. {
  181. button = customKeypad.getKey();
  182. if (button == 'C')
  183. {
  184. dotFlag = 0;
  185. num1 = 0;
  186. num2 = 0;
  187. fraction = 0;
  188. total = 0;
  189. operation = 0;
  190. lcd.clear();
  191. break;
  192. }
  193. }
  194. }
  195. void domath()
  196. {
  197. switch (operation)
  198. {
  199. case '+':
  200. total = num1 + num2;
  201. break;
  202. case '-':
  203. total = num1 - num2;
  204. break;
  205. case '/':
  206. total = num1 / num2;
  207. break;
  208. case '*':
  209. total = num1 * num2;
  210. break;
  211. }
  212. lcd.print('=');
  213. if (operation == '/' && num2 == 0)
  214. {
  215. lcd.print("ERROR 0 DIV");
  216. }
  217. else
  218. {
  219. lcd.print(total);
  220. }
  221. }
复制代码

跳转到指定楼层
feishixi
发表于: 2023-5-14 18:41:44 | 显示全部楼层

为什么没显示
回复

使用道具 举报

llllllyyyyyy
发表于: 2023-6-12 11:39:38 | 显示全部楼层

我也按照这个方法 不显示数字
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题 705 | 回复: 1492



手机版|

GMT+8, 2024-11-8 02:38 , Processed in 0.040818 second(s), 6 queries , Gzip On, MemCache On. Powered by Discuz! X3.5

YiBoard一板网 © 2015-2022 地址:河北省石家庄市长安区高营大街 ( 冀ICP备18020117号 )

快速回复 返回顶部 返回列表