Arduino blinking LED project

AR-AD2: Arduino Advanced Calculator

This entry is part 2 of 4 in the series Advanced Arduino Projects

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

  1. Type 3+5 and press Enter to get the answer.
  2. Type 36/3 and press Enter to get the answer.
  3. Type 785*562348 and press Enter to get the answer.
  4. Type 574-394 and press Enter to get the answer.

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.

Share on your network!
Prof. Dattaraj Vidyasagar
Prof. Dattaraj Vidyasagar

Author on this website. He is veteran of Core Electronics since last 36+ years. ATL Mentor of Change, Niti Ayog, Govt. of India, Google Certified Educator, International Robotics Trainer and author of 18 books on electronics, robotics, programming languages and web designing... ➤➤

0 0 votes
Article Rating
Subscribe
Notify of
guest

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
1
0
Would love your thoughts, please comment.x
()
x