Introduction

In this article, I have explained about creating a simple calculator with Arduino Mega 2560. It can be used to calculate for Addition, Multiplication, Division, and Subtraction. The result will be displayed in the LCD.
Parts Of Lists
  • Arduino Mega 2560
  • LEDs
  • Push Button
  • Potentiometer
  • LCD Display
  • Bread Board
  • Hookup Wires
Potentiometer
  • It can have three terminals.
  • It can have sliding or rotating contact that forms an adjustable voltage divider.
  • It acts as a variable resistor (or) rheostat.
Figure 1: Potentiometer
LCD Display
  • LCD is Liquid Crystal Display (LCD).
  • It can have flat-panel or electronic display
  • It is used in hospital, showrooms, buses, railway stations, airports, etc.
Figure 2: LCD Display
Push Button
  • The push-button connects the two points.
  • The first pin in the push button is connected to 5Vc.
  • The second pin in the push button is connected to gnd.
Figure 3: Push Button
Connection
Step 1: Connection from Arduino Mega 2560 to LCD Display.
  • Fix the LCD in the 16 to 2 in the bread board.
  • The 16 pin to the Gnd
  • The 15 pin to the Vcc
  • The 14 pin to the Digital pin 12
  • The 13 pin to the Digital pin 11
  • The 12 pin to the Digital pin 05
  • The 11 pin to the Digital pin 04
  • The 01 pin to the Gnd.
  • The 02 pin to the Vcc.
  • The 03 pin to the potentiometer.
  • The 04 pin to the Digital pin 03.
  • The 05 pin to the Gnd.
  • The 06 pin to the Digital pin 02.
Figure 4: LCD Display connection
Step 2: Connection from Push Button to ArduinoMega2560
Push-button 1:
  • The first pin in the push button is connected to 5Vc and 12 of board
  • The second pin in the push button is connected to gnd.
Push-button 2:
  • The first pin in the push button is connected to 5Vc and 13 of board
  • The second pin in the push button is connected to gnd.
Push-button 3:
  • The first pin in the push button is connected to 5Vc and 14 of board
  • The second pin in the push button is connected to gnd.
Push-button 4:
  • The first pin in the push button is connected to 5Vc and 15 of board
  • The second pin in the push button is connected to gnd.
Push-button 5:
  • The first pin in the push button is connected to 5Vc and 16 of board
  • The second pin in the push button is connected to gnd.
Push-button 6:
  • The first pin in the push button is connected to 5Vc and 17 of board
  • The second pin in the push button is connected to gnd.
Push-button 7:
  • The first pin in the push button is connected to 5Vc and 18 of board
  • The second pin in the push button is connected to gnd.
Push-button 8:
  • The first pin in the push button is connected to 5Vc and 19 of board
  • The second pin in the push button is connected to gnd.
Push-button 9:
  • The first pin in the push button is connected to 5Vc and 20 of board
  • The second pin in the push button is connected to gnd.
Push-button 10:
  • The first pin in the push button is connected to 5Vc and 21 of board
  • The second pin in the push button is connected to gnd.
Push-button 11:
  • The first pin in the push button is connected to 5Vc and 22 of board
  • The second pin in the push button is connected to gnd.
Push-button 12:
  • The first pin in the push button is connected to 5Vc and 23 of board
  • The second pin in the push button is connected to gnd.
Programming:
  1. // include the library code:
  2. #include <LiquidCrystal.h>
  3. #define key0 12
  4. #define key1 13
  5. #define key2 14
  6. #define key3 15
  7. #define key4 16
  8. #define key5 17
  9. #define key6 18
  10. #define key7 19
  11. #define key8 20
  12. #define key9 21
  13. #define DEL 22
  14. #define ENTER 23
  15. #define MAXVALUE 99999
  16. // initialize the library with the numbers of the interface pins
  17. LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
  18. long number_1 = 0;
  19. long number_2 = 0;
  20. int control = 0;
  21. boolean error = false;
  22. long result = 0;
  23. float result_div = 0.0;
  24. void insertNumb () {
  25. for (int i = 0; i <= 9; i++) {
  26. //whithout this delay, the selection button pressed could become the first digit
  27. delay (200);
  28. lcd.clear();
  29. lcd.setCursor(0, 0);
  30. lcd.print("FIRST NUMBER");
  31. control = readKey ();
  32. //statement needed to catch the first digit
  33. if ( i == 0 ) {
  34. //statement needed to check if the pressed button is the special key OK
  35. //if yes "i" becomes 5 and the algorithm goes to the next for loop
  36. if ( control == 11 ) {
  37. i = 5;
  38. }
  39. //statement needed to check if the pressed button is the special key DEL
  40. //if yes "i" becomes 10 and the algorithm goes from both for loops
  41. //resetting the calculator
  42. else if ( control == 10) {
  43. i = 10;
  44. error = true;
  45. lcd.clear();
  46. lcd.setCursor(0, 0);
  47. lcd.print("CANCELLED!");
  48. delay(2000);
  49. }
  50. else {
  51. //stores the most significant digit
  52. number_1 = control;
  53. lcd.clear();
  54. lcd.setCursor(0, 1);
  55. lcd.print(control);
  56. delay(500);
  57. }
  58. }
  59. //statement needed to catch the remaining four digits
  60. //the following conditional statements have the same purpose of the ones described above
  61. else {
  62. if ( control == 11 ) {
  63. i = 5;
  64. }
  65. else if ( control == 10) {
  66. i = 10;
  67. error = true;
  68. lcd.clear();
  69. lcd.setCursor(0, 0);
  70. lcd.print("CANCELLED!");
  71. delay(2000);
  72. }
  73. else {
  74. number_1 = (number_1 * 10) + control;
  75. //check if number_1 is negative or is greater than MAXVALUE printing an error if yes
  76. if ((number_1 < 0) || (number_1 > MAXVALUE)) {
  77. error = true;
  78. i = 10;
  79. lcd.clear();
  80. lcd.setCursor(0, 0);
  81. lcd.print("ERROR!");
  82. lcd.setCursor(0, 1);
  83. lcd.print("MAX VALUE " + (String)MAXVALUE);
  84. delay(2000);
  85. }
  86. else {
  87. lcd.clear();
  88. lcd.setCursor(0, 1);
  89. lcd.print(control);
  90. lcd.setCursor(0, 0);
  91. lcd.print(number_1);
  92. delay(500);
  93. }
  94. }
  95. }
  96. if (i >= 4 && i <= 9) {
  97. //the following for loop catches the digits of the second number
  98. //and basically does the same thing as the previous one
  99. for (int j = 0; j <= 4; j++) {
  100. delay (200);
  101. lcd.clear();
  102. lcd.setCursor(0, 0);
  103. lcd.print("SECOND NUMBER");
  104. control = readKey ();
  105. i++;
  106. if ( j == 0 ) {
  107. if ( control == 11 ) {
  108. i = 10;
  109. j = 5;
  110. }
  111. else if ( control == 10 ) {
  112. error = true;
  113. j = 5;
  114. i = 10;
  115. lcd.clear();
  116. lcd.setCursor(0, 0);
  117. lcd.print("CANCELLED!");
  118. delay(2000);
  119. }
  120. else {
  121. number_2 = control;
  122. lcd.clear();
  123. lcd.setCursor(0, 1);
  124. lcd.print(control);
  125. delay(500);
  126. }
  127. }
  128. else {
  129. if ( control == 11 ) {
  130. j = 5;
  131. i = 10;
  132. }
  133. else if ( control == 10 ) {
  134. error = true;
  135. j = 5;
  136. i = 10;
  137. lcd.clear();
  138. lcd.setCursor(0, 0);
  139. lcd.print("CANCELLED!");
  140. delay(2000);
  141. }
  142. else {
  143. number_2 = (number_2 * 10) + control;
  144. //check if number_2 is negative or is greater than MAXVALUE printing an error if yes
  145. if ((number_2 < 0) || (number_2 > MAXVALUE)) {
  146. error = true;
  147. j = 5;
  148. i = 10;
  149. lcd.clear();
  150. lcd.setCursor(0, 0);
  151. lcd.print("ERROR!");
  152. lcd.setCursor(0, 1);
  153. lcd.print("MAX VALUE " + (String)MAXVALUE);
  154. delay(2000);
  155. }
  156. else {
  157. lcd.clear();
  158. lcd.setCursor(0, 1);
  159. lcd.print(control);
  160. lcd.setCursor(0, 0);
  161. lcd.print(number_2);
  162. delay(500);
  163. }
  164. }
  165. }
  166. }
  167. }
  168. }
  169. }
  170. void wait () {
  171. while (digitalRead(key0) == LOW &&
  172. digitalRead(key1) == LOW &&
  173. digitalRead(key2) == LOW &&
  174. digitalRead(key3) == LOW &&
  175. digitalRead(key4) == LOW &&
  176. digitalRead(key5) == LOW &&
  177. digitalRead(key6) == LOW &&
  178. digitalRead(key7) == LOW &&
  179. digitalRead(key8) == LOW &&
  180. digitalRead(key9) == LOW &&
  181. digitalRead(DEL) == LOW &&
  182. digitalRead(ENTER) == LOW
  183. );
  184. }
  185. void setup() {
  186. //initializes push-buttons
  187. pinMode(key0, INPUT);
  188. pinMode(key1, INPUT);
  189. pinMode(key2, INPUT);
  190. pinMode(key3, INPUT);
  191. pinMode(key4, INPUT);
  192. pinMode(key5, INPUT);
  193. pinMode(key6, INPUT);
  194. pinMode(key7, INPUT);
  195. pinMode(key8, INPUT);
  196. pinMode(key9, INPUT);
  197. pinMode(DEL, INPUT);
  198. pinMode(ENTER, INPUT);
  199. // set up the LCD's number of columns and rows:
  200. lcd.begin(16, 2);
  201. }
  202. int readKey () {
  203. wait();
  204. int operand = 0;
  205. if (digitalRead(key0) == HIGH) {
  206. operand = 0;
  207. }
  208. else if (digitalRead(key1) == HIGH) {
  209. operand = 1;
  210. }
  211. else if (digitalRead(key2) == HIGH) {
  212. operand = 2;
  213. }
  214. else if (digitalRead(key3) == HIGH) {
  215. operand = 3;
  216. }
  217. else if (digitalRead(key4) == HIGH) {
  218. operand = 4;
  219. }
  220. else if (digitalRead(key5) == HIGH) {
  221. operand = 5;
  222. }
  223. else if (digitalRead(key6) == HIGH) {
  224. operand = 6;
  225. }
  226. else if (digitalRead(key7) == HIGH) {
  227. operand = 7;
  228. }
  229. else if (digitalRead(key8) == HIGH) {
  230. operand = 8;
  231. }
  232. else if (digitalRead(key9) == HIGH) {
  233. operand = 9;
  234. }
  235. else if (digitalRead(DEL) == HIGH) {
  236. operand = 10;
  237. }
  238. else if (digitalRead(ENTER) == HIGH) {
  239. operand = 11;
  240. }
  241. delay(100);
  242. return operand;
  243. }
  244. //SUM function
  245. long sum (long number_1, long number_2) {
  246. return number_1 + number_2;
  247. }
  248. //SUBtraction function
  249. long subtraction (long number_1, long number_2) {
  250. return number_1 - number_2;
  251. }
  252. //MULtiplication function
  253. long multiplication (long number_1, long number_2) {
  254. return number_1 * number_2;
  255. }
  256. //DIVsion FUNCTION
  257. float division (long number_1, long number_2) {
  258. return (float)number_1 / (float)number_2;
  259. }
  260. void loop() {
  261. number_1 = 0;
  262. number_2 = 0;
  263. delay (200);
  264. //asks which calculation you want to do
  265. lcd.clear();
  266. lcd.setCursor(0, 0);
  267. lcd.print("SUM,SUB,MUL,DIV");
  268. lcd.setCursor(0, 1);
  269. lcd.print("0 ,1 ,2 ,3 ");
  270. //waits into the following loop till the user press a key
  271. wait();
  272. //"operation" is the variable needed to control the flow of the sketch
  273. int operation = readKey();
  274. //SUM
  275. if (operation == 0) {
  276. insertNumb();
  277. if (error == true) {
  278. lcd.clear();
  279. lcd.setCursor(0, 0);
  280. lcd.print("DEL TO CONTINUE");
  281. }
  282. else {
  283. lcd.clear();
  284. lcd.setCursor(0, 0);
  285. lcd.print("DEL TO CONTINUE");
  286. lcd.setCursor(0, 1);
  287. lcd.print("SUM = ");
  288. lcd.setCursor(7, 1);
  289. result = sum(number_1, number_2);
  290. if (result < MAXVALUE) {
  291. lcd.print(result);
  292. } else {
  293. lcd.clear();
  294. lcd.setCursor(0, 0);
  295. lcd.print("ERROR!");
  296. lcd.setCursor(0, 1);
  297. lcd.print("MAX VALUE " + (String)MAXVALUE);
  298. delay(4000);
  299. lcd.clear();
  300. lcd.setCursor(0, 0);
  301. lcd.print("DEL TO CONTINUE");
  302. }
  303. }
  304. while (readKey() != 10);
  305. }
  306. else if (operation == 1) {
  307. insertNumb();
  308. if (error == true) {
  309. lcd.clear();
  310. lcd.setCursor(0, 0);
  311. lcd.print("DEL TO CONTINUE");
  312. } else {
  313. lcd.clear();
  314. lcd.setCursor(0, 0);
  315. lcd.print("DEL TO CONTINUE");
  316. lcd.setCursor(0, 1);
  317. lcd.print("SUB = ");
  318. lcd.setCursor(7, 1);
  319. result = subtraction(number_1, number_2);
  320. if (result < MAXVALUE) {
  321. lcd.print(result);
  322. } else {
  323. lcd.clear();
  324. lcd.setCursor(0, 0);
  325. lcd.print("ERROR!");
  326. lcd.setCursor(0, 1);
  327. lcd.print("MAX VALUE " + (String)MAXVALUE);
  328. delay(4000);
  329. lcd.clear();
  330. lcd.setCursor(0, 0);
  331. lcd.print("DEL TO CONTINUE");
  332. }
  333. }
  334. while (readKey() != 10);
  335. }
  336. else if (operation == 2) {
  337. insertNumb();
  338. if (error == true) {
  339. lcd.clear();
  340. lcd.setCursor(0, 0);
  341. lcd.print("DEL TO CONTINUE");
  342. } else {
  343. lcd.clear();
  344. lcd.setCursor(0, 0);
  345. lcd.print("DEL TO CONTINUE");
  346. lcd.setCursor(0, 1);
  347. lcd.print("MUL = ");
  348. lcd.setCursor(7, 1);
  349. result = multiplication(number_1, number_2);
  350. if (result < MAXVALUE) {
  351. lcd.print(result);
  352. } else {
  353. lcd.clear();
  354. lcd.setCursor(0, 0);
  355. lcd.print("ERROR!");
  356. lcd.setCursor(0, 1);
  357. lcd.print("MAX VALUE " + (String)MAXVALUE);
  358. delay(4000);
  359. lcd.clear();
  360. lcd.setCursor(0, 0);
  361. lcd.print("DEL TO CONTINUE");
  362. }
  363. }
  364. while (readKey() != 10);
  365. }
  366. else if (operation == 3) {
  367. insertNumb();
  368. if (error == true) {
  369. lcd.clear();
  370. lcd.setCursor(0, 0);
  371. lcd.print("DEL TO CONTINUE");
  372. } else {
  373. lcd.clear();
  374. lcd.setCursor(0, 0);
  375. lcd.print("DEL TO CONTINUE");
  376. lcd.setCursor(0, 1);
  377. lcd.print("DIV = ");
  378. lcd.setCursor(7, 1);
  379. result_div = division(number_1, number_2);
  380. if (result < MAXVALUE) {
  381. lcd.print(result_div, 7);
  382. } else {
  383. lcd.clear();
  384. lcd.setCursor(0, 0);
  385. lcd.print("ERROR!");
  386. lcd.setCursor(0, 1);
  387. lcd.print("MAX VALUE " + (String)MAXVALUE);
  388. delay(4000);
  389. lcd.clear();
  390. lcd.setCursor(0, 0);
  391. lcd.print("DEL TO CONTINUE");
  392. }
  393. }
  394. while (readKey() != 10);
  395. } else {
  396. lcd.clear();
  397. lcd.setCursor(0, 0);
  398. lcd.print("ERROR!");
  399. lcd.setCursor(0, 1);
  400. lcd.print("PRESS 0,1,2 or 3");
  401. delay(2000);
  402. }
  403. int control = 0;
  404. }
Explanation
  • This calculator can be used to calculate the for loop() function. It can be used to display one by one condition i.e addition/ subtraction/ multiplexing/ division.
  • The result can be displayed in the LCD display screen. If we made any mistake, press del to continue.
Output
Figure 5: Output