Components and supplies
2.4 TFT LCD display
Arduino UNO
Apps and platforms
Arduino IDE
Project description
Code
The code
arduino
1#include <Adafruit_GFX.h> 2#include <Adafruit_TFTLCD.h> 3#include <TouchScreen.h> 4#define YP A3 5#define XM A2 6#define YM 9 7#define XP 8 8#define TS_MINX 140 9#define TS_MINY 120 10#define TS_MAXX 910 11#define TS_MAXY 920 12TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300); 13#define LCD_CS A3 14#define LCD_CD A2 15#define LCD_WR A1 16#define LCD_RD A0 17#define LCD_RESET A4 18Adafruit_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET); 19int32_t answer = 0; 20char lastchar = ' '; 21char keyarray1[200]; 22char keyarray2[200]; 23int32_t keyarray1i = 0; 24int32_t keyarray2i = 0; 25char operation = ' '; 26long Time = 0; 27long millicount = 0; 28int interval = 1000; 29int screenTimout = 15; 30#define BLACK 0x0000 31int32_t BLUE = tft.color565(50, 50, 255); 32#define DARKBLUE 0x0010 33#define VIOLET 0x8888 34#define RED 0xF800 35#define GREEN 0x07E0 36#define CYAN 0x07FF 37#define MAGENTA 0xF81F 38#define YELLOW 0xFFE0 39#define WHITE 0xFFFF 40#define GREY tft.color565(64, 64, 64); 41#define GOLD 0xFEA0 42#define BROWN 0xA145 43#define SILVER 0xC618 44#define LIME 0x07E0 45#define ORANGE 0xFD20 46#define ORANGERED 0xFA20 47#define minpressure 10 48#define maxpressure 1000 49#define row1x 0 50#define box1y 260 51#define boxsize 60 52#define r1x 190 53#define b1y 55 54int32_t tftheight = 0; 55int32_t tftwidth = 0; 56int32_t extray = 43; 57int32_t x, y = 0; 58 59char button[4][4] = { 60 { '7', '8', '9', '/' }, 61 { '4', '5', '6', '*' }, 62 { '1', '2', '3', '-' }, 63 { 'C', '0', '=', '+' } 64}; 65 66 67 68void draw() 69{ 70 tft.fillScreen(BLACK); 71 72 tft.drawRoundRect(row1x, box1y, boxsize, boxsize, 8, RED); 73 tft.drawRoundRect(row1x, box1y - boxsize, boxsize, boxsize, 8, WHITE); 74 tft.drawRoundRect(row1x, box1y - boxsize * 2, boxsize, boxsize, 8, WHITE); 75 tft.drawRoundRect(row1x, box1y - boxsize * 3, boxsize, boxsize, 8, WHITE); 76 77 for (int32_t b = box1y; b >= 80; b -= boxsize) 78 { tft.drawRoundRect (180, b, boxsize, boxsize, 8, BLUE); 79 tft.drawRoundRect (row1x + boxsize, b, boxsize, boxsize, 8, WHITE); 80 } 81 tft.drawRoundRect(row1x + boxsize * 2, box1y, boxsize, boxsize, 8, GREEN); 82 tft.drawRoundRect(row1x + boxsize * 2, box1y - boxsize, boxsize, boxsize, 8, WHITE); 83 tft.drawRoundRect(row1x + boxsize * 2, box1y - boxsize * 2, boxsize, boxsize, 8, WHITE); 84 tft.drawRoundRect(row1x + boxsize * 2, box1y - boxsize * 3, boxsize, boxsize, 8, WHITE); 85 86 for (int32_t j = 0; j < 4; j++) { 87 for (int32_t i = 0; i < 4; i++) { 88 tft.setCursor(22 + (boxsize * i), 100 + (boxsize * j)); 89 tft.setTextSize(3); 90 tft.setTextColor(WHITE); 91 tft.println(button[j][i]); 92 } 93 } 94 tft.drawRoundRect(2, 5, 235, 70, 8, ORANGERED); 95} 96 97 98void drawintro() 99{ 100 tft.fillScreen(BLACK); 101 tft.setTextSize(3); 102 tft.drawRoundRect(0, 0, 240, 319, 20, RED); 103 104 tft.setTextColor(BLUE); 105 tft.setCursor(30, 100); 106 tft.print("Welcome To"); 107 tft.setCursor(30, 130); 108 tft.setTextColor(YELLOW); 109 tft.print("Arduino.cc"); 110 tft.setCursor(30, 160); 111 tft.setTextColor(RED); 112 tft.print("CALCULATOR"); 113 tft.setCursor(45, 250); 114 tft.setTextColor(WHITE); 115 tft.print("Touch To"); 116 tft.setCursor(45, 280); 117 tft.print("Continue"); 118 tft.setCursor(30, 50); 119 tft.fillRoundRect(20, 40, 200, 40, 8, YELLOW); 120 tft.setTextColor(RED); 121 tft.print("Minuka"); 122 tft.fillRoundRect(25, 215, 180, 5, 8, GREEN); 123 digitalWrite(13, HIGH); 124 TSPoint p = ts.getPoint(); 125 digitalWrite(13, LOW); 126 pinMode(XM, OUTPUT); 127 pinMode(YP, OUTPUT); 128 129 130 int i = 1; 131 132 while (i == 1) 133 { 134 digitalWrite(13, HIGH); 135 TSPoint p = ts.getPoint(); 136 digitalWrite(13, LOW); 137 138 pinMode(XM, OUTPUT); 139 pinMode(YP, OUTPUT); 140 141 if (p.z > minpressure && p.z < maxpressure) 142 { 143 return; 144 } 145 } 146} 147 148void screenSaver() 149{ 150 digitalWrite(13, HIGH); 151 TSPoint p = ts.getPoint(); 152 digitalWrite(13, LOW); 153 pinMode(XM, OUTPUT); 154 pinMode(YP, OUTPUT); 155 tft.fillScreen(BLACK); 156 tft.setCursor(30, 100); 157 tft.print("ENTERING"); 158 tft.setCursor(10, 150); 159 tft.print("SCREEN SAVER"); 160 tft.setCursor(50, 200); 161 tft.print("MODE"); 162 delay(1000); 163 tft.fillScreen(RED); 164 165 166 int i = 0; 167 while (i == 0) 168 { 169 for (int i = 0; i < 150; i++) 170 { 171 int color = tft.color565(random(255), random(255), random(255)); 172 int x = random(350); 173 int y = random(350); 174 int r = random(30); 175 tft.fillCircle(x, y, r, color); 176 TSPoint p = ts.getPoint(); 177 digitalWrite(13, LOW); 178 pinMode(XM, OUTPUT); 179 pinMode(YP, OUTPUT); 180 181 if (p.z > minpressure && p.x < 1000) 182 { 183 draw(); 184 tft.setCursor(10, 30); 185 if (keyarray1[0] != '\\0') 186 { 187 tft.print(keyarray1); 188 189 if (operation != ' ') 190 { 191 tft.print(operation); 192 193 if (keyarray2[0] != '\\0') 194 { 195 tft.print(keyarray2); 196 } 197 } 198 } 199 else 200 tft.setCursor(10, 30); 201 202 return; 203 } 204 205 206 } 207 tft.fillScreen(tft.color565(random(255), random(255), random(255))); 208 } 209} 210 211 212void setup() 213{ 214 keyarray1[0] = '\\0'; 215 keyarray2[0] = '\\0'; 216 217 Serial.begin(9600); 218 Serial.println("Calculator"); 219 tft.begin(0x9341); 220 tft.setRotation(2); 221 drawintro(); 222 tftheight = tft.height(); 223 tftwidth = tft.width(); 224 draw(); 225 tft.setCursor(10, 30); 226} 227 228void loop() 229{ 230 digitalWrite(13, HIGH); 231 TSPoint p = ts.getPoint(); 232 digitalWrite(13, LOW); 233 pinMode(XM, OUTPUT); 234 pinMode(YP, OUTPUT); 235 236 x = map(p.x, TS_MINX, TS_MAXX, tft.width(), 0); 237 y = map(p.y, TS_MINY, TS_MAXY, tft.height(), 0) + 30; 238 239 if ((millis() - millicount) > interval) 240 { 241 millicount = millis(); 242 Time += 1; 243 } 244 if (Time >= screenTimout && (p.z < 10 || p.z > 1000)) 245 { 246 screenSaver(); 247 Time = 0; 248 } 249 250 if (p.z > 10 && p.z < 1000) 251 { 252 Time = 0; 253 millicount = millis(); 254 } 255 256 if (p.z > minpressure && p.z < maxpressure) 257 { 258 259 lastchar = ' '; 260 lastchar = idbutton(); 261 262 if (lastchar >= '0' && lastchar <= '9') 263 { 264 if (operation == ' ') 265 { 266 addchar(keyarray1, lastchar); 267 tft.print(lastchar); 268 Serial.println(keyarray1); 269 } 270 271 else if (operation != ' ') 272 { 273 addchar(keyarray2, lastchar); 274 tft.print(lastchar); 275 Serial.println(keyarray2); 276 } 277 } 278 279 if (lastchar == '+' || lastchar == '-' || lastchar == '/' || lastchar == '*' && keyarray2[0] == '\\0' && keyarray1[0] != '\\0') 280 { 281 if ( operation != ' ') 282 { 283 operation = lastchar; 284 tft.fillRoundRect(2, 5, 235, 70, 8, BLACK); 285 tft.drawRoundRect(2, 5, 235, 70, 8, ORANGERED); 286 tft.setCursor(10, 30); 287 tft.print(keyarray1); 288 tft.print(operation); 289 } 290 else 291 { 292 if (operation == ' ') 293 { 294 operation = lastchar; 295 tft.print(operation); 296 } 297 } 298 } 299 300 if (lastchar == 'C') 301 { 302 keyarray1[0] = '\\0'; 303 keyarray2[0] = '\\0'; 304 answer = 0; 305 306 keyarray1i = 0; 307 keyarray2i = 0; 308 309 operation = ' '; 310 draw(); 311 tft.fillRoundRect(2, 5, 235, 70, 8, BLACK); 312 tft.drawRoundRect(2, 5, 235, 70, 8, ORANGERED); 313 tft.setCursor(10, 30); 314 } 315 316 if (lastchar == '=') 317 { 318 if (keyarray1[0] != '\\0' && keyarray2[0] != '\\0') 319 { 320 Serial.println("Calculate"); 321 if (keyarray2[0] == '0' && operation == '/') 322 { 323 tft.setCursor(10, 30); 324 tft.fillRoundRect(2, 5, 235, 70, 8, BLACK); 325 tft.drawRoundRect(2, 5, 235, 70, 8, ORANGERED); 326 tft.print("="); 327 tft.setTextColor(RED); 328 tft.print("ERROR"); 329 tft.setTextColor(WHITE); 330 keyarray1[0] = '\\0'; 331 keyarray2[0] = '\\0'; 332 operation = ' '; 333 return; 334 } 335 keyarray1i = 0; 336 keyarray2i = 0; 337 keyarray1i = chararraytoint(keyarray1); 338 keyarray2i = chararraytoint(keyarray2); 339 340 answer = calc(keyarray1i, keyarray2i, operation); 341 342 tft.fillRoundRect(2, 5, 235, 70, 8, BLACK); 343 tft.drawRoundRect(2, 5, 235, 70, 8, ORANGERED); 344 tft.setCursor(10, 30); 345 tft.print('='); 346 tft.print(answer); 347 keyarray1i = 0; 348 operation = ' '; 349 keyarray2i = 0; 350 } 351 } 352 delay(500); 353 } 354 355 356} 357 358char idbutton() 359{ 360 //Row 1 identification 361 if ((tftwidth >= x) && (x >= (tftwidth - boxsize))) 362 { 363 364 if (((extray + boxsize) >= y) && (y >= extray)) 365 { 366 tft.drawRoundRect(row1x, box1y, boxsize, boxsize, 8, WHITE); 367 delay(100); 368 tft.drawRoundRect(row1x, box1y, boxsize, boxsize, 8, RED); 369 return 'C'; 370 } 371 372 if (((extray + (boxsize * 2)) >= y) && (y >= (extray + boxsize))) 373 { 374 tft.drawRoundRect(row1x, box1y - boxsize, boxsize, boxsize, 8, RED); 375 delay(100); 376 tft.drawRoundRect(row1x, box1y - boxsize, boxsize, boxsize, 8, WHITE); 377 return '1'; 378 } 379 380 if (((extray + (boxsize * 3)) >= y) && (y >= (extray + (boxsize * 2)))) 381 { 382 tft.drawRoundRect(row1x, box1y - boxsize * 2, boxsize, boxsize, 8, RED); 383 delay(100); 384 tft.drawRoundRect(row1x, box1y - boxsize * 2, boxsize, boxsize, 8, WHITE); 385 return '4'; 386 } 387 388 if (((extray + (boxsize * 4)) >= y) && (y >= (extray + (boxsize * 3)))) 389 { 390 tft.drawRoundRect(row1x, box1y - boxsize * 3, boxsize, boxsize, 8, RED); 391 delay(100); 392 tft.drawRoundRect(row1x, box1y - boxsize * 3, boxsize, boxsize, 8, WHITE); 393 return '7'; 394 } 395 396 } 397 398 //Row 2 identification 399 if (((tftwidth - boxsize) >= x) && (x >= (tftwidth - (boxsize * 2)))) 400 { 401 402 403 if (((extray + boxsize) >= y) && (y >= extray)) 404 { 405 tft.drawRoundRect(row1x + boxsize, box1y, boxsize, boxsize, 8, RED); 406 delay(100); 407 tft.drawRoundRect(row1x + boxsize, box1y, boxsize, boxsize, 8, WHITE); 408 return '0'; 409 } 410 411 if (((extray + (boxsize * 2)) >= y) && (y >= (extray + boxsize))) 412 { 413 tft.drawRoundRect(row1x + boxsize, box1y - boxsize, boxsize, boxsize, 8, RED); 414 delay(100); 415 tft.drawRoundRect(row1x + boxsize, box1y - boxsize, boxsize, boxsize, 8, WHITE); 416 return '2'; 417 } 418 419 if (((extray + (boxsize * 3)) >= y) && (y >= (extray + (boxsize * 2)))) 420 { 421 tft.drawRoundRect(row1x + boxsize, box1y - boxsize * 2, boxsize, boxsize, 8, RED); 422 delay(100); 423 tft.drawRoundRect(row1x + boxsize, box1y - boxsize * 2, boxsize, boxsize, 8, WHITE); 424 return '5'; 425 } 426 //8 427 if (((extray + (boxsize * 4)) >= y) && (y >= (extray + (boxsize * 3)))) 428 { 429 tft.drawRoundRect(row1x + boxsize, box1y - boxsize * 3, boxsize, boxsize, 8, RED); 430 delay(100); 431 tft.drawRoundRect(row1x + boxsize, box1y - boxsize * 3, boxsize, boxsize, 8, WHITE); 432 return '8'; 433 } 434 } 435 436 437 if (((tftwidth - boxsize * 2) >= x) && (x >= (tftwidth - (boxsize * 3)))) 438 { 439 440 if (((extray + boxsize) >= y) && (y >= extray)) 441 { 442 tft.drawRoundRect(row1x + boxsize * 2, box1y, boxsize, boxsize, 8, BLUE); 443 delay(100); 444 tft.drawRoundRect(row1x + boxsize * 2, box1y, boxsize, boxsize, 8, GREEN); 445 return '='; 446 } 447 448 if (((extray + (boxsize * 2)) >= y) && (y >= (extray + boxsize))) 449 { 450 tft.drawRoundRect(row1x + boxsize * 2, box1y - boxsize, boxsize, boxsize, 8, RED); 451 delay(100); 452 tft.drawRoundRect(row1x + boxsize * 2, box1y - boxsize, boxsize, boxsize, 8, WHITE); 453 return '3'; 454 } 455 456 if (((extray + (boxsize * 3)) >= y) && (y >= (extray + (boxsize * 2)))) 457 { 458 tft.drawRoundRect(row1x + boxsize * 2, box1y - boxsize * 2, boxsize, boxsize, 8, RED); 459 delay(100); 460 tft.drawRoundRect(row1x + boxsize * 2, box1y - boxsize * 2, boxsize, boxsize, 8, WHITE); 461 return '6'; 462 } 463 464 if (((extray + (boxsize * 4)) >= y) && (y >= (extray + (boxsize * 3)))) 465 { 466 tft.drawRoundRect(row1x + boxsize * 2, box1y - boxsize * 3, boxsize, boxsize, 8, RED); 467 delay(100); 468 tft.drawRoundRect(row1x + boxsize * 2, box1y - boxsize * 3, boxsize, boxsize, 8, WHITE); 469 return '9'; 470 } 471 472 } 473 474 //Row 4 identification 475 if (((tftwidth - boxsize * 3) >= x) && (x >= (tftwidth - (boxsize * 4)))) 476 { 477 478 if (((extray + boxsize) >= y) && (y >= extray)) 479 { 480 tft.drawRoundRect(row1x + boxsize * 3, box1y, boxsize, boxsize, 8, GREEN); 481 delay(100); 482 tft.drawRoundRect(row1x + boxsize * 3, box1y, boxsize, boxsize, 8, BLUE); 483 return '+'; 484 } 485 486 if (((extray + (boxsize * 2)) >= y) && (y >= (extray + boxsize))) 487 { 488 tft.drawRoundRect(row1x + boxsize * 3, box1y - boxsize, boxsize, boxsize, 8, GREEN); 489 delay(100); 490 tft.drawRoundRect(row1x + boxsize * 3, box1y - boxsize, boxsize, boxsize, 8, BLUE); 491 return '-'; 492 } 493 494 if (((extray + (boxsize * 3)) >= y) && (y >= (extray + (boxsize * 2)))) 495 { 496 tft.drawRoundRect(row1x + boxsize * 3, box1y - boxsize * 2, boxsize, boxsize, 8, GREEN); 497 delay(100); 498 tft.drawRoundRect(row1x + boxsize * 3, box1y - boxsize * 2, boxsize, boxsize, 8, BLUE); 499 return '*'; 500 } 501 502 if (((extray + (boxsize * 4)) >= y) && (y >= (extray + (boxsize * 3)))) 503 { 504 tft.drawRoundRect(row1x + boxsize * 3, box1y - boxsize * 3, boxsize, boxsize, 8, GREEN); 505 delay(100); 506 tft.drawRoundRect(row1x + boxsize * 3, box1y - boxsize * 3, boxsize, boxsize, 8, BLUE); 507 return '/'; 508 } 509 } 510} 511 512void addchar(char *array1, char inchar) 513{ 514 int32_t input_str_len = strlen(array1); 515 array1[input_str_len] = inchar; 516 position in the string 517 array1[input_str_len + 1] = '\\0'; 518} 519 520int32_t chartoint(char num) 521{ 522 523 return num - '0'; 524} 525 526int32_t calc(int32_t num1, int32_t num2, char op) 527{ 528 if (op == '+') 529 { 530 Serial.print(num1); 531 Serial.print(" "); 532 Serial.print(num2); 533 return (num1 + num2); 534 } 535 if (op == '-') 536 { 537 return (num1 - num2); 538 } 539 if (op == '*') 540 { 541 return (num1 * num2); 542 } 543 if (op == '/') 544 { 545 return (num1 / num2); 546 } 547} 548 549int32_t chararraytoint(char *a) 550{ 551 int32_t len = strlen(a); 552 int32_t finalnum = 0; 553 int32_t intval = 0; 554 int32_t placeval = 1; 555 556 if (len > 0) 557 { 558 for (int i = len - 1; i >= 0; i--) 559 { 560 intval = chartoint(a[i]); 561 intval = intval * placeval; 562 finalnum = finalnum + intval; 563 placeval *= 10; 564 } 565 } 566 return finalnum; 567} 568
Downloadable files
Clip the Display on the Arduino
Clip the Display on the Arduino
Clip the Display on the Arduino
Clip the Display on the Arduino
Comments
Only logged in users can leave comments
UNOProjects
2 years ago
You can change the characters in the code. But the code has no errors. char button[4][4] = { { '7', '8', '9', '/' }, { '4', '5', '6', '*' }, { '1', '2', '3', '-' }, { 'C', '0', '=', '+' } };
woodini254
4 years ago
Hi I have uploaded your code, everything works except the characters are all in the wrong place i.e. if I press the '7' I get the '/' instead. I am using an Eelegoo 2.4" display so I imagine that I need to change some of the code. Any help would be gratefully received as I am quite new to this. Rich