Touchscreen calculator using Arduino and TFT LCD Display

This time, I have made a calculator with the TFT display and Arduino. This note describes the steps I followed.

Required Materials

Hardware

  • Arduino board (I’ve used UNO)
  • TFT Touchscreen display (Must be compatible with Adafruit TFTLCD library)

Software

  • Arduino IDE

The TFT

To know more about how to program a TFT display for Arduino, you may read this note.

Calculator Display and Logic

Draw buttons for digits and operators and the display field of the calculator. All digits and operator buttons have the same size. So, the co-ordinates of each button can be calculated in the program.

When a screen press is detected, the program calculates the row and column co-ordinates and identifies the button pressed.

If a number button is pressed, the program will add that digit to a char array. It has two arrays for storing the first and second number. If the first array and the operator is empty, it will add the digit pressed to the first array. If the operator isn’t empty, it will add digits to the  second char array.

When the ‘=’ button is pressed, it will check both the arrays and the operator. If atleast one of them is empty, it will return back to the loop section without calculating anything.

If both arrays and operator have values, it will first convert the first and second array into integer numbers. The below function does the conversion :

int32_t chararraytoint(char *a)
{
  int32_t len = strlen(a);
  int32_t finalnum = 0;
  int32_t intval = 0;
  int32_t placeval = 1;

  if (len > 0)
  {
    for (int i = len - 1; i >= 0; i--)
    {
      intval = chartoint(a[i]); 
      intval = intval * placeval;
      finalnum = finalnum + intval;
      placeval *= 10;
    }
  }
  return finalnum;
}

In the character array, the digit in the last array location will be in the units place. For converting the array into a number, we iterate from the last array position to position 0.

To begin, the place value of the number is set to 1(ones place). The for loop will iterate until it reaches array position 0.  

To convert a character location into an integer, the chartoint() function is used. Once the integer is obtained for the digit, it is multiplied with its place value and the result is added to finalnum variable. The placevalue is multiplied by 10 in every iteration of the loop, so that when it reaches position 0, it will have the highest placevalue.

The finalnum variable will have the integer number at the end of the for loop.

It also has a screen saver which will be activated after 15 seconds of inactivity. The variable named time will be incremented by 1 every second. When it receives a touch, it will set time variable to 0. If the time variable exceeds 15 seconds, it will enter the screen saver loop. Inside, it will keep reading the touchscreen and also print shapes in random colours and sizes. Whenever it reads a touch, the logic will return to the main loop, resetting time variable to 0.

End note

The complete code is available here for download.

Hope this was useful. Let me know what you think about the project.  You can post any queries or comments right below this article.

Thanks for reading.

Create a Menu on TFT LCD Shield for Arduino

Written by Joseph Justin

I am a gadget freak, electronics enthusiast and passionate programmer. My current interests are electronics platforms like Arduino and various IOT boards. Attending school (St. Peter's Senior Secondary School, Kadayiruppu) by day and coding by night is what I do.