Finally!
I made a TFT LCD calculator!!!
I wanted to not see the internet for clues, but eventually I had to ( cause it was a little tough ) which helped me 0%. I mean literally. So all code was written by me.
So this is a touchscreen calculator suitable for 320*240 TFT LCD shields. You can copy-paste the code and try it out yourself!
A special thanks to david_prentice for his knowledge in displays and the creator of the amazing MCU_FRIEND Library!
I was first using strings in my code ( not understanding the evil of Strings ) but then after seeing this
and after my calculations failed a few times, I started using char arrays.
Char Arrays are amazing! easy to work with and best of all NO ERRORS like strings!!!
The calculator can perform simple calculations like +, -, *, / and for division it only gives the decimal ( float ) value if required.
Example: 6 / 3: Float value not required.
5 / 2: Float value required.
It also has a clear button ( ‘C’ ) to clear the screen and reset the calculator.
Code for the calculator:
#include <MCUFRIEND_kbv.h> // MCUFRIEND_kbv library
#include <TouchScreen.h> // Adafruit Touchscreen library
#include <Adafruit_GFX.h> // Adafruit graphics library
MCUFRIEND_kbv tft;
const int XP = 7, XM = A1, YP = A2, YM = 6;
const int TS_LEFT = 185, TS_RT = 897, TS_TOP = 153, TS_BOT = 887; // Calibrated
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);
TSPoint tp;
int xpos, ypos, n, n1, n2;
int xf, yf = 90, count;
int i;
int check, sum;
int op;
int check2;
float floatSum;
int cx;
int counterNum, floatCheck;
char ar[5];
#define white 0xFFFFFF
#define black 0x0000
#define blue 0x001F
#define red 0xF800
#define green 0x07E0
#define cyan 0x07FF
#define magenta 0xF81F
#define yellow 0xFFE0
void setup()
{
Serial.begin ( 9600 );
tft.begin ( 0x9325 );
count = 1;
for ( int j = 1; j <= 3; j++ )
{
for ( i = 1; i <= 3; i++ )
{
tft.fillRect ( xf, yf, 60, 60, yellow );
tft.setCursor ( xf + 20, yf + 10 );
tft.setTextSize ( 4 );
tft.setTextColor ( black );
tft.print ( count );
xf = xf + 60;
count = count + 1;
}
xf = 0;
yf = yf + 60;
}
tft.fillRect ( 0, 270, 60, 60, yellow );
tft.setCursor ( 20, 280 );
tft.setTextSize ( 4 );
tft.setTextColor ( black );
tft.print ( "C" );
tft.fillRect ( 60, 270, 60, 60, yellow );
tft.setCursor ( 80, 280 );
tft.setTextSize ( 4 );
tft.setTextColor ( black );
tft.print ( 0 );
tft.fillRect ( 120, 270, 60, 60, yellow );
tft.setCursor ( 140, 280 );
tft.setTextSize ( 4 );
tft.setTextColor ( black );
tft.print ( "=" );
tft.fillRect ( 180, 270, 60, 60, yellow );
tft.setCursor ( 200, 280 );
tft.setTextSize ( 4 );
tft.setTextColor ( black );
tft.print ( "/" );
tft.fillRect ( 180, 210, 60, 60, yellow );
tft.setCursor ( 200, 220 );
tft.setTextSize ( 4 );
tft.setTextColor ( black );
tft.print ( "*" );
tft.fillRect ( 180, 150, 60, 60, yellow );
tft.setCursor ( 200, 160 );
tft.setTextSize ( 4 );
tft.setTextColor ( black );
tft.print ( "-" );
tft.fillRect ( 180, 90, 60, 60, yellow );
tft.setCursor ( 200, 100 );
tft.setTextSize ( 4 );
tft.setTextColor ( black );
tft.print ( "+" );
cls();
cx = 10;
tft.setCursor ( cx, 45 );
check = 0;
check2 = 0;
counterNum = 0;
}
void loop()
{
floatSum = 0;
tft.setTextSize ( 5 );
tft.setTextColor ( white );
uint16_t xpos, ypos;
tp = ts.getPoint(); // Getting x, y ( touched )
pinMode ( XM, OUTPUT );
pinMode ( YP, OUTPUT );
xpos = map (tp.x, TS_LEFT, TS_RT, 0, tft.width());
ypos = map (tp.y, TS_TOP, TS_BOT, 0, tft.height()); // mapping the values
if ( tp.z >= 165 )
{
tft.setCursor ( cx, 45 );
if ( xpos >= 0 && ypos >= 90 && xpos <= 60 && ypos <= 150 && check == 0 )
{
tft.print ( "1" );
ar[counterNum] = '1';
counterNum = counterNum + 1;
cx = cx + 30;
}
if ( xpos >= 60 && ypos >= 90 && xpos <= 120 && ypos <= 150 && check == 0 )
{
tft.print ( "2" );
ar[counterNum] = '2';
counterNum = counterNum + 1;
cx = cx + 30;
}
if ( xpos >= 120 && ypos >= 90 && xpos <= 180 && ypos <= 150 && check == 0 )
{
tft.print ( "3" );
ar[counterNum] = '3';
counterNum = counterNum + 1;
cx = cx + 30;
}
if ( xpos >= 180 && ypos >= 90 && xpos <= 240 && ypos <= 150 && check == 0 )
{
tft.print ( "+" );
cx = cx + 30;
sum = atoi ( ar );
counterNum = 0;
op = 1;
memset( ar, '?', sizeof ( ar ) );
}
if ( xpos >= 0 && ypos >= 150 && xpos <= 60 && ypos <= 210 && check == 0 )
{
tft.print ( "4" );
ar[counterNum] = '4';
counterNum = counterNum + 1;
cx = cx + 30;
}
if ( xpos >= 60 && ypos >= 150 && xpos <= 120 && ypos <= 210 && check == 0 )
{
tft.print ( "5" );
ar[counterNum] = '5';
counterNum = counterNum + 1;
cx = cx + 30;
}
if ( xpos >= 120 && ypos >= 150 && xpos <= 180 && ypos <= 210 && check == 0 )
{
tft.print ( "6" );
ar[counterNum] = '6';
counterNum = counterNum + 1;
cx = cx + 30;
}
if ( xpos >= 180 && ypos >= 150 && xpos <= 240 && ypos <= 210 && check == 0 )
{
tft.print ( "-" );
cx = cx + 30;
sum = atoi ( ar );
counterNum = 0;
op = 2;
memset( ar, '?', sizeof( ar ) );
}
if ( xpos >= 0 && ypos >= 210 && xpos <= 60 && ypos <= 270 && check == 0 )
{
tft.print ( "7" );
ar[counterNum] = '7';
counterNum = counterNum + 1;
cx = cx + 30;
}
if ( xpos >= 60 && ypos >= 210 && xpos <= 120 && ypos <= 270 && check == 0 )
{
tft.print ( "8" );
ar[counterNum] = '8';
counterNum = counterNum + 1;
cx = cx + 30;
}
if ( xpos >= 120 && ypos >= 210 && xpos <= 180 && ypos <= 270 && check == 0 )
{
tft.print ( "9" );
ar[counterNum] = '9';
counterNum = counterNum + 1;
cx = cx + 30;
}
if ( xpos >= 180 && ypos >= 210 && xpos <= 240 && ypos <= 270 && check == 0 )
{
tft.print ( "*" );
cx = cx + 30;
sum = atoi ( ar );
counterNum = 0;
op = 3;
memset( ar, '?', sizeof( ar ) );
}
if ( xpos >= 0 && ypos >= 270 && xpos <= 60 && ypos <= 330 )
{
cls();
check = 0;
cx = 10;
check2 = 0;
memset( ar, '?', sizeof( ar ) );
}
if ( xpos >= 60 && ypos >= 270 && xpos <= 120 && ypos <= 330 && check == 0 )
{
tft.print ( "0" );
cx = cx + 30;
ar[counterNum] = '0';
counterNum = counterNum + 1;
}
if ( xpos >= 120 && ypos >= 270 && xpos <= 180 && ypos <= 330 && check == 0 )
{
check = 1;
cls();
}
if ( xpos >= 180 && ypos >= 270 && xpos <= 240 && ypos <= 330 && check == 0 )
{
tft.print ( "/" );
cx = cx + 30;
sum = atoi ( ar );
counterNum = 0;
op = 4;
memset( ar, '?', sizeof( ar ) );
}
delay ( 200 );
}
if ( check == 1 && check2 != 1 )
{
int n2 = 0;
check2 = check2 + 1;
n2 = atoi ( ar );
floatSum = sum;
switch ( op ) {
case 1:
sum = sum + n2;
break;
case 2:
sum = sum - n2;
break;
case 3:
sum = sum * n2;
break;
case 4:
sum = sum / n2;
floatSum = floatSum / n2;
floatCheck = 0;
if ( floatSum - sum != 0 )
{
floatCheck = 1;
}
break;
}
tft.setCursor ( 10, 45 );
tft.setTextColor ( white );
tft.setTextSize ( 5 );
tft.print ( "=" );
tft.setCursor ( 45, 45 );
if ( floatCheck == 0 ) {
tft.print ( sum );
}
if ( floatCheck == 1 ) {
tft.print ( floatSum );
}
cx = 10;
sum = 0;
counterNum = 0;
floatCheck = 0;
}
}
void cls()
{
tft.fillRect ( 0, 0, 240, 90, black );
tft.drawFastHLine ( 0, 90, 240, white );
tft.fillRect ( 0, 0, 240, 25, yellow );
tft.setCursor ( 35, 1 );
tft.setTextSize ( 3 );
tft.setTextColor ( black );
tft.print ( "Calculator" );
}
Sorry if I leave a lot of space but I usually code like that :D; Code is Auto Formatted.
Wiring: Just plug the shield into the Arduino. ( Nothin’ much )
Hope you all like it :)!!