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
729 views
MyCalculator
I create a small math calculator
729
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)
Upload photo
You can only upload 5 files in total. Each file cannot exceed 2MB. Supports JPG, JPEG, GIF, PNG, BMP
0 / 10000
It looks like you have not written anything. Please add a comment and try again.
You can upload up to 5 images!
Image size should not exceed 2MB!
File format not supported!
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
-
Build a Walking Robot: Theo Jansen Style 3D Printed Octopod
131 0 3 -
-
-
kmMiniSchield MIDI I/O - IN/OUT/THROUGH MIDI extension for kmMidiMini
141 0 0 -
DIY Laser Power Meter with Arduino
216 0 2 -
-
-
Box & Bolt, 3D Printed Cardboard Crafting Tools
187 0 2