Name: Ong Aaron Course/Section: BSME III-GM Set B

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Name: ONG AARON

Course/Section: BSME III-GM

SET B

Activity 3 – Arduino Calculator. Create a simple calculator using the keypad. Using letters ABC and D as
basic operators A = Addition, B = Subtraction C = Multiplication and D = Division. A sample structure is
provided. If an error is encountered, encircle the statement to be corrected and write the correction on
side.

1. Write your pseudo code here:


Arduino Calculator using Keypad matrix 4x4:
Step 1: Connect the keypad matrix to the corresponding digital pins from the arduino board
Step 2: Include the Keypad library to the set of codes
Step 3: Write data types and variables
Step 4: Initialize the keypad setup codes
Step 5: Define the character keys of keypad rows and columns
Step 6: Make sure to specify the arrangement of columns and rows also the connection to pins
Step 7: Setup the serial monitor by adjusting the baud rate to 9600
Step 8: The loop will start and the character values will convert to numerical values
Step 9: The buttons of keypad switch will be activated
Step 10: The case will serve as a control flow, the numbers contains by the keypad is 0-9
wherein will stored in memory according to the priority of data entry
Step 11: For case of addition, asking for the first inputs then followed by printing plus key and
ask to enter second number the result is total number = first + second. Print the total number
and setting back to zero, break.
Step 12: For case of subtraction, asking for the first inputs then followed by printing minus key
and ask to enter second number the result is total number = first - second. Print the total
number and setting back to zero, break.
Step 13: For case of multiplication, asking for the first inputs then followed by printing multiply
key and ask to enter second number the result is total number = first * second. Print the total
number and setting back to zero, break.
Step 14: For case of division, asking for the first inputs then followed by printing divide key and
ask to enter second number the result is total number = first / second. Print the total number
and setting back to zero, break.
Step 15: For case C it will clear out or remove the memory from the previous total answer hence
the total will set back to zero, break
Step 16: Store second number function and return second number Step 17: Restart the loop
2. Write your codes here:

/*Coding Arduino Calculator using Keypad Matrix*/

#include <Keypad.h> {'C' , '0', '=', '/'}

};

long first = 0;

long second = 0; byte rowPins[ROWS]= {9,8,7,6};

double total= 0; byte colPins[COLS]= {5,4,3,2};

char customKey; Keypad customKeypad = Keypad (makeKeymap


(keys), rowPins, colPins, ROWS,COLS);
const byte ROWS = 4;

const byte COLS = 4;


void setup ()

{
char keys[ROWS][COLS]= {
Serial.begin(9600);
{'1' , '2', '3', '+'},
}
{'4' , '5', '6', '-'},

{'7' , '8', '9', '*'},


void loop(){
char customKey = customKeypad.getKey(); second= SecondNumber();

switch(customKey) total=first*second;

{ Serial.println(total);

case '0'...'9': first=0; second=0;

first = first * 10+(customKey-'0'); break;

Serial.println(first);

break; case '/':

first = (total != 0 ? total : first);

case '+': Serial.println("/");

first = (total !=0? total:first); second= SecondNumber();

Serial.println("+");

second= SecondNumber(); second==0?


Serial.println("Invalid"):total=(float)first/(float)
total=first+second;
second;
Serial.println(total);

first=0; second=0;
Serial.println(total);
break;
first=0,second=0;

break;
case '-':

first = (total !=0?total:first);


case 'C':
Serial.println("-");
total=0;
second= SecondNumber();
break;
total=first-second;

Serial.println(total);
}
first=0; second=0;
}
break;
long SecondNumber()

{
case '*':
while(1)
first = (total !=0?total:first);
{
Serial.println("*") ;
customKey= customKeypad.getKey(); }

if(customKey>= '0' && customKey <='9') if(customKey == '=') break;

{ }

second= second*10+(customKey-'0'); return second;

Serial.println(second); }

3. Describe the output created


A calculator is a device that performs arithmetic operations on numbers. The simplest
calculators can do only addition, subtraction, multiplication, and division. In this activity, we
made a simple calculator using Arduino board and its software, 4x4 matrix keypad, and jump
wires that solves basic math like addition, subtraction, multiplication, and division. Wherein A is
[+], B is [-], C is [*], D is [/], # is [=], Asterisk (*) is to cut the connection of the previous
calculation to the new input.
By putting the corrected codes and after some modifications, the Arduino calculator will
recognize the operator and computes the results. The serial monitor serves as the screen or the
one that will print the output. The numbers that we press on the keyboard will be written to the
next line if we input two digits, and another line depends on the number of digits we input or its
place value. And because it is just a simple calculator, we can only put two given numbers to be
calculated at a time.

CONCLUSION

What are the types of pin you used? Explain why you chose them. What function and structure of
your program is essential in resolving the given problem? How?

The switch statement was necessary since it allocates a case based on the keypad button pressed. When
a specific button is pressed, the switch statement starts a loop that performs the arithmetic operation
that was assigned to that button. Furthermore, by employing the switch statement, the problem
became easier to code and comprehend. Using the function SecondNumber(), on the other hand, made
the program easier to code because it eliminated the need to create a code for getting the value of the
second integer in the arithmetic operation. Additionally, the while loop in the SecondNumber() function
is required for receiving and processing the second number's input as well as waiting before pressing the
'=' button.

1. Switch Statement
2. While Loop
3. SecondNumber ()

You might also like