Advanced Arduino Robotics Project #2, Arduino Advanced Calculator

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.

Video Lecture with Demo

The Code

/*
 * Calculator using Arduino
 * Designed by Vidyasagar Academy, Akola
 * Date: 04.08.2020
 * www.vsa.edu.in 
 */

double first_number; // the first number variable in calculations
double second_number; // the second number variable in calculations
char math_operator; // create a char variable to store the calculated value
double 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() > 1) 
  {    
    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
    delay(1000);
    Serial.println(); // creates one white space after each calculation
    Serial.println("Ready to calculate next...");
  }
}

void calculate() 
{ 
  // Custom function that performs the calculations  

  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; 
  }

  else if (math_operator == '/')  
  {
    answer = first_number / second_number; 
  }
}

More Advanced Arduino Projects

All these projects are password protected and paid projects. If you want construct these projects you will have to register and make online payment of Rs.1000/- For more details, contact us.

  1. Advanced Arduino Robotics Project #1, The project of Sanguinoscope Blood Group Typing Project
  2. Advanced Arduino Robotics Project #2, Arduino Advanced Calculator
  3. Advanced Arduino Robotics Project #3, Arduino Security System cum Door Bell using Piezo Sensor
Share on your network!
Dattaraj Vidyasagar
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... ➤➤

3 Comments

  1. You can also calculate the percentage by inserting following code in void calculate() function.

    [php]
    else if (math_operator == ‘%’)
    {
    answer = (first_number * 100) / second_number;
    }
    [/php]

  2. An interesting discussion is worth comment. I think that you should write more on this topic, it might not be a taboo subject but generally people are not enough to speak on such topics. To the next. Cheers

Leave a Reply

Your email address will not be published. Required fields are marked *