Arduino Mega – Making a Calculator (Serial Port)

In this tutorial, I am going to show you how to make a calculator using Arduino Mega 2560 but you can use any Arduino board. This calculator uses only UART module, to communicate with the computer. You don´t need any extra parts.

Considerations

After opening the serial monitor, you should ensure that the 9600 and “newline” are selected, in the options in the bottom bar, as you can see in image below:

Code

As you can see in the code below, there is a function called “serialEvent ()”, this function is recognized by the native libraries of Arduino and therefore the name of this function can not be changed, otherwise, it will not be recognized and the system will not work.

Copy the code below to your Arduino IDE project and save it. Then open the serial monitor of IDE or any other and insert the numbers and operator.

//variable declaration
int value1;
int value2;
char operation = "";
float result = 0;

int aux = 0;

String inputString = ""; // a String to hold incoming data
boolean nextInsertion = false;

void setup() {
  // init Serial Port with baudrate = 9600
  Serial.begin(9600);
  // reserve 100 bytes for the inputString:
  inputString.reserve(100);
  Serial.println("Calculator!!!");
  Serial.println("Enter the first number!");
  
}

void loop() {
  if (nextInsertion && aux == 0){
    nextInsertion = false;
    aux++; // increment var
    value1 = inputString.toInt();
    inputString = ""; // clear inputString
    Serial.print("value1: ");
    Serial.println(value1);
    Serial.println("Enter the operator (+, -, / or *)!");
  }
  if (nextInsertion && aux == 1){
    nextInsertion = false;
    aux++;
    operation = inputString[0]; //only first char
    inputString = "";    
    Serial.print("operation: ");
    Serial.println(operation);
    Serial.println("Enter the second number!");
  }
  if (nextInsertion && aux == 2){
    nextInsertion = false;
    aux++;
    value2 = inputString.toInt();
    inputString = "";
    Serial.print("value2: ");
    Serial.println(value2);
  }

  if (aux == 3){
    /* compute */
    aux = 0;
    switch( operation )
    {
      case '*':
        result = value1 * value2;
        break;
      case '/':
        result = value1 / value2;
        break;
      case '+':
        result = value1 + value2;
        break;
      case '%':
        result = value1 % value2;
        break;
      case '-':
        result = value1 - value2;
        break;
      default:
        Serial.println( "Erro! Operacao indefinida!\r" );
      return;
        
    }
  Serial.print("result: ");
  Serial.println(result);
  
  }
}

/*
SerialEvent occurs whenever a new data comes in the hardware serial Port. This
routine is independent of the loop(), but using delay inside loop can
delay response.
*/
void serialEvent()
{
  while( Serial.available() ) {
    // get a new byte:
    char inputByte = (char)Serial.read();
    // add byte to the String
    inputString += inputByte;
    if( inputByte == '\n' || inputByte == '\r' )
    { //next insertion
      nextInsertion = true;
    }
  }
}

Leave a Reply

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