Create A Simple Calculator With Ardunio Mega 2560

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