Activity 3. Simple calculator
This activity uses a keypad and a LCD 16x2. The user can calculate the 4 basic mathematical operations between two single-digit positive numbers. (50 minutes)
Activity 3
The calculator:
- With the “A” key, makes the addition
- With the “B” key, makes the subtraction
- With the “C” key, makes the multiplication
- With the “D” key, makes the division
The "#" key resets the result and a new calculation cycle begins.
Step 1. Draw the circuit in Tinkercad.
Step 2. Study the code and write it on the microcontroller:
/* Simple calculator
Circuit Connections: **LCD Ground => Gnd
Power => Vcc
Contrast => Potentiometer
RS => PIN_A5
RW => Gnd
E => PIN_A4
DB0 => Gnd
DB1 => Gnd
DB2 => Gnd
DB3 => Gnd
DB4 => PIN_2
DB5 => PIN_3
DB6 => PIN_4
DB7 => PIN_5
LED Anode => Vcc
LED Cathode => Resistor 220Ω => Gnd**Potentiometer1 Terminal 1 => Gnd Wiper => LCD_Contrast Terminal 2 => Vcc **Keypad PIN_5 => Column4 PIN_6 => Column3 PIN_7 => Column2 PIN_8 => Column1 PIN_9 => Row4 PIN_10 => Row3 PIN_11 => Row2 PIN_12 => Row1 */
//include the library #include <LiquidCrystal.h> #define RS A5 //give the name "RS" to PIN_A5 #define EN A4 //give the name "EN" to PIN_A4 #define DB4 0 //give the name "DB4" to PIN_0 #define DB5 1 //give the name "DB5" to PIN_1 #define DB6 2 //give the name "DB6" to PIN_2 #define DB7 3 //give the name "DB7" to PIN_3
//configure the library with Arduino Uno - LCD interface
LiquidCrystal lcd(RS, EN, DB4, DB5, DB6, DB7);
//inlcude the library
#include <Keypad.h>
const byte cols = 4; //four columns
const byte rows = 4; //four rows
//keypad output
char keys[rows][cols] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
//configure the Arduino Uno - Keypad interface
byte row_pins[] = {12,11,10,9}; //pins connect to the rows
byte col_pins[] = {8, 7, 6, 5}; //pins connect to the columns
Keypad keypad = Keypad(makeKeymap(keys), row_pins, col_pins, rows, cols);
//variable to save keypad's charachters
char key;
//variable for the first number
int num1;
//variable for the second number
int num2;
//variable for the operation
char operation;
//variable for the result
float result;
//The setup() function initializes and sets the initial values
//It will only run once after each power up or reset
void setup(){
//configure the LCD's columns and rows
lcd.begin(16, 2);
}
//loops consecutively
void loop(){
//the first number
do{ //wait until a key is pressed
key=keypad.waitForKey();
//call the "convert_to_number"
num1=convert_to_number(key);
}
while(num1>9);
//print the first number
lcd.print(num1);
//wait 0.2s
delay(200);
//the operation
do{
//wait until a key is pressed
operation=keypad.waitForKey();
}
while(operation!='A' && operation!='B' && operation!='C' && operation!='D');
//print the operation
if(operation=='A'){
lcd.print(" + ");
}
else if(operation=='B'){
lcd.print(" - ");
}
else if(operation=='C'){
lcd.print(" * ");
}
else if(operation=='D'){
lcd.print(" / ");
}
//wait 0.2s
delay(200);
//the second number
do{
//wait until a key is pressed
key=keypad.waitForKey();
//call the "convert_to_number"
num2=convert_to_number(key);
}
while(num2>9);
//print the second number
lcd.print(num2);
//wait 0.2s
delay(200);
//calculate
if(operation=='A'){
result=num1+num2;
}
else if(operation=='B'){
result=num1-num2;
}
else if(operation=='C'){
result=num1*num2;
}
else if(operation=='D'){
result=float(num1)/num2;
}
//print the result
lcd.print(" = ");
lcd.print(result);
//wait until the "#" is pressed
do{
key=keypad.waitForKey();
}
while(key!='#');
//clear the LCD
lcd.clear();
}
//this function converts
//keypad's character to integer
int convert_to_number(char c){
if(c=='0'){
return 0;
}
else if(c=='1'){
return 1;
}
else if(c=='2'){
return 2;
}
else if(c=='3'){
return 3;
}
else if(c=='4'){
return 4;
}
else if(c=='5'){
return 5;
}
else if(c=='6'){
return 6;
}
else if(c=='7'){
return 7;
}
else if(c=='8'){
return 8;
}
else if(c=='9'){
return 9;
}
else if(c=='A'){
return 10;
}
else if(c=='B'){
return 11;
}
else if(c=='C'){
return 12;
}
else if(c=='D'){
return 13;
}
else if(c=='*'){
return 14;
}
else {// if(c=='#'){
return 15;
}
}
Step
3. Run
the simulation and check the correct operation of the circuit
Step 4. Suggested modifications and discussion:
- Add an operation. Specifically, the power can be calculated between two single-digit positive numbers
Tip. link