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.
EXPANDED TECHNICAL DETAILS
GUI & Touch Interface Development
This project turns an Arduino and a TFT shield into a fully functional, standalone graphical calculator.
- Touchscreen Matrix: Uses the 4-wire resistive touch interface of the 2.4" TFT. The Arduino implements a "Button Mapping" logic where specific (X, Y) touch coordinates are translated into numerical or operator inputs ('+', '-', '*', '/').
- Arithmetic Engine: The firmware handles floating-point math and manages a string-to-number buffer to allow for multi-digit calculations.
Visual Rendering
- Adafruit_GFX UI: The calculator layout features shaded buttons and a real-time "Result Display" bar. The code uses optimized "Invalidate" rendering, only redrawing specific areas of the screen to maintain a fast, responsive user interface.