Simple Calculator using Arduino IDE

Tabrez Ahmed
3 min readMay 1, 2021

--

If you are a beginner in Arduino then writing code for this program should be good practice to get to know the terms used in writing code for the Arduino.

Here are some terms to learn to write the code:

  1. Under setup section, we have the Serial.begin(9600), which means that we require the serial monitor to input values and to obtain output as well. When we want to display something in the serial monitor we have to initiate a serial connection and when we do so we need to mention the rate at which we will be transferring the data. here the data is transferred at the rate of 9600 bits per second.
  2. Serial.print(“”) is used to display text on the serial monitor
  3. Serial.available() is used inside the while loop to check if the user has given an input value. When serial input is available Serial.available() becomes 1 and execution comes out of the while loop.
  4. Serial.read(); is used to read the input given by the user and is stored in some variable(here num1 and num2)
  5. Here we use a switch statement to give the user choice of operations to be carried out on the 2 numbers. the choice variable is of integer data type and the operations are of string type hence we use the .toInt(); function to convert them to an integer.

basically, we use to.int() to convert a valid string to an integer. The input string should start with an integer number otherwise the function will stop performing the conversion

so num1.toint() converts the entered string (by user) into the integer number provided.

In the case of DIVISION alone to.Float() is used because division can give us decimal values. If we use int, it will give only whole numbers after the decimal point.

We print the result in each case because the data type could vary. The default statement Is “Invalid Operation” if the user gives a value other than 1,2,3,4. Finally, we have a delay and close the loop section

The print statement may look weird to you but don't worry it is just the concatenation of a string.

String num1= "";
String num2= "";
void setup() {
Serial.begin(9600);//to display value on serial monitor
Serial.println(" SIMPLE CALACULATOR ");
Serial.println("---------\n");
}void loop() {
//input first number
Serial.print("Enter the first number: ");
while(Serial.available()==0)
{
//wait for input
}
num1= Serial.readString();
Serial.println(num1);
// input 2nd numberSerial.print("Enter the second number: ");
while(Serial.available()==0)
{
}
num2= Serial.readString();
Serial.println(num2);

//operation menu
Serial.println("\nOperation Menu:");
Serial.println("'''''''''''''''");
Serial.println("1) Addition");
Serial.println("2)Subtraction");
Serial.println("3)Multiplication");
Serial.println("4)Division");
Serial.println("5)Modulo");
Serial.print("Choose your operation: ");
while(Serial.available()==0){

}
int choice = Serial.readString().toInt();
switch(choice){
case 1:
Serial.println("Addition");
Serial.println(num1+"+"+num2+"="+ (num1.toInt()+num2.toInt())+"\n");
break;
case 2:
Serial.println("Subtraction");
Serial.println(num1+"-"+num2+"="+(num1.toInt()- num2.toInt())+"\n");
break;
case3:
Serial.println("Multiplication");
Serial.println(num1+"X"+num2+"="+ (num1.toInt()*num2.toInt())+"\n");
break;
case 4:
Serial.println("Division");
Serial.println(num1+"/"+ num2+"="+(num1.toFloat() / num2.toFloat())+"\n");
break;
case 5:
Serial.println("Modulo");
Serial.println(num1+"%"+num2+"="+(num1.toInt() % num2.toInt())+"\n");
break;
default:
Serial.println("INVALID OPERATION");
break;
}
delay(1000);

}

Don't forget to connect the Arduino board while uploading the code. Good luck! and Have Fun!

After uploading, Press the Magnifying glass button(Serial monitor)
Input 2 numbers here I have input digits 11 and 12
Choose your operation, I have chosen Modulo operator
Result

--

--

Tabrez Ahmed
Tabrez Ahmed

Written by Tabrez Ahmed

Electronics & Communication Engineering graduate RV College of Engineering.

No responses yet