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 Substraction. 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 a adjustable voltage divider.
- It act 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 station, airport, 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 are 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:
-
- #include <LiquidCrystal.h>
-
- #define key0 12
- #define key1 13
- #define key2 14
- #define key3 15
- #define key4 16
- #define key5 17
- #define key6 18
- #define key7 19
- #define key8 20
- #define key9 21
- #define DEL 22
- #define ENTER 23
- #define MAXVALUE 99999
-
-
- LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
-
- long number_1 = 0;
- long number_2 = 0;
- int control = 0;
- boolean error = false;
- long result = 0;
- float result_div = 0.0;
-
- void insertNumb () {
- for (int i = 0; i <= 9; i++) {
-
- delay (200);
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("FIRST NUMBER");
- control = readKey ();
-
- if ( i == 0 ) {
-
-
- if ( control == 11 ) {
- i = 5;
- }
-
-
-
- else if ( control == 10) {
- i = 10;
- error = true;
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("CANCELLED!");
- delay(2000);
- }
- else {
-
- number_1 = control;
- lcd.clear();
- lcd.setCursor(0, 1);
- lcd.print(control);
- delay(500);
- }
- }
-
-
- else {
- if ( control == 11 ) {
- i = 5;
- }
- else if ( control == 10) {
- i = 10;
- error = true;
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("CANCELLED!");
- delay(2000);
- }
- else {
- number_1 = (number_1 * 10) + control;
-
- if ((number_1 < 0) || (number_1 > MAXVALUE)) {
- error = true;
- i = 10;
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("ERROR!");
- lcd.setCursor(0, 1);
- lcd.print("MAX VALUE " + (String)MAXVALUE);
- delay(2000);
- }
- else {
- lcd.clear();
- lcd.setCursor(0, 1);
- lcd.print(control);
- lcd.setCursor(0, 0);
- lcd.print(number_1);
- delay(500);
- }
- }
- }
- if (i >= 4 && i <= 9) {
-
-
- for (int j = 0; j <= 4; j++) {
- delay (200);
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("SECOND NUMBER");
- control = readKey ();
- i++;
- if ( j == 0 ) {
- if ( control == 11 ) {
- i = 10;
- j = 5;
- }
- else if ( control == 10 ) {
- error = true;
- j = 5;
- i = 10;
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("CANCELLED!");
- delay(2000);
- }
- else {
- number_2 = control;
- lcd.clear();
- lcd.setCursor(0, 1);
- lcd.print(control);
- delay(500);
- }
- }
- else {
- if ( control == 11 ) {
- j = 5;
- i = 10;
- }
- else if ( control == 10 ) {
- error = true;
- j = 5;
- i = 10;
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("CANCELLED!");
- delay(2000);
- }
- else {
- number_2 = (number_2 * 10) + control;
-
- if ((number_2 < 0) || (number_2 > MAXVALUE)) {
- error = true;
- j = 5;
- i = 10;
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("ERROR!");
- lcd.setCursor(0, 1);
- lcd.print("MAX VALUE " + (String)MAXVALUE);
- delay(2000);
- }
- else {
- lcd.clear();
- lcd.setCursor(0, 1);
- lcd.print(control);
- lcd.setCursor(0, 0);
- lcd.print(number_2);
- delay(500);
- }
- }
- }
- }
- }
- }
-
- }
-
- void wait () {
- while (digitalRead(key0) == LOW &&
- digitalRead(key1) == LOW &&
- digitalRead(key2) == LOW &&
- digitalRead(key3) == LOW &&
- digitalRead(key4) == LOW &&
- digitalRead(key5) == LOW &&
- digitalRead(key6) == LOW &&
- digitalRead(key7) == LOW &&
- digitalRead(key8) == LOW &&
- digitalRead(key9) == LOW &&
- digitalRead(DEL) == LOW &&
- digitalRead(ENTER) == LOW
- );
- }
-
- void setup() {
-
- pinMode(key0, INPUT);
- pinMode(key1, INPUT);
- pinMode(key2, INPUT);
- pinMode(key3, INPUT);
- pinMode(key4, INPUT);
- pinMode(key5, INPUT);
- pinMode(key6, INPUT);
- pinMode(key7, INPUT);
- pinMode(key8, INPUT);
- pinMode(key9, INPUT);
- pinMode(DEL, INPUT);
- pinMode(ENTER, INPUT);
-
- lcd.begin(16, 2);
- }
-
- int readKey () {
- wait();
- int operand = 0;
- if (digitalRead(key0) == HIGH) {
- operand = 0;
- }
- else if (digitalRead(key1) == HIGH) {
- operand = 1;
- }
- else if (digitalRead(key2) == HIGH) {
- operand = 2;
- }
- else if (digitalRead(key3) == HIGH) {
- operand = 3;
- }
- else if (digitalRead(key4) == HIGH) {
- operand = 4;
- }
- else if (digitalRead(key5) == HIGH) {
- operand = 5;
- }
- else if (digitalRead(key6) == HIGH) {
- operand = 6;
- }
- else if (digitalRead(key7) == HIGH) {
- operand = 7;
- }
- else if (digitalRead(key8) == HIGH) {
- operand = 8;
- }
- else if (digitalRead(key9) == HIGH) {
- operand = 9;
- }
- else if (digitalRead(DEL) == HIGH) {
- operand = 10;
- }
- else if (digitalRead(ENTER) == HIGH) {
- operand = 11;
- }
- delay(100);
- return operand;
- }
-
-
- long sum (long number_1, long number_2) {
- return number_1 + number_2;
- }
-
-
- long subtraction (long number_1, long number_2) {
- return number_1 - number_2;
- }
-
-
- long multiplication (long number_1, long number_2) {
- return number_1 * number_2;
- }
-
-
- float division (long number_1, long number_2) {
- return (float)number_1 / (float)number_2;
- }
-
- void loop() {
- number_1 = 0;
- number_2 = 0;
- delay (200);
-
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("SUM,SUB,MUL,DIV");
- lcd.setCursor(0, 1);
- lcd.print("0 ,1 ,2 ,3 ");
-
- wait();
-
- int operation = readKey();
-
- if (operation == 0) {
- insertNumb();
- if (error == true) {
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("DEL TO CONTINUE");
- }
- else {
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("DEL TO CONTINUE");
- lcd.setCursor(0, 1);
- lcd.print("SUM = ");
- lcd.setCursor(7, 1);
- result = sum(number_1, number_2);
- if (result < MAXVALUE) {
- lcd.print(result);
- } else {
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("ERROR!");
- lcd.setCursor(0, 1);
- lcd.print("MAX VALUE " + (String)MAXVALUE);
- delay(4000);
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("DEL TO CONTINUE");
- }
- }
- while (readKey() != 10);
- }
- else if (operation == 1) {
- insertNumb();
- if (error == true) {
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("DEL TO CONTINUE");
- } else {
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("DEL TO CONTINUE");
- lcd.setCursor(0, 1);
- lcd.print("SUB = ");
- lcd.setCursor(7, 1);
- result = subtraction(number_1, number_2);
- if (result < MAXVALUE) {
- lcd.print(result);
- } else {
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("ERROR!");
- lcd.setCursor(0, 1);
- lcd.print("MAX VALUE " + (String)MAXVALUE);
- delay(4000);
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("DEL TO CONTINUE");
- }
- }
- while (readKey() != 10);
- }
- else if (operation == 2) {
- insertNumb();
- if (error == true) {
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("DEL TO CONTINUE");
- } else {
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("DEL TO CONTINUE");
- lcd.setCursor(0, 1);
- lcd.print("MUL = ");
- lcd.setCursor(7, 1);
- result = multiplication(number_1, number_2);
- if (result < MAXVALUE) {
- lcd.print(result);
- } else {
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("ERROR!");
- lcd.setCursor(0, 1);
- lcd.print("MAX VALUE " + (String)MAXVALUE);
- delay(4000);
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("DEL TO CONTINUE");
- }
- }
- while (readKey() != 10);
- }
- else if (operation == 3) {
- insertNumb();
- if (error == true) {
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("DEL TO CONTINUE");
- } else {
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("DEL TO CONTINUE");
- lcd.setCursor(0, 1);
- lcd.print("DIV = ");
- lcd.setCursor(7, 1);
- result_div = division(number_1, number_2);
- if (result < MAXVALUE) {
- lcd.print(result_div, 7);
- } else {
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("ERROR!");
- lcd.setCursor(0, 1);
- lcd.print("MAX VALUE " + (String)MAXVALUE);
- delay(4000);
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("DEL TO CONTINUE");
- }
- }
- while (readKey() != 10);
- } else {
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("ERROR!");
- lcd.setCursor(0, 1);
- lcd.print("PRESS 0,1,2 or 3");
- delay(2000);
- }
- int control = 0;
-
- }
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