To work on this project, just connect Arduino UNO with data cable to your PC or laptop. Then upload the given program in it. Press Ctrl-Shift-M to open Serial Monitor window. Enter two numbers with required mathematical operator, and press Enter.
Examples
- Type 3+5 and press Enter to get the answer.
- Type 36/3 and press Enter to get the answer.
- Type 785*562348 and press Enter to get the answer.
- Type 574-394 and press Enter to get the answer.
Video Guidelines
The Code
/*
* Calculator using Arduino
* Designed by Vidyasagar Academy, Akola
* Date: 04.08.2020
* www.vsa.edu.in
*/
int first_number; // the first number variable in calculations
int second_number; // the second number variable in calculations
char math_operator; // create a char variable to store the calculated value
int answer; // the answer of the calculation
/* ***** IMPORTANT NOTE *****
* This calculator can calculate only the integer values.
* This calculator can calculate only two numbers at its input, for example:
* 2+2 is allowed, but 2+2+2+2 is not allowed.
*/
void setup()
{
Serial.begin(9600); // begins serial communications
Serial.println("Welcome to my calculator...!");
Serial.println("Ready to Calculate...");
Serial.println(" ");
}
void loop()
{
while(Serial.available() > 0)
{
first_number = Serial.parseInt();
math_operator = Serial.read();
second_number = Serial.parseInt();
calculate(); // user defined function to perform the calculations
Serial.print(first_number);
Serial.print(" "); // create one space after first number
Serial.print(math_operator);
Serial.print(" "); // create one space after operator
Serial.print(second_number);
Serial.print(" = ");
Serial.println(answer); // Prints the answer of the calculation
Serial.println("Ready to calculate next...");
delay(1000);
}
}
void calculate()
{
// Custom function that performs the calculations
if (math_operator == '+') // character literal (see bottom note)
{
answer = first_number + second_number;
}
else if (math_operator == '-')
{
answer = first_number - second_number;
}
else if (math_operator == '*')
{
answer = first_number * second_number;
}
else if (math_operator == '/')
{
answer = first_number / second_number;
}
}
/*
Literals or constants are the values we write in a conventional form
whose value is obvious. In contrast to variables,
literals (123, 4.3, "hi") do not change in value.
These are also called explicit constants or manifest constants.
In C and in C++ single quotes identify a single character,
while double quotes create a string literal.
'a' is a single a character literal, while "a" is a string literal
containing an 'a' and a null terminator (that is a 2 char array).
*/
Circuit Diagram
No circuit diagram required. Just connect your Arduino Board to PC, upload the code and then open Serial monitor to input the numerical values and get the answer.
