Me
ITALY • + Follow
Edit Project
Description
MyCalculator
I created a small math calculator to do some quick calculations. It works through a display that displays the data inputs from the keypad and the outputs calculated by the program. The calculator can do addition, subtraction, multiplication and division.
Code
Internal Code
C/C++
#include <Keypad.h>
#include <Wire.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(13, 12, 11, 10, 9, 8);
long firstNum = 0;
long secondNum = 0;
double total = 0;
char customKey;
const byte ROWS = 4;
const byte COLS = 4;
//create a table in code
char keys[ROWS][COLS] = {
{'1','2','3','+'},{'4','5','6','-'},{'7','8','9','*'},{'C','0','=','/'}
};
//connect rows to tab
byte rowPins[ROWS] = {7,6,5,4};
//connect columns to the board
byte colPins[COLS] = {3,2,1,0};
//instance of the NewKeypad class
Keypad customKeypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS);
void setup()
{
lcd.begin(16, 2); //create despla
for(int i=0;i<=3;i++);
lcd.setCursor(0,5);
lcd.print("Calculator");
delay(2000);
lcd.clear();
//explain the table
lcd.setCursor(0, 0);
lcd.print("A -> +");
lcd.setCursor(9, 0);
lcd.print("B -> -");
lcd.setCursor(0, 5);
lcd.print("C -> *");
lcd.setCursor(9, 5);
lcd.print("D -> /");
delay(3000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("# -> =");
lcd.setCursor(0, 5);
lcd.print("* -> Clear");
delay(3000);
lcd.clear();
}
void loop()
{
customKey = customKeypad.getKey();
switch(customKey)
{
case '0' ... '9':
lcd.setCursor(0,0);
firstNum = firstNum * 10 + (customKey - '0');
lcd.print(firstNum);
break;
case '+':
if (total != 0)firstNum = total; //ipnut first number
lcd.setCursor(0,1);
lcd.print("+"); //print sign
secondNum = SecondInput();
total = firstNum + secondNum; //calculate
lcd.setCursor(0,3);
lcd.clear();
lcd.print(total); //print result
firstNum = 0, secondNum = 0; //set the first and second numbers zero for a successive calculation
break;
case '-':
if (total != 0)firstNum = total;
lcd.setCursor(0,1);
lcd.print("-");
secondNum = SecondInput();
total = firstNum - secondNum;
lcd.setCursor(0,3);
lcd.clear();
lcd.print(total);
firstNum = 0, secondNum = 0;
break;
case '*':
if (total != 0)firstNum = total;
lcd.setCursor(0,1);
lcd.print("*");
secondNum = SecondInput();
total = firstNum * secondNum;
lcd.setCursor(0,3);
lcd.clear();
lcd.print(total);
firstNum = 0, secondNum = 0;
break;
case '/':
if (total != 0)firstNum = total;
lcd.setCursor(0,1);
lcd.print("/");
secondNum = SecondInput();
lcd.setCursor(0,3);
lcd.clear();
if (secondNum == 0){
lcd.print("Invalid");
}else{
total = (float)firstNum / (float)secondNum;
}
lcd.print(total);
firstNum = 0, secondNum = 0;
break;
case 'C':
total = 0;
lcd.clear();
lcd.print("0.00");
break;
}
}
long SecondInput()
{
while( 1 )
{
customKey = customKeypad.getKey();
if(customKey >= '0' && customKey <= '9')
{
secondNum = secondNum * 10 + (customKey - '0');
lcd.setCursor(0,2);
lcd.print(secondNum);
}
if(customKey == '=') break;
}
return secondNum;
}
Oct 07,2022
689 views
MyCalculator
I create a small math calculator
689
1
0
Published: Oct 07,2022
Purchase
Donation Received ($)
PCBWay Donate 10% cost To Author
Copy this HTML into your page to embed a link to order this shared project
Copy
Under the
Attribution-NonCommercial-NoDerivs (CC BY-NC-ND)
License.
- Comments(0)
- Likes(1)
You can only upload 1 files in total. Each file cannot exceed 2MB. Supports JPG, JPEG, GIF, PNG, BMP
0 / 10000
Remove
It looks like you have not written anything. Please add a comment and try again.
View More
- Bernd Desens Mar 06,2023
View More
VOTING
0 votes
- 0 USER VOTES
0.00
- YOUR VOTE 0.00 0.00
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
Design
1/4
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
Usability
2/4
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
Creativity
3/4
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
Content
4/4
More by Me
You may also like
-
-
-
3D printed Enclosure Backplate for Riden RD60xx power supplies
80 1 1 -
-
-
-
Sega Master System RGB Encoder Switcher Z80 QSB v1.2
71 0 0 -
18650 2S2P Battery Charger, Protection and 5V Output Board
107 0 0 -
High Precision Thermal Imager + Infrared Thermometer | OpenTemp
523 0 7 -
Sony PlayStation Multi Output Frequency Oscillator (MOFO) v1
147 0 2 -